簡體   English   中英

閃亮的反應性解釋(使用ObserveEvent)

[英]Shiny Reactivity Explaination (using ObserveEvent)

我希望使用下面的簡化代碼作為示例來清楚Shiny的反應行為。

當y在應用程序中更新時,圖表會更新。
當x在應用程序中更新時,圖表不會更新。

我已經閱讀了Shiny的教程,我的理解是,鑒於我在observeEvent中包含了test()和plot()函數,這兩個參數都不應該導致圖形在更改時更新。

有人可以幫助解釋這背后的邏輯嗎?

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")
    )
  )
))

如果將行x = test(input$x)放在renderPlot ,它將在x或y更改時作出反應。 實際上,觀察者在第一次單擊操作按鈕時會創建一個反應輸出,然后您只需要一個響應元素來響應其內部輸入的更改。 希望有所幫助。

為了使圖形僅在單擊按鈕時更新,您可能需要將正在繪制的數據放在eventReactive中並將其用作圖形的輸入。

像這樣的東西:

data <- eventReactive(input$run, {
    x = test(input$x)
    if(input$y){
      x = x+2
    }
    x
  })
output$distPlot <- renderPlot({
  plot(data())
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM