假设我有两个动作按钮。当我按下“Go!”时,我想计算一个值然后,按下第二个动作按钮,我想在另一个计算中使用计算值。此代码不起作用,并给出如下错误:
“警告:observeEventHandler中出错:找不到对象'coeff'”
为什么它没有给我答案?
library(shiny)
ui <- fluidPage(
actionButton(inputId = "AB", label = "Go!"),
actionButton(inputId = "AB1", label = "Calculate!")
)
server <- function(input, output, session) {
observeEvent(input$AB,{
coeff <- sum(1:15)
})
observeEvent(input$AB1,{
calculatedValue <- coeff*10
})
}
shinyApp(ui = ui, server = server)
答案 0 :(得分:2)
您最好使用reactiveValues
,因为内部observeEvent
并不知道coeff
是什么,因为它只认为它是一个局部变量,所以请尝试下面的代码。您可以使用v$coeff
和v$calculatedValue
library(shiny)
ui <- fluidPage(
actionButton(inputId = "AB", label = "Go!"),
actionButton(inputId = "AB1", label = "Calculate!"),
textOutput("myValue")
)
server <- function(input, output, session) {
v <- reactiveValues()
observeEvent(input$AB,{
v$coeff <- sum(1:15)
})
observeEvent(input$AB1,{
v$calculatedValue <- v$coeff*10
})
output$myValue <- renderText( v$calculatedValue)
}
shinyApp(ui = ui, server = server)