【问题标题】:R Shiny make second action button 2 appear after action button 1 is clickedR Shiny 使第二个动作按钮 2 在单击动作按钮 1 后出现
【发布时间】:2019-04-03 20:44:18
【问题描述】:

我想我遗漏了一些非常简单的东西。

我希望我的用户单击操作按钮 1,以便显示操作按钮 2。但是如何在 ui 中“渲染”一个新的操作按钮?

我的代码如下。 非常感谢!

library(shiny)
ui = shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      actionButton("button1", label = "Press Button 1")
    ),
    mainPanel(
      # what should I write here?
      #renderPrint("button2")
    )
  )
))

server = shinyServer(function(input, output, session) {
  observeEvent(input$button1, {
    output$button2 <- renderUI({
      actionButton("button2", label = "Press Button 2")
    })
  })
})
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny action-button


    【解决方案1】:

    如果你在服务器端使用renderUI(),你必须在ui端使用uiOutput()

    完整代码如下:

    library(shiny)
    ui = shinyUI(fluidPage(
      sidebarLayout(
        sidebarPanel(
          actionButton("button1", label = "Press Button 1")
        ),
        mainPanel(
          # what should I write here?
          uiOutput("button2")
        )
      )
    ))
    
    server = shinyServer(function(input, output, session) {
      observeEvent(input$button1, {
        output$button2 <- renderUI({
          actionButton("button2", label = "Press Button 2")
        })
      })
    })
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 就是这样!非常感谢!
    • 一个简单的问题 - 我如何引用 button2 前进(例如,产生依赖于 button2 的 observeEvent?我还能说:observeEvent(input$button2 ...?或者我应该引用它吗?不一样?
    猜你喜欢
    • 2023-03-22
    • 2022-01-15
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 2016-08-03
    • 2020-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多