【问题标题】:How to to update data by clicking actionButton in R in runtime如何在运行时通过单击 R 中的 actionButton 来更新数据
【发布时间】:2022-01-02 21:23:54
【问题描述】:

我想每次都更新更新按钮上的输出数据。 这是我的代码,它在我第一次运行代码时显示更新按钮上的输出,但在运行时,如果输入发生更改,输出会自动更新。

library(shiny)

ui <- fluidPage(
  titlePanel("My Shop App"),
  
  sidebarLayout(
    sidebarPanel(
      helpText("Controls for my app"),
      
      selectInput("item", 
                  label = "Choose an item",
                  choices = list("Keyboard", 
                                 "Mouse",
                                 "USB",
      
      sliderInput("price", 
                  label = "Set Price:",
                  min=0, max = 100, value=10),
      
      actionButton ("update","Update Price")
    ),
    
    mainPanel(
      helpText("Selected Item:"),
      verbatimTextOutput("item"),
      helpText("Price"),
      verbatimTextOutput("price")
    )
  )
)

server <- function(input, output) {
  
  SelectInput <- eventReactive (input$update , {

    output$item = renderText(input$item)
    output$price = renderText(input$price)

  })

  output$item <- renderText(SelectInput())
  output$price <- renderText(SelectInput())
}

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny action-button


    【解决方案1】:

    要么创建一个依赖项并将它们放入reactive 并返回它:

    server <- function(input, output) {
      
      SelectInput <- eventReactive(input$update,{
        list(item = input$item, price = input$price)
      })
      
      output$item <- renderText(SelectInput()$item)
      output$price <- renderText(SelectInput()$price)
    }
    

    或者你可以isolate,但是你必须为每个听众添加按钮反应

    server <- function(input, output) {
    
      output$item <- renderText({
        req(input$update)
        input$update
        isolate(input$item)
      })
      
      output$price <- renderText({
        req(input$update)
        input$update
        isolate(input$price)
      })
      
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      • 2023-04-04
      • 1970-01-01
      • 2015-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多