我真的很喜欢 R 的dygraphs 缩放功能,并尝试将其与 shiny 相结合,用于一个简单的绘图应用程序。我可以加载和绘制数据,但是当我通过滑块输入更改滚动均值因子或在放大部分绘图时使用鼠标单击添加事件时,绘图将以最大日期范围重新渲染。
为了解决这个问题,我定义了一个新的反应式函数,该函数在单击鼠标时存储当前视图。虽然这几乎是我想要的,但仍然存在一些问题
(i( 它仅在鼠标单击(在绘图中(后有效,因此缩放和更改滚动平均值仍会恢复原始比例,直到单击鼠标为止
(ii( 平移不再(顺利(工作
(iii( 加载新数据文件时,不会调整日期范围以适应新文件。
女工程师:
# ui.R
library(dygraphs)
shinyUI(fluidPage(
titlePanel("Simple plot"),
sidebarLayout(
sidebarPanel(
helpText("Data files must be 2 column format"),
fileInput("inputfile",label = "Load File"),
sliderInput("rollmean", label = "Running Average",
value = 1, min = 1, max = 25, step = 1),
textOutput("text1")
),
mainPanel(
dygraphOutput("dygraph")
)
)
))
和
# server.R
library(dygraphs)
shinyServer(function(input, output) {
spectrum <- reactive({
inFile <- input$inputfile
read.table(inFile$datapath)
})
currentview <- reactive({
if(is.null(input$dygraph_click$x))
{return(NULL)}
else
{
input$dygraph_date_window
}
})
cut <- reactive({
if (is.null(input$dygraph_click$x))
return(NULL)
cut <- input$dygraph_click$x
})
output$dygraph <- renderDygraph({
if (is.null(input$inputfile)){
return(NULL)
}
else{
dygraph(spectrum(), main = input$inputfile$name) %>%
dyOptions(drawXAxis = TRUE, drawYAxis = FALSE, drawGrid = FALSE,animatedZooms = FALSE) %>%
dyRangeSelector(dateWindow = currentview(), fillColor = "") %>%
dyRoller(rollPeriod = input$rollmean, showRoller = FALSE) %>%
dyEvent(cut())}
})
output$text1 <- renderText({
paste("You have selected", input$dygraph_click$x)
})
})
这是一个简单的数据文件。
> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X Yosemite 10.10.5
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
other attached packages:
[1] dygraphs_1.1.1.4 shiny_1.0.3
loaded via a namespace (and not attached):
[1] Rcpp_0.12.7 lattice_0.20-34 zoo_1.8-0 digest_0.6.10
[5] mime_0.5 grid_3.3.3 R6_2.2.1 xtable_1.8-2
[9] jsonlite_1.4 magrittr_1.5 xts_0.9-7 tools_3.3.3
[13] htmlwidgets_0.8 httpuv_1.3.3 yaml_2.1.13 htmltools_0.3.5
我通过使用 dyRangeSelector
的 retainDateWindow
选项和加载新数据时设置为 FALSE 的布尔参数找到了解决方案。我还必须将包含 dyRangeSelector
关键字的行移动到dygraphs
选项块的末尾,以获得所需的行为。
新服务器。R 文件:
# server.R
library(dygraphs)
shinyServer(function(input, output) {
spectrum <- reactive({
keepscale <<-FALSE
inFile <- input$inputfile
read.table(inFile$datapath)
})
cut <- reactive({
if (is.null(input$dygraph_click$x))
return(NULL)
cut <- input$dygraph_click$x
})
output$dygraph <- renderDygraph({
if (is.null(input$inputfile)){
keepscale <<-FALSE
return(NULL)
}
else{
simpleplot<-dygraph(spectrum(), main = input$inputfile$name) %>%
dyOptions(drawXAxis = TRUE, drawYAxis = FALSE, drawGrid = FALSE,animatedZooms = FALSE) %>%
dyRoller(rollPeriod = input$rollmean, showRoller = FALSE) %>%
dyEvent(cut()) %>%
dyRangeSelector(retainDateWindow=keepscale, fillColor = "")}
keepscale <<-TRUE
return(simpleplot)
})
output$text1 <- renderText({
paste("You have selected", input$dygraph_click$x)
})
})