R单击操作按钮1后出现闪亮的第二个操作按钮2

时间:2019-04-03 20:44:19

标签: r shiny action-button

我想我缺少了一些简单的东西。

我希望用户单击操作按钮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)

1 个答案:

答案 0 :(得分: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)