2016-10-04 45 views
3

我一直在使用dygraphs库将一些非常漂亮的时间序列图放在闪亮的应用程序中。我特别喜欢将group参数与dygraph结合使用dyRangeSelector来跨越多个dygraphs同步缩放级别。从闪亮的应用程序中提取dygraph中的dyRangeSelector值

有没有办法使其他闪亮的输出反应对用户操纵范围选择器?借此示例应用程序,它显示了一个简单dygraph和求和系列于下表:

# app.R 
library(shiny) 
library(dygraphs) 
library(dplyr) 

indoConc <- Indometh[Indometh$Subject == 1, c("time", "conc")] 

ui <- fluidPage(
    dygraphOutput("plot"), 
    tableOutput("table") 
) 

server <- shinyServer(function(input, output) { 

    output$plot <- renderDygraph({ 
    indoConc %>% 
     dygraph %>% 
     dyRangeSelector 
    }) 

    output$table <- renderTable({ 
    indoConc %>% 
     filter(time >= min(indoConc$time), time <= max(indoConc$time)) %>% 
     summarise(total_conc = sum(conc)) 
    }) 
}) 

shinyApp(ui, server) 

我想该表为只通过用户当前选择的时间间隔之和。这意味着更改filter行以使用除最小/最大点之外的其他值(这不会导致过滤)。

如何从相应格式的范围选择器中提取这两个值,以便我可以在filter调用中使用它们,并在用户移动滑块时反应性地更新表格?

+1

我可以看到那里有像一些隐藏的输入'plot_date_window'我可以使用,这要归功于这个答案:http://stackoverflow.com/questions/30955578/r-shiny-dygraphs - 系列反应的最新窗口。现在的诀窍是从中提取我的非时间序列y值;可能吗? – ClaytonJY

回答

2

由于在dataframetime变量是一个3位数的变量,我建议你转换datetime对象为character,然后选择最后3个位数是你需要的,粗到一个numeric进一步使用,如所以:

rm(list = ls()) 
library(shiny) 
library(dygraphs) 
library(dplyr) 
library(stringr) 

indoConc <- Indometh[Indometh$Subject == 1, c("time", "conc")] 
takeLastnvalues <- -3 
ui <- fluidPage(dygraphOutput("plot"),tableOutput("table")) 

server <- shinyServer(function(input, output,session) { 

    values <- reactiveValues() 
    observeEvent(input$plot_date_window,{ 
    value1 <- input$plot_date_window[[1]] 
    value2 <- input$plot_date_window[[2]] 
    value1 <- sub("Z", "", value1) 
    value2 <- sub("Z", "", value2) 
    value1 <- str_sub(value1,takeLastnvalues,-1) 
    value2 <- str_sub(value2,takeLastnvalues,-1) 
    values$v1 <- as.numeric(value1) 
    values$v2 <- as.numeric(value2) 
    }) 

    output$plot <- renderDygraph({ 
    indoConc %>% 
     dygraph %>% 
     dyRangeSelector 
    }) 

    output$table <- renderTable({ 
    indoConc %>% 
     filter(time >= min(values$v1), time <= max(values$v2)) %>% 
     summarise(total_conc = sum(conc)) 
    }) 
}) 

shinyApp(ui, server) 

enter image description here

+0

请注意,您只能提取整数。 (在你的截图中'values $ v1 = 2'和'values $ v2 = 4') – GyD

+0

@GyD我也注意到了;这是我设置的固有限制吗,还是有办法获得这些端点的“完整”值? – ClaytonJY

+0

我正在修复这个问题,但是它与'dygraphs'本身有关,时间戳只有毫秒范围,并且还有一些与之相关的故障单。你可以把你的'时间变量'修改为千位,然后在选择时修改它。 –

相关问题