如何在闪亮的应用程序中添加带有反应()的多个阴影?

时间:2017-04-05 08:48:08

标签: r rstudio shiny shiny-server

我尝试使用for循环在dygrapgh函数中添加多个阴影。但我不能。

老实说,我正在寻找解决方案超过一周,我真的找不到任何建议。

请帮忙!

我写了这段代码:

dg= reactive ({
 dygraph(X1(), main ="interactive graph",
                  xlab = "time frame",
                  ylab = "records" ) %>% dyRangeSelector()

           }) 

# I have a table for the shades to be added, it's defined with reactive

shade_tab=reactive({ df[df$Equipement==input$NameOfMachine,] })  

# add shades

for( i in 1:nrow(shade_tab())) 
           { dg()= dyShading(dg(), from= shade_tab()$Date[i],
                  to= shade_tab()$Date[i] + 24*60*60 ,  
                  color = 'black')
           }
output$dygraph <- renderDygraph({ dg() })

这是我尝试的代码,但我总是收到错误消息:

Warning: Error in .getReactiveEnvironment()$currentContext:
Operation not allowed without an active reactive context. (You tried to do
something that can only be done from inside a reactive expression or
observer.)
Stack trace (innermost first):
46: .getReactiveEnvironment()$currentContext
45: .dependents$register
44: shade_tab
43: nrow
42: server [C:\Users\Curiosity\Desktop\Shiny Interface/server.R#90]
 1: runApp
Error in .getReactiveEnvironment()$currentContext() : 
Operation not allowed without an active reactive context. (You tried to do
something that can only be done from inside a reactive expression or 
observer.)
ERROR: [on_request_read] connection reset by peer

谢谢!

1 个答案:

答案 0 :(得分:0)

我刚刚在observe()中进行了快速试用,因为我发现更容易处理输入更改。更改输入值以查看1或2个阴影区域。

library(shiny)
library(dygraphs)
library(DT)

# ui.R
shinyUI(fluidPage(
  numericInput("input", "select", value=1, min=1, max=2),
  dygraphOutput("dyPlot"),
  dataTableOutput("dt")
))

# server.R
shinyServer(function(input, output) {
  df <- data.frame(from = c("1920-1-1", "1940-1-1"), to = c("1930-1-1", "1950-1-1"), range = c(1, 2), stringsAsFactors = FALSE)

  observe({
    df_up <- df[df$range <= input$input,]
    dy <- dygraph(nhtemp, main = "New Haven Temperatures") %>% dyRangeSelector()
    for(i in 1:nrow(df_up)) {
      dy <- dy %>% dyShading(from = df_up$from[i], to = df_up$to[i])
    }
    output$dyPlot <- renderDygraph(dy)

    output$dt <- renderDataTable(df_up)
  })
})