在Shiny中使用动态radioButtons

时间:2016-10-02 06:04:06

标签: r shiny

在Shiny应用程序中,我在服务器上动态创建radioButtons,并使用renderUI将其传递给客户端。现在我遇到了将radioButtons(所选项目)的响应恢复进行进一步处理的问题。在我的问题的精简版下面。

library(shiny)

ui <- shinyUI(pageWithSidebar(  
    headerPanel("test dynamic radio buttons"),  
    sidebarPanel(    
    ),
    mainPanel(    
        x <- uiOutput('radioTest'),
        actionButton('submit', label = "Submit"),
        br(),
        print(paste("Radiobutton response is:", "reply()")),
        textOutput('text')
    )
))

server <- shinyServer(  
    function(input, output) {
        output$radioTest <- renderUI({
            options <- c("item 1", "item 2", "item 3")
            # The options are dynamically generated on the server
            radioButtons('reply', 'What item do you select ?', options, selected = character(0))
        })
        observe({
            input$submit
            isolate(
                output$text <- renderText({
                    paste("Radiobutton response is:", "reply()" )
                })
            )
        })
    }
)

# Run the application 
shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:2)

您想要以下内容吗?

library(shiny)

ui <- shinyUI(pageWithSidebar(  
  headerPanel("test dynamic radio buttons"),  
  sidebarPanel(    
  ),
  mainPanel(    
    x <- uiOutput('radioTest'),
    actionButton('submit', label = "Submit"),
    br(),
    #print(paste("Radiobutton response is:", "reply")),
    textOutput('text')
  )
))

server <- shinyServer(  
  function(input, output) {
    output$radioTest <- renderUI({
      options <- c("item 1", "item 2", "item 3")
      # The options are dynamically generated on the server
      radioButtons('reply', 'What item do you select ?', options, selected = character(0))
    })
    observe({
      input$submit

      isolate(
        output$text <- renderText({
          paste("Radiobutton response is:", input$reply )
        })
      )
    })
  }
)

# Run the application 
shinyApp(ui = ui, server = server)

enter image description here