【问题标题】:Plain Dygraphs JavaScript options in R/ShinyR/Shiny 中的普通 Dygraphs JavaScript 选项
【发布时间】:2016-06-26 21:31:21
【问题描述】:

有没有办法在 R 中使用普通的 Dygraphs JavaScript 选项(特别是 Shiny)?
http://dygraphs.com/options.html

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

例如,我想使用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)

【问题讨论】:

  • 我无法回答你的问题,但为什么不直接使用rstudio.github.io/dygraphs 呢?
  • 我已经在使用 R 的 dygraphs 包了 :)。但是,没有方便的内置功能仅显示图例中突出显示的系列。此功能需要 CSS,但 dygraphs 网站上提供的 CSS 代码不知何故在 R/Shiny 中不起作用。很可能它在执行期间被覆盖。那么有什么办法可以追查到呢?

标签: javascript r shiny dygraphs


【解决方案1】:

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

lungDeaths <- cbind(ldeaths, mdeaths, fdeaths)

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

为了在 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)

【讨论】:

  • 谢谢! .dygraph-legend &gt; span {display:none;} 和 .dygraph-legend &gt; span.highlight {display:inline;} 成功了 :)。重要的是要注意&gt; span 是使其在 R/Shiny 中工作所必需的。我之前找到的 CSS 代码(参见 OP),没有提到这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-27
  • 2015-12-02
  • 1970-01-01
  • 2018-06-26
  • 1970-01-01
相关资源
最近更新 更多