【发布时间】: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() 工作得很好,但不适用于反应式表达式。有人可以帮帮我吗?
【问题讨论】:
-
不要在反应性之外创建变量
comm、marketA和baseP,而只是在反应性中直接在GetForwardProduct调用中使用input$maCommodity2等。input变量是您在server中调用的反应值,它不是反应环境 -
@starja 谢谢,现在不再显示带有反应值的错误了!但是还有很多其他错误和警告.....
-
尝试理解错误信息并用谷歌搜索;我还建议阅读shiny tutorial
-
问题是,
print(maProduct2)什么也没提供。所以有些东西不适用于reactive({})部分。