2017-05-27 3 views
1

Rのdygraphsのズーム機能が本当に好きで、単純なプロットアプリの場合は光沢と組み合わせてみました。私はデータを読み込んでプロットすることができますが、スライダー入力でロール平均因子を変更するか、プロットの一部を拡大してマウスクリックでイベントを追加すると、プロットは最大の日付範囲で再レンダリングされます。rollmeanの更新時に現在の日付範囲をグラフに保存する

これを解決する試みとして、マウスクリックで現在のビューを保存する新しいリアクティブ関数を定義しました。これは私の望むほとんどのことですが、まだいくつかの問題があります。

(i)マウスのクリック(プロット内)後にのみ機能するので、ズームとロールミーンの変更は、マウスが作成されるまで元の縮尺を復元します

(ⅱ)パンは(スムーズに)もう

動作しない]をクリックし、新しいデータファイルがロードされたときに(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) 
    }) 

}) 
関連する問題