【发布时间】:2019-06-07 22:43:06
【问题描述】:
我希望用户定义一个初始数值,然后在每次单击操作按钮时按设定的数量更新(即添加)。我使用 reactiveVal 尝试进行此更新,但我得到了可怕的 Operation not allowed without an active reactive context 错误。我很困惑,因为我认为使用 reactiveVal 是在反应式表达式中做某事,但显然我错了。
我已查找相关问题/文章/教程,但一无所获。非常感谢任何建议。请让我知道是否有更好的方法来完成这项任务,即我是否应该使用不同的功能或方法。
我的代码的精简版本如下:
library(shiny)
ui <- fluidPage(
## initilize the value
numericInput('init.i','initial i',10),
## click this button to add one to the initial value
actionButton("run","Run"),
## output the current count
textOutput("cur.i")
)
server <- function(input,output) {
## define the current value according to the inital value
i <- reactiveVal({
input$init.i ## this line fails
##1 ## this line doesn't fail but value always initializes at 1
})
observeEvent(input$run, {
## make a new value by adding 1 to the current value
new.i <- i() + 1
## update the value
i(new.i)
output$cur.i <- renderText({
## print the current value
i()
})
})
}
shinyApp(ui=ui,server=server)
【问题讨论】: