基于其他 2 个动作按钮显示和隐藏动作按钮

Display and hide actionButton based on 2 other actionButtons

下面我按第一个 actionButton() "Show" 显示另一个 actionButton() 但我还想要第二个 actionButton() 命名为 "Hide" 将隐藏 actionButton()单击 "Show".

后显示
library(shiny)
ui = shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      actionButton("button1", label = "Show"),
      actionButton("button1b", label = "Hide")

    ),
    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)

一个选项是将第二个按钮放在 conditionalPanel 内,并设置一个切换到 display/hide 面板。请参阅下面的工作代码。

library(shiny)
ui = shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      actionButton("button1", label = "Show"),
      actionButton("button1b", label = "Hide")
      
    ),
    mainPanel(
      # what should I write here?
      conditionalPanel(condition = "output.display",
                       actionButton("button2", label = "Press Button 2"))
    )
  )
))

server = shinyServer(function(input, output, session) {
  r <- reactiveValues(
    toggle = NULL
  )
  observeEvent(input$button1, {
    r$toggle = 1
  })
  observeEvent(input$button1b, {
    r$toggle = 0
  })
  output$display <- reactive({
    r$toggle
  })
  outputOptions(output, "display", suspendWhenHidden = FALSE)  
})
shinyApp(ui = ui, server = server)

另一种选择是动态插入和删除 UI 元素。但是每次单击按钮时,该选项都需要 creation/destruction 个 UI 个元素。 See example here