如何在R闪亮中允许来自用户的动态文件输入量?

时间:2015-12-14 18:55:41

标签: r dynamic shiny

我希望允许用户输入他们有兴趣上传的文件数量,然后让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选项不起作用,因为这些文件可能位于不同的位置。

screen shot

1 个答案:

答案 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)
  })

})

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>