【问题标题】:Use dynamic radioButtons in Shiny在 Shiny 中使用动态单选按钮
【发布时间】:2016-10-02 06:04:06
【问题描述】:

在一个闪亮的应用程序中,我在服务器上动态创建单选按钮并使用 renderUI 将其传递给客户端。现在我有一个问题是要返回单选按钮(所选项目)的响应以进行进一步处理。下面是我的问题的精简版。

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)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    你想要类似下面的东西吗?

    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)
    

    【讨论】:

    • 谢谢桑迪潘。这确实是本意。我剩下的问题是代码观察了提交按钮的响应,但是只要您激活另一个选项, input$reply 的隔离值仍然会立即更改。如何延迟 Radiobutton 响应直到 Submit 按钮被激活?从调试中我可以看到启动应用程序后立即运行输出$文本渲染(它不等待)
    • 我已经解决了响应问题。请参阅下面的代码。再次感谢您为我指明正确的方向。观察({ input$submit isolate( text
    猜你喜欢
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    • 2011-10-03
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    相关资源
    最近更新 更多