【问题标题】:RShiny Error: Error in .getReactiveEnvironment()$currentContext()RShiny 错误:.getReactiveEnvironment()$currentContext() 中的错误
【发布时间】:2023-03-27 01:28:01
【问题描述】:

我收到以下错误:

 "The following error happened while the function call GetForwardProduct():\n 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.)\n"

我的用户界面如下所示:

shinyUI(fluidPage(

                 dateRangeInput(inputId = "maSpreadDateRange", label = "Select Date Range", 
                                start = "2016-01-01", end = Sys.Date()),
                 selectInput(inputId = "maCommodity2", label = "Select Commodity", 
                             choices = c("Power", "Gas", "Oil", "Coal", "CO2", "EUR/USD")),
                 uiOutput(outputId = "maOutputMarketArea2"),
                 uiOutput(outputId = "maOutputBasePeak2"),
                 textInput(inputId = "maProd2", label = "Define Forward Product", value = "Cal-2021"),
                 actionButton(inputId = "goSpread", label = "Calculate")
                 
))

我的服务器

shinyServer(
function(input, output) {

  # Dynamical input selection for market area 2: #
  output$maOutputMarketArea2 <- renderUI({
    
    try({
      
      commodity <- input$maCommodity2
      
      if (commodity == "Power"){
          selectInput(inputId = "maMarketArea2", label = "Select Market Area", choices = c("DE", "AT"))
      } else if (commodity == "Gas"){
          selectInput(inputId = "maMarketArea2", label = "Select Market Area", choices = c("TTF", "NCG", "CEGH VTP"))
      } 
      
    })
    
  })
  
  # Dynamical input selection for base or peak 1: #
  output$maOutputBasePeak2 <- renderUI({
    
    try({
      
      commodity <- input$maCommodity2
      
      if (commodity == "Power"){
          selectInput(inputId = "maBasePeak2", label = "Select Base or Peak", choices = c("Base", "Peak"))
      }
      
    })
    
  })
  
  # Reactive expression for product 2 function call: #
  comm <- input$maCommodity2
  marketA <- input$maMarketArea2
  baseP <- input$maBasePeak2

      maProduct2 <- reactive({
        maProduct2 <- GetForwardProduct(commodity = comm, marketArea = marketA, basePeak = baseP, 
                                        product = input$maProd2)
      })
      

 

我不知道如何使用反应式表达,我对 RShiny 很陌生! reactive({ }) 中的函数GetForwardProduct() 是一个将字符串合并在一起的函数。 函数MASpread() 工作得很好,但不适用于反应式表达式。有人可以帮帮我吗?

【问题讨论】:

  • 不要在反应性之外创建变量commmarketAbaseP,而只是在反应性中直接在GetForwardProduct 调用中使用input$maCommodity2 等。 input 变量是您在 server 中调用的反应值,它不是反应环境
  • @starja 谢谢,现在不再显示带有反应值的错误了!但是还有很多其他错误和警告.....
  • 尝试理解错误信息并用谷歌搜索;我还建议阅读shiny tutorial
  • 问题是,print(maProduct2) 什么也没提供。所以有些东西不适用于reactive({}) 部分。

标签: r shiny


【解决方案1】:

你应该记住 input 应该被包裹在一个响应式中并且一个响应式应该与 () 相关联。例如,我正在稍微重写您的代码,使其工作并使用用户选择的输入打印文本。我将 maProduct2 包裹在一个响应式中,并使用 renderText 和 verbatimTextOutput 打印该响应式,如下所示。我不知道你的 getForwardProduct() 函数是做什么的,所以我在下面简单地使用 paste() 来说明。



library(shiny)

ui <- fluidPage(
  dateRangeInput(
    inputId = "maSpreadDateRange",
    label = "Select Date Range",
    start = "2016-01-01",
    end = Sys.Date()
  ),
  selectInput(
    inputId = "maCommodity2",
    label = "Select Commodity",
    choices = c("Power", "Gas", "Oil", "Coal", "CO2", "EUR/USD")
  ),
  uiOutput(outputId = "maOutputMarketArea2"),
  uiOutput(outputId = "maOutputBasePeak2"),
  textInput(
    inputId = "maProd2",
    label = "Define Forward Product",
    value = "Cal-2021"
  ),
  actionButton(inputId = "goSpread", label = "Calculate"),
  

  verbatimTextOutput("text")
)


server <- function(input, output, session) {
  # Dynamical input selection for market area 2: #
  output$maOutputMarketArea2 <- renderUI({
    try({
      commodity <- input$maCommodity2
      
      if (commodity == "Power") {
        selectInput(
          inputId = "maMarketArea2",
          label = "Select Market Area",
          choices = c("DE", "AT")
        )
      } else if (commodity == "Gas") {
        selectInput(
          inputId = "maMarketArea2",
          label = "Select Market Area",
          choices = c("TTF", "NCG", "CEGH VTP")
        )
      }
      
    })
    
  })
  
  # Dynamical input selection for base or peak 1: #
  output$maOutputBasePeak2 <- renderUI({
    try({
      commodity <- input$maCommodity2
      
      if (commodity == "Power") {
        selectInput(
          inputId = "maBasePeak2",
          label = "Select Base or Peak",
          choices = c("Base", "Peak")
        )
      }
      
    })
    
  })
  

  maProduct2 <- reactive({
    paste(input$maCommodity2,
          input$maBasePeak2,
          input$maProd2)
  })
  
  output$text <- renderText({
    maProduct2()
  })
  
  
}

shinyApp(ui, server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-09
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 2018-04-24
    • 2012-05-12
    • 1970-01-01
    相关资源
    最近更新 更多