首页 > 解决方案 > RShiny 错误:.getReactiveEnvironment()$currentContext() 中的错误

问题描述

我收到以下错误:

 "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 很陌生!GetForwardProduct()中的函数reactive({ })是将字符串合并在一起的函数。该函数MASpread()运行良好,但不适用于反应式表达式。有人能帮我吗??

标签: rshiny

解决方案


你应该记住,输入应该被包裹在一个响应式中,并且一个响应式应该与 () 相关联。例如,我正在稍微重写您的代码,使其工作并使用用户选择的输入打印文本。我将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)


推荐阅读