r - 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()
运行良好,但不适用于反应式表达式。有人能帮我吗??
解决方案
你应该记住,输入应该被包裹在一个响应式中,并且一个响应式应该与 () 相关联。例如,我正在稍微重写您的代码,使其工作并使用用户选择的输入打印文本。我将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)
推荐阅读
- javascript - 在Android的Firefox上,谷歌一键式提示显示在右上角而不是底部
- python - Python 的类型库中的类型是全局变量吗?
- python - 从同一个种子启动不同的随机数生成器(Python)
- r - 如何在R管道中输出两次?
- java - 运行 jdbc 程序时线程“主”java.sql.SQLException 中的异常?
- mongodb - MongoDb 关闭 - 缺少索引文件
- sql - 如何替换 SQL 查询中的字符串
- webassembly - Web 程序集二进制文件会小于 JavaScript/TypeScript 包吗?
- c# - 删除 dotnet 应用程序中的 kubernetes pod
- c# - 已检索数据但未保存或更新 ASP.NET Core 3.1