繁体   English   中英

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

[英]How can I add multiple shades with reactive() within shiny app?

我试图使用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

谢谢 !

我只是在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)
  })
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM