R/Shiny中的Plain Dygraphs JavaScript选项 [英] Plain Dygraphs JavaScript options in R/Shiny

查看:64
本文介绍了R/Shiny中的Plain Dygraphs JavaScript选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以在R中使用普通的Dygraphs JavaScript选项(具体来说还有Shiny)?
http://dygraphs.com/options.html

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

例如,我想使用 highlightSeriesOpts (参见第一个链接)突出显示书本图中的单个系列,以便仅在图例中显示选定的系列(而不是在图例中显示所有系列).默认情况下是同一时间).以下链接中的下部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"))))服务器<-功能(输入,输出){set.seed(123)数据<-矩阵(rnorm(12),ncol = 2)数据<-ts(数据)#解决可能是错误的方法#参考:http://stackoverflow.com/questions/28305610/use-dygraph-for-r-to-plot-xts-time-series-by-year-only数据<-cbind(as.xts(data [,1]),as.xts(data [,2]))colnames(data)<-c(系列1",系列2")#print(data)#取消注释以查看数据框#以下逻辑是普通Dygraphs JavaScript#代码可用作绘图材料output $ plot<-JS(新Dygraph(图,数据,{highlightSeriesOpts:{strokeWidth:3}});g.updateOptions({HighlightSeriesOpts:{strokeWidth:3}});")}ShinyApp(ui = ui,服务器=服务器) 

解决方案

highlightSeriesOpts 导致突出显示的系列笔触变粗,并且不影响图例.您仍然需要适当的 CSS 来仅显示图例中最接近的系列.要按照您的建议设置 highlightSeriesOpts ,请在

要获得有关Shiny的完整答案,我们可以做这样的事情.

 库(发光)图书馆(书本)肺部死亡<-cbind(ldeaths,mdeaths,fdeaths)ui<-dygraph(lungDeaths,main =来自肺疾病的死亡(英国)")%&%;%dyHighlight(highlightSeriesOpts = list(strokeWidth = 3))%&%dyCSS(textConnection(.dygraph-legend>跨度{display:none;}.dygraph-legend>span.highlight {display:inline;}))服务器<-功能(输入,输出,会话){}ShinyApp(用户界面,服务器) 

Is there a way to use plain Dygraphs JavaScript options in R (and Shiny more in specific)?
http://dygraphs.com/options.html

I think the JS() function from the htmlwidgets package can be of use, but am not sure.

For example, I want to use highlightSeriesOpts (cf. first link) to highlight individual series in a dygraphs plot in order to display ONLY the selected series in the legend (and not all series at the same time by default). The lower 2 plots in the following link show exactly what is to be achieved:
http://dygraphs.com/gallery/#g/highlighted-series

A CSS solution has already been given ( i.e. .dygraph-legend {display: none;} and .dygraph-legend .highlight {display: inline;} ), but that somehow does not work in R/Shiny.

Anyhow, here is a conceptual script of mine. It does not work, but all advice is much appreciated.

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)

解决方案

The highlightSeriesOpts causes the highlighted series stroke to be bolder and does not affect the legend. You will still need the proper CSS to only show the closest series in the legend. To set the highlightSeriesOpts as you suggest, there is a clear example at 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))

For a fuller answer in Shiny, we could do something like this.

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)

这篇关于R/Shiny中的Plain Dygraphs JavaScript选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆