【问题标题】:How can I add multiple shades with reactive() within shiny app?如何在闪亮的应用程序中使用 reactive() 添加多个阴影?
【发布时间】:2017-08-30 18:38:49
【问题描述】:

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

谢谢!

【问题讨论】:

  • 您必须对另一个reactiveobserve 中的reactive 表达式进行操作
  • 我做了另一个反应,但我得到了错误!你能更正代码吗?
  • 您需要提供您使用的变量的样本数据:X1(),df,input$NameOfMachine
  • 好的!请稍等
  • X1() 是一个 xts 对象 | G.pk --------------- | ---------------------- 2014-02-10 18:54:13 | 0.7210235 2014-02-10 19:22:11 | 0.7628376 2014-02-10 19:24:21 | 0.6971685 2014-02-11 09:01:32 | 0.4503746 2014-02-11 09:04:41 | 0.7056433 2014-02-11 09:22:57 | 0.7525473 日期 |页眉 ------ | ------ 细胞 |细胞

标签: r rstudio shiny shiny-server


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-07-27
    • 2020-06-16
    • 2021-11-05
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 2013-07-08
    相关资源
    最近更新 更多