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)