How to allow for dynamic amount of file inputs from user in R shiny?

孤人 提交于 2020-01-06 04:14:05

问题


I am looking to allow the user to enter the quantity of files they are interested in uploading, and then having the UI react to give that many file input buttons.

I am not certain how to do this. Even once this is completed, I am not certain how best to handle this dynamic amount of variables.

#ui.R

  shinyUI(fluidPage(
    titlePanel("Upload your files"),
    fluidRow(

      column(3, wellPanel(
        numericInput("user_num_2", 
                     label = h4("Number of Old Files"), 
                     value = 1)
      )),

      column(4, wellPanel(
        # This outputs the dynamic UI component
        uiOutput("ui1")
      ))
    ),
    fluidRow(

      column(3, wellPanel(
        numericInput("user_num_2", 
                     label = h4("Number of New Files"), 
                     value = 1)
      )),


      column(4, wellPanel(
        # This outputs the dynamic UI component
        uiOutput("ui2")
      ))
    ),
    fluidRow(

      column(3, wellPanel(

        selectInput("input_type", 
                    label = h4("Geography Assignment"), 
                    c("Zip", "County", "Zip-County", "Custom Assignment"
                    )
        )
      ))
    )
  ))

So a user can enter how many file upload buttons they want. The idea is to merge them all together. The multiple option would not work because these files could be in different locations.


回答1:


Ok I hope you like this. Idea came from this post: Shiny R renderPrint in loop usinf RenderUI only update the output

You can easily store the objects created from each of the file inputs in a list to be used for further analysis.

Server

shinyServer(function(input, output, session) {
  output$fileInputs=renderUI({
    html_ui = " "
    for (i in 1:input$nfiles){
      html_ui <- paste0(html_ui, fileInput(paste0("file",i), label=paste0("file",i)))
    }
    HTML(html_ui)
  })

})

UI

shinyUI(pageWithSidebar(
  headerPanel('Variable files'),
  sidebarPanel(
    numericInput("nfiles", "number of files", value = 2, min = 1, step = 1),
    uiOutput("fileInputs")
  ),
  mainPanel()
))

Some explanation, shiny operates through HTML. If you run the line of code below in the console you will see the logic behind creating the object html_ui that can hold a variable number of these HTML elements.

fileInput(paste0("file",1), label=paste0("file",1))

# <div class="form-group shiny-input-container">
#   <label>file1</label>
#   <input id="file1" name="file1" type="file"/>
#     <div id="file1_progress" class="progress progress-striped active shiny-file-input-progress">
#       <div class="progress-bar"></div>
#   </div>
# </div>


来源:https://stackoverflow.com/questions/34274538/how-to-allow-for-dynamic-amount-of-file-inputs-from-user-in-r-shiny

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!