【问题标题】:Shiny: add upload widget after input type selectionShiny:在输入类型选择后添加上传小部件
【发布时间】:2017-10-17 01:47:57
【问题描述】:

我对闪亮还是很陌生。 我正在开发一个应用程序,它提供了上传 csv 文件或使用文本输入生成“csv”的选项(它实际上是内部的 data.table)。根据选择,我希望侧边栏面板扩展并加载上传小部件(或显示在主面板中的文本输入)

目前上传小部件会在应用加载时立即显示。 感谢您的帮助!

ui <- shinyUI(fluidPage(
  tagList(
    navbarPage( id = 'mynavlist', "My App",
      tabPanel("Create Boolean Gates",
           sidebarPanel(
             radioButtons("radio", label = p("Choose one option"),
                          choices = list("Upload Template" = 1, "Create Template" = 2), 
                          selected = 1),
             tags$hr(),

             ####only when selection is 'Upload Template'
             uiOutput("templ_upload"),
             tags$hr()

           ),
           mainPanel(
             #####only when upload was selected and after uploading the csv file
             tableOutput(outputId = 'table'),

             ####only when selection is 'Create Template'
             uiOutput("templ_create")
           )
  )
))))

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

  #### display upload widget if 'upload template' is chosen
  output$templ_upload <- renderUI({
    fileInput(inputId = 'templ_file', label = 'Choose a Template in csv 
       format')
    tags$hr()
    checkboxInput('header', 'Header', TRUE)
    radioButtons('sep', 'Separator', 
    c(Comma=',',Semicolon=';',Tab='\t'))
  })

  ####show the data after upload in mainpanel
  output$table <- renderTable({
    if (is.null(input$table)){
      h5("You have not uploaded a valid file")
    }else{
      template_csv <- fread(input$table$datapath, header=input$header, 
      sep=input$sep,quote=input$quote, check.names = FALSE)
      return(template_csv)
  }
 })

 ####to be finished
 #  output$templ_create <- renderUI({
 #  })
 })
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r file-upload shiny


    【解决方案1】:

    可以使用conditionalPanel(),但我认为在这种情况下更容易 在renderUI() 中指定这些条件:

    (如果您想从 renderUI() 传递多个 UI 元素,请不要忘记使用 tagList()

      output$templ_upload <- renderUI({
        if(input$radio == 1){
          tagList(
            fileInput(inputId = 'templ_file', label = 'Choose a Template in csv 
                      format'),
            tags$hr(),
            checkboxInput('header', 'Header', TRUE),
            radioButtons('sep', 'Separator', 
                         c(Comma=',',Semicolon=';',Tab='\t'))
          )
        }
      })
    
      output$templ_create <- renderUI({
        if(input$radio == 2){
          textInput("table", "Table", "Sample text")  
        }
      })
    

    【讨论】:

    • 在哪些条件下使用 conditionalPanel() 更合适?
    • 对于更复杂的例子,包括条件输出,..但我想说的是它的主要偏好,...
    猜你喜欢
    • 2020-06-27
    • 2014-10-07
    • 2016-12-30
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多