如何在Shiny应用程序中基于一个小部件的选择显示一些其他小部件

3
以下是一个闪亮的应用程序,用于上传RDA或CSV文件。我需要的只是在选择输入中选择CSV文件类型时仅显示复选框输入和单选按钮小部件。当选择RDA时,不要显示这些小部件。
library(shiny)
library(DT)

##---------------------------------------------------------------
## ui
##---------------------------------------------------------------

ui <- fluidPage(

    sidebarLayout(
        sidebarPanel(

                # select a file type
                selectInput('filetype', label = h5(strong('Please select a file type')),
                            choices = c('rda', 'csv'),
                            selected = 'rda'),

                # Input: Select a file ----
                fileInput("file1", "Choose a file",
                          multiple = TRUE,
                          accept = c("text/csv",
                                     "text/comma-separated-values,text/plain",
                                     ".csv")),

                tags$hr(),

                # Input: Checkbox if file has header ----
                checkboxInput("header", "Header", TRUE),

                # Input: Select separator ----
                radioButtons("sep", "Separator",
                             choices = c(Comma = ",",
                                         Semicolon = ";",
                                         Tab = "\t"),
                             selected = ","),


                # Input: Select quotes ----
                radioButtons("quote", "Quote",
                             choices = c(None = "",
                                         "Double Quote" = '"',
                                         "Single Quote" = "'"),
                             selected = '"')

        ),


        #------------------------------Main Panel--------------------    
        mainPanel(

            DT::dataTableOutput("data.table")

        )
    )
)


##---------------------------------------------------------------------
# server 
##---------------------------------------------------------------------
options(shiny.maxRequestSize=30*1024^2) 

server <- function(input, output, session) {


    dt <- reactive ({

        if (input$filetype %in% 'rda') {
            load (input$file1$datapath)
            df  
        } else {
            read.csv(input$file1$datapath,
                     header = input$header,
                     sep = input$sep,
                     quote = input$quote)
        }

    })

    output$data.table <- DT::renderDataTable({
        req(input$file1)
        DT::datatable(dt(),
                      options = list(orderClasses = TRUE,
                    lengthMenu = c(5, 10, 20), pageLength = 5))
    })

}

runApp(shinyApp(ui=ui, server=server))

非常感谢有人能够帮助我。我不知道如何实现这一点。

1个回答

3
这里是如何构建动态UI的示例,只有在选择的文件类型是“csv”时才会显示单选按钮。更多信息请参考https://shiny.rstudio.com/articles/dynamic-ui.html
library(shiny)

ui <- fluidPage(
  selectInput(
    "select",
    label = "File Type",
    choices = list("csv", "rda"),
    selected = c("csv")
  ),

  conditionalPanel(
    condition = "input.select == 'csv'",
    radioButtons(
      "radio",
      label = "Separator",
      choices = list("commas", "tabs", "spaces")
    )
  )
)

server <- function(input, output, session) {

}

shinyApp(ui, server)

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