R/Shiny中的Plain Dygraphs JavaScript选项 [英] Plain Dygraphs JavaScript options in R/Shiny
问题描述
是否有一种方法可以在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,服务器=服务器)
要获得有关Shiny的完整答案,我们可以做这样的事情. Is there a way to use plain Dygraphs JavaScript options in R (and Shiny more in specific)? I think the For example, I want to use A CSS solution has already been given ( i.e. Anyhow, here is a conceptual script of mine. It does not work, but all advice is much appreciated.
The For a fuller answer in Shiny, we could do something like this.
这篇关于R/Shiny中的Plain Dygraphs JavaScript选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! highlightSeriesOpts
导致突出显示的系列笔触变粗,并且不影响图例.您仍然需要适当的 CSS
来仅显示图例中最接近的系列.要按照您的建议设置 highlightSeriesOpts
,请在
库(发光)图书馆(书本)肺部死亡<-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(用户界面,服务器)
http://dygraphs.com/options.htmlJS()
function from the htmlwidgets package can be of use, but am not sure.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.dygraph-legend {display: none;}
and .dygraph-legend .highlight {display: inline;}
), but that somehow does not work in 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)
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))
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)