我希望允许用户输入他们有兴趣上传的文件数量,然后让UI做出反应以提供那么多文件输入按钮。
我不确定如何做到这一点。即使一旦完成,我也不确定如何最好地处理这些动态数量的变量。
#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"
)
)
))
)
))
因此用户可以输入他们想要的文件上传按钮数量。我们的想法是将它们合并在一起。 multiple
选项不起作用,因为这些文件可能位于不同的位置。
答案 0 :(得分:0)
好的,我希望你喜欢这个。想法来自这篇文章:Shiny R renderPrint in loop usinf RenderUI only update the output
您可以轻松地将从每个文件输入创建的对象存储在列表中,以用于进一步分析。
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)
})
})
shinyUI(pageWithSidebar(
headerPanel('Variable files'),
sidebarPanel(
numericInput("nfiles", "number of files", value = 2, min = 1, step = 1),
uiOutput("fileInputs")
),
mainPanel()
))
一些解释,闪亮的操作通过HTML。如果您在控制台中运行下面的代码行,您将看到创建对象html_ui
背后的逻辑,该对象可以包含可变数量的这些HTML元素。
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>