问题
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