r基于选择的闪亮用户输入

时间:2015-03-05 01:16:25

标签: r shiny

我知道如何在用户界面上添加文字输入或单选按钮或日期输入,我想要做的是,询问用户是否要输入文字或日期范围?根据用户选择的内容显示文本输入或日期输入。不知道怎么做。

       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.

不知道该怎么做。需要一些指示。

1 个答案:

答案 0 :(得分:0)

我在这里感觉很慷慨,通常你应该提供你想要的代码,但这里有一个如何有条件输入的工作示例。

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")
                }
            })
        }))

我在这里展示了多个概念。首先,您可以通过在server.R部分创建输入来创建动态输入。然后由uiOutput显示。其次,我总是想向人们介绍validate。这是帮助排除故障或向用户提供有用的错误消息的重要功能。