2017-05-27 25 views
1

我真的很喜欢dygraph的缩放功能,并尝试将它与闪亮的简单情节应用程序结合使用。我可以加载和绘制数据,但是当我通过滑块输入更改滚动因子或在放大部分绘图时使用鼠标单击添加事件时,绘图将以最大日期范围重新绘制。在更新时保留dygraph中的当前日期范围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 
+0

似乎拒绝所有的数据帧,我试试。我应该加载什么? “2列格式”对我来说毫无意义。 –

+0

感谢您的评论,我添加了一个适用于我的数据文件。 – Paul

+0

1 - 该文件不起作用,但我可以看到你的意思,所以我只是弄了一些数据来使程序工作。第一列正在被阅读作为一个因素。可能是你在你的系统上改变了一些阅读默认值。 –

回答

0

我使用的dyRangeSelectorretainDateWindow选项,并且当被加载的新数据,它被设置为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) 
    }) 

}) 
相关问题