【问题标题】:how to show some widgets based on the selection from one widget in shiny app如何根据闪亮应用程序中一个小部件的选择显示一些小部件
【发布时间】:2018-07-01 18:43:40
【问题描述】:

下面是一个闪亮的应用程序,用于上传 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))

如果有人可以帮助我,我真的很感激。我不知道如何实现这一目标。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    以下是如何构建动态 UI 的示例,该 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-14
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-23
      • 2022-01-08
      • 1970-01-01
      相关资源
      最近更新 更多