【问题标题】:How to automatically trigger an action button in R Shiny如何在 R Shiny 中自动触发动作按钮
【发布时间】:2020-05-27 04:31:29
【问题描述】:

我想在用户打开/登陆“tab1”时自动运行操作按钮。因此,我希望自动打印日期,而不是单击刷新按钮来查看日期。有没有办法做到这一点?我的真实代码比这个简单的例子更复杂。但是,它展示了我想做的事情。谢谢!

library(shiny)

ui <- fluidPage(
  shiny::tabPanel(value = 'tab1', title = 'Data page',
                  br(),
                  shiny::actionButton("btn", "Refresh!"),
                  br(),
                  shiny::verbatimTextOutput("out")
  )
)

server <- function(input, output, session) {
   curr_date <- shiny::eventReactive(input$btn, {
     format(Sys.Date(), "%c")
   })

   output$out <- shiny::renderText({
     print(curr_date())

   })
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny action-button


    【解决方案1】:

    您应该为此使用reactiveValues()observeEvent()。内部服务器函数:

    server <- function(input, output, session) {
    
      text_out <- reactiveValues(date = format(Sys.Date(), "%c"))
    
      observeEvent(input$btn, {
        text_out$date <- "something else"
      })
    
      output$out <- renderText({
         print(text_out$date)
    }
    

    【讨论】:

      【解决方案2】:

      你可以让curr_date 响应标签集:

      library(shiny)
      
      ui <- fluidPage(
        tabsetPanel(
          tabPanel(value = 'tab1', title = 'Data page',
                   br(),
                   actionButton("btn", "Refresh!"),
                   br(),
                   verbatimTextOutput("out")
          ),
          tabPanel(value = 'tab2', title = 'Other tab'),
          id = "tabset"
        )
      )
      
      server <- function(input, output, session) {
        curr_date <- eventReactive(list(input$btn, input$tabset), {
          req(input$tabset == 'tab1')
          format(Sys.time(), "%c")
        })
      
        output$out <- renderText({
          print(curr_date())
        })
      }
      
      shinyApp(ui, server)
      

      【讨论】:

      • 我不知道我们可以让它对标签集产生反应。非常感谢!
      猜你喜欢
      • 1970-01-01
      • 2021-08-16
      • 2019-07-14
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      相关资源
      最近更新 更多