更新 rollmean 时在 dygraphs 中保留当前日期范围
Keeping current date range in dygraphs when updating rollmean
我非常喜欢 R 的 dygraphs 的缩放功能,并尝试将它与 shiny 结合起来用于一个简单的绘图应用程序。我可以加载和绘制数据,但是当我通过滑块输入更改 rollmean 因子或在放大部分图时使用鼠标单击添加事件时,图会重新呈现最大日期范围。
为了解决这个问题,我定义了一个新的反应函数,它可以在单击鼠标时存储当前视图。虽然这几乎是我想要的,但仍然存在一些问题
(i) 它仅在单击鼠标后(在图中)起作用,因此缩放和更改 rollmean 仍然会恢复原始比例,直到单击鼠标
(ii) 平移不再(平滑)工作
(iii) 加载新数据文件时,它不会调整日期范围以适应新文件。
MWE:
# 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)
})
})
Here是一个简单的数据文件。
> 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
选项块的末尾以获得所需的行为。
新建server.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)
})
})
我非常喜欢 R 的 dygraphs 的缩放功能,并尝试将它与 shiny 结合起来用于一个简单的绘图应用程序。我可以加载和绘制数据,但是当我通过滑块输入更改 rollmean 因子或在放大部分图时使用鼠标单击添加事件时,图会重新呈现最大日期范围。
为了解决这个问题,我定义了一个新的反应函数,它可以在单击鼠标时存储当前视图。虽然这几乎是我想要的,但仍然存在一些问题
(i) 它仅在单击鼠标后(在图中)起作用,因此缩放和更改 rollmean 仍然会恢复原始比例,直到单击鼠标
(ii) 平移不再(平滑)工作
(iii) 加载新数据文件时,它不会调整日期范围以适应新文件。
MWE:
# 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)
})
})
Here是一个简单的数据文件。
> 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
选项块的末尾以获得所需的行为。
新建server.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)
})
})