基于选择的r shiny用户输入

3

我知道如何在UI上添加文本输入框、单选按钮或日期输入框,现在我想要的是询问用户,他想输入文本还是日期范围?根据用户的选择,显示文本输入框或日期输入框。不确定如何实现。

       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个回答

1

我在这里很慷慨,一般来说你应该提供你尝试过的代码,但这里有一个工作示例,展示如何拥有条件输入。

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函数。这是一个重要的函数,可以帮助排除故障或提供用户有用的错误消息。


我正在添加一些操作按钮和其他功能,一旦它们可用,我将尽快更新此部分 :) - Ty Voss

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接