【问题标题】:How to allow for dynamic amount of file inputs from user in R shiny?如何在 R Shiny 中允许来自用户的动态文件输入量?
【发布时间】:2015-12-16 18:04:50
【问题描述】:

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

【问题讨论】:

  • 您应该包含一个可重现的示例,说明您正在尝试实现的目标。
  • 一个最小的闪亮代码会很有用,但问题很清楚 IMO。
  • @MLavoie 我希望你不会觉得这个争论不休,但如果我有能力做到这一点,我就不需要寻求帮助了。
  • 不确定您的文件有多大,但我能想到的最快解决方案是将它们全部放在同一位置并使用多个参数。比拥有多个可变文件输入要干净得多...
  • @NBATrends 过去一直有效,尽管这个特定项目不允许这样做。

标签: r dynamic shiny


【解决方案1】:

好的,我希望你喜欢这个。想法来自这篇文章: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()
))

一些解释,shiny 通过 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>

【讨论】:

    猜你喜欢
    • 2019-02-05
    • 2018-11-28
    • 1970-01-01
    • 2014-04-05
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    相关资源
    最近更新 更多