R/Shiny 中的 Plain Dygraphs JavaScript 选项

i.p*_*ely 3 javascript r dygraphs shiny

有没有办法在 R 中使用普通 Dygraphs JavaScript 选项(更具体地说是 Shiny)?
http://dygraphs.com/options.html

我认为JS()htmlwidgets 包中的功能可以使用,但我不确定。

例如,我想使用highlightSeriesOpts(参见第一个链接)突出显示 dygraphs 图中的各个系列,以便仅显示图例中选定的系列(默认情况下并非同时显示所有系列)。以下链接中的下面 2 个图准确显示了要实现的目标:
http://dygraphs.com/gallery/#g/highlighted-series

已经给出了 CSS 解决方案(即.dygraph-legend {display: none;}.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)
Run Code Online (Sandbox Code Playgroud)

tim*_*lio 5

highlightSeriesOpts会使突出显示的系列笔划变得更粗,并且不会影响图例。您仍然需要正确的方式CSS仅显示图例中最接近的系列。要按照您的建议进行设置, http://rstudio.github.io/dygraphs/gallery-series-highlighting.htmlhighlightSeriesOpts中有一个清晰的示例。

lungDeaths <- cbind(ldeaths, mdeaths, fdeaths)

dygraph(lungDeaths, main = "Deaths from Lung Disease (UK)") %>%
  dyHighlight(highlightSeriesOpts = list(strokeWidth = 3))
Run Code Online (Sandbox Code Playgroud)

为了在 Shiny 中获得更完整的答案,我们可以这样做。

library(shiny)
library(dygraphs)

lungDeaths <- cbind(ldeaths, mdeaths, fdeaths)

ui <- dygraph(lungDeaths, main = "Deaths from Lung Disease (UK)") %>%
  dyHighlight(highlightSeriesOpts = list(strokeWidth = 3)) %>%
  dyCSS(textConnection("
     .dygraph-legend > span { display: none; }
     .dygraph-legend > span.highlight { display: inline; }
  "))

server <- function(input,output,session){

}

shinyApp(ui,server)
Run Code Online (Sandbox Code Playgroud)