如何在R Shiny中允许用户动态输入文件数量? [英] How to allow for dynamic amount of file inputs from user in R shiny?

查看:144
本文介绍了如何在R Shiny中允许用户动态输入文件数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望允许用户输入他们有兴趣上传的文件数量,然后让UI做出反应以提供许多文件输入按钮。

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"
                    )
        )
      ))
    )
  ))

所以用户可以输入所需的文件上传按钮数量。想法是将它们全部合并在一起。 multiple 选项不起作用,因为这些文件可能位于不同的位置。

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.

推荐答案

好的,我希望你喜欢这个。想法来自于这篇文章: Shiny R renderPrint in循环使用RenderUI仅更新输出

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.

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



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元素。

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>

这篇关于如何在R Shiny中允许用户动态输入文件数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆