闪亮:在输入类型选择后添加上传小部件

时间:2017-05-17 14:21:34

标签: r file-upload shiny

我很擅长闪亮。 我正在开发一个应用程序,并且它可以选择上传csv文件或使用文本输入来生成' csv'(它实际上是一个data.table内部)。根据选择,我希望sidebarpanel扩展并加载上传小部件(或文本输入显示在主面板中)

目前,在加载应用程序时,上传窗口小部件会立即显示。 我感谢任何帮助!

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)

1 个答案:

答案 0 :(得分:1)

可以使用conditionalPanel(),但我认为在这种情况下更容易 在renderUI()

中指定这些条件

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

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