reactiveValues

reactiveValues 可用於儲存物件,其他表示式可以依賴這些物件。

在下面的示例中,reactiveValues 物件初始化為值“尚未提交文字。”。每當按下提交按鈕時,都會建立一個單獨的觀察者來更新 reactiveValues 物件。請注意,reactiveValues 本身不依賴於其主體中的表示式。

library(shiny)

ui <- fluidPage(
  headerPanel("Example reactiveValues"),
  
  mainPanel(
    
    # input field
    textInput("user_text", label = "Enter some text:", placeholder = "Please enter some text."),
    actionButton("submit", label = "Submit"),
    
    # display text output
    textOutput("text"))
)

server <- function(input, output) {
  
  # observe event for updating the reactiveValues
  observeEvent(input$submit,
               {
    text_reactive$text <- input$user_text
  })
  
  # reactiveValues
  text_reactive <- reactiveValues(
    text = "No text has been submitted yet."
  )
  
  # text output
  output$text <- renderText({
    text_reactive$text
  })
}

shinyApp(ui = ui, server = server)