简体   繁体   English

基于选择的闪亮用户输入

[英]r shiny user input based on choice

I know how to add a text input or radio button or date input on the UI, what I am trying to do is, ask the user whether he wants to enter a text or a date range? 我知道如何在UI上添加文本输入或单选按钮或日期输入,我想做的是问用户是否要输入文本或日期范围? Depending on what the user chooses either show a text input or date input. 根据用户选择的内容,显示文本输入还是日期输入。 Not sure how to do this. 不确定如何执行此操作。

       Pseudo Code

       1) Please choose if you want to enter text or date range.
            Radio Button 1 - Text Input
            Radio Button 2 - Date Range

       2a) If the user chooses Radio Button 1, then Text Input should be displayed on the main panel, option to enter two dates (From & to) should not be displayed

       2b) If the user chooses Ratio Button 2, then the option to enter two dates (From & to) should be displayed on the Main panel and text input should not be displayed.

Not sure how to do this. 不确定如何执行此操作。 Need some pointers. 需要一些指针。

I am feeling generous here, generally you should provide your attempted code but here is a working example of how to have conditional inputs. 我在这里感到很慷慨,通常您应该提供尝试的代码,但这是如何有条件输入的有效示例。

library(shiny)

runApp(
    list(
        ui = pageWithSidebar(
            headerPanel("Option Input via Radio Buttons"),
            sidebarPanel(
                radioButtons("radio", label = h3("Radio buttons"),
                             choices = list("Date" = "date", "Text" = "text"), 
                             selected = 1),
                uiOutput("textORdate")
                ),
            mainPanel()
            ),
        server = function(input, output){
            output$textORdate <- renderUI({
                validate(
                    need(!is.null(input$radio), "please select a input type")
                    )
                if(input$radio == "text"){
                    textInput("mytext", "Text Input", "please enter text")
                }else{
                    dateRangeInput("daterange", "Date range:",
                                   start = "2012-01-01",
                                   end   = "2015-03-06")
                }
            })
        }))

There are multiple concepts I am demonstrating here. 我在这里演示了多个概念。 Firstly, you can create your dynamic inputs by creating your input in your server.R section. 首先,您可以通过在server.R部分中创建输入来创建动态输入。 This is then displayed by uiOutput . 然后由uiOutput显示。 Secondly, I always like to introduce people to validate . 其次,我总是喜欢介绍人们进行validate This is an important function to help troubleshoot or provide the user helpful error messages. 这是一项重要功能,可帮助排除故障或为用户提供有用的错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM