【发布时间】:2016-06-01 16:57:43
【问题描述】:
我希望使用下面的简化代码作为示例,了解 Shiny 的反应行为。
当 y 在应用中更新时,图表也会更新。
当 x 在应用程序中更新时,图表不会更新。
我已经阅读了 Shiny 的教程,并且我的理解是,鉴于我已经将 test() 和 plot() 函数都包装在了 observeEvent 中,这两个参数不应该导致图形在更改时更新。
有人可以帮忙解释一下这背后的逻辑吗?
library(shiny)
test <- function(x){x*2}
shinyServer(function(input, output, session) {
observeEvent(input$run, {
x = test(input$x)
output$distPlot <- renderPlot({
if(input$y){
x = x+2
}
plot(x)
})
})
})
shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
numericInput("x", "x:", 10),
checkboxInput("y", label = "y", value = FALSE),
actionButton("run", "run")
),
mainPanel(
plotOutput("distPlot")
)
)
))
【问题讨论】: