有没有办法在 R 中使用普通 Dygraphs JavaScript 选项(更具体地说是 Shiny)?
http://dygraphs.com/options.html http://dygraphs.com/options.html
我觉得JS()
htmlwidgets 包中的函数可以使用,但我不确定。
例如,我想使用highlightSeriesOpts
(参见第一个链接)突出显示 dygraphs 图中的各个系列,以便仅在图例中显示选定的系列(默认情况下不会同时显示所有系列)。以下链接中的下面 2 个图准确地显示了要实现的目标:
http://dygraphs.com/gallery/#g/highlighted-series http://dygraphs.com/gallery/#g/highlighted-series
CSS 解决方案已经给出(即.dygraph-legend {display: none;}
and .dygraph-legend .highlight {display: inline;}
),但这在 R/Shiny 中不起作用。
无论如何,这是我的概念脚本。它不起作用,但非常感谢所有建议。
ui <- fluidPage(
sidebarLayout(
sidebarPanel(),
mainPanel(dygraphOutput("plot"))
)
)
server <- function(input, output) {
set.seed(123)
data <- matrix(rnorm(12), ncol = 2)
data <- ts(data)
# Workaround for what might be a bug
# Reference: http://stackoverflow.com/questions/28305610/use-dygraph-for-r-to-plot-xts-time-series-by-year-only
data <- cbind(as.xts(data[,1]), as.xts(data[,2]))
colnames(data) <- c("Series 1", "Series 2")
#print(data) # Uncomment to view data frame
# The logic of the following is that plain Dygraphs JavaScript
# code can be used as plotting material
output$plot <- JS("
new Dygraph(plot,
data,
{ highlightSeriesOpts: {strokeWidth: 3} });
g.updateOptions({ highlightSeriesOpts: {strokeWidth: 3} });
")
}
shinyApp(ui = ui, server = server)