I’m creating a dygraph using this:
library(dygraphs)
library(htmlwidgets)
library(rgl)
knitr::opts_chunk$set(echo = TRUE)
knitr::knit_hooks$set(webgl = hook_webgl)
includeScript('C://Desktop//Javascripts/dygraph.js')
includeScript('C://Desktop//Javascripts/jquery-3.6.0.js')
df<-data.frame(date = seq(as.Date("1980-03-01), length.out = 100, by="day), Long = rnorm(100,50,10), Medium = rnorm(100,30, 5), Short = rnorm(100,10,2), Very.Short = rnorm(100,5,1))
dygraph(df, elementId="the_plot") %>% dyAxis("y", label= "Value ($s)") %>% dyOptions(labelsUTC = TRUE) %>% dyCrosshair(direction = "vertical") %>% dyHighlight(highlightCircleSize = 5, highlightSeriesBackgroundAlpha = 0.2, hideOnMouseOut = FALSE) %>% dyCallbacks(drawCallback = JS('
function(dygraph, is_initial) {
if (!is_initial) return;
var data = this.dataHandler_.data_;
var labels = dygraph.getLabels();
var num_cols = labels.length - 1; // exclude x-axis
var checkboxes_html = "";
// generate checkbox HTML for each series
for (var i = 1; i <= num_cols; i++) {
var label = labels[i];
var id = "checkbox" + i;
checkboxes_html += "<label><input type=\"checkbox\" id=\"" + id + "\" checked> " + label + "</label><br>";
}
// add checkboxes to the page
$("#checkboxes").html(checkboxes_html);
// add change event listeners to checkboxes
$("#checkboxes").find("input[type=checkbox]").change(function() {
var series = this.id.replace("checkbox", "");
var show = this.checked;
dygraph.setVisibility(parseInt(series), show);
});
}'))
I’m trying to add some checkboxes that’ll allow users of the knitted markdown html to show and hide individual series on that dygraph. While I can get it to knit to an html fine, I can’t get the checkboxes to render.
How can I fix this?
2
Answers
Does this work?
If that doesn’t work, you could try and create a plugin for it like here: https://rstudio.github.io/dygraphs/gallery-plugins.html