如何以闪亮的方式遍历多个上传小部件?
Posted
技术标签:
【中文标题】如何以闪亮的方式遍历多个上传小部件?【英文标题】:How to loop through multiple upload widegets in shiny? 【发布时间】:2017-10-04 15:15:01 【问题描述】:我想创建多个 fileInput 函数来允许用户上传文件。我创建多个上传小部件的主要原因是因为我希望允许用户通过不同的路径上传。我在这里尝试完成的是遍历所有 fileInputs 并将所有文件保存到一个数据框中,但在我的代码示例中无法做到这一点。
library(shiny)
library(data.table)
library(DT)
n_attachments <- sprintf("file%s",seq(1:2))
ui <- fluidPage(
titlePanel('File download'),
sidebarLayout(
sidebarPanel(
textInput("LOAN_NUMBER", label = "Fannie Mae Loan Number", placeholder = "Please enter loan #")
, textInput("REO_ID", label = "REO Number", placeholder = "Please enter REO #")
, fileInput("file1", "Attachments1", accept = c("text/csv", "text/comma-separated-values,text/plain",".csv", ".pdf", ".doc", ".xlsx"), multiple = TRUE)
, fileInput("file2", "Attachments2", accept = c("text/csv", "text/comma-separated-values,text/plain",".csv", ".pdf", ".doc", ".xlsx"), multiple = TRUE)
, textOutput('text')
),
mainPanel(
DT::dataTableOutput("table"), tags$hr()
)
)
)
server <- function(input, output)
bin_data <- reactive(
attachement_data <- data.frame(ATTACHMENT = character(), FILENAME = character(), LOAN_NUMBER = character(), REO_ID = character())
for(x in n_attachments)
output$text <- renderText( input$x )
req(input$x)
# binary_data <- paste(readBin(input$file1$datapath, what="raw", n=1e6), collapse="-")
# attachment_info <- data.frame(ATTACHMENT = binary_data, FILENAME = paste0(input$file1$name))
# attachment_info
binary_data=list()
filenames=list()
for(i in 1:length(input$x[,1]))
binary_data[[i]] <- paste(readBin(input$x[[i, 'datapath']], what = "raw", n=1e6), collapse = "-")
filenames[[i]] <- input$x[[i, 'name']]
bin_data_frame <- data.frame(ATTACHMENT = as.character(unlist(binary_data)), FILENAME = as.character(unlist(filenames)))
bin_data_frame$LOAN_NUMBER <- input$LOAN_NUMBER
bin_data_frame$REO_ID <- input$REO_ID
attachement_data <- rbind(attachement_data, bin_data_frame)
save(attachement_data, file="attachement_data.RData")
attachement_data
)
output$table <- DT::renderDataTable(
bin_data()
)
shinyApp(ui = ui, server = server)
【问题讨论】:
忘记开头的代码我有 n_attachments 请使用新代码编辑和更新您的帖子。 【参考方案1】:好吧,我想我明白了,我必须使用 input[[x]] 而不是 input$x,并且我添加了几行来检查上传了多少文件输入。
server <- function(input, output)
bin_data <- reactive(
attachement_data <- data.frame(ATTACHMENT = character(), FILENAME = character(), LOAN_NUMBER = character(), REO_ID = character())
k <- 0
for(x in n_attachments)
if(!is.null(input[[x]]))
k = k + 1
for(x in n_attachments[0:k])
if(!is.null(input[[x]]))
output$text <- renderText( input[[x]] )
req(input[[x]])
# binary_data <- paste(readBin(input$file1$datapath, what="raw", n=1e6), collapse="-")
# attachment_info <- data.frame(ATTACHMENT = binary_data, FILENAME = paste0(input$file1$name))
# attachment_info
binary_data=list()
filenames=list()
for(i in 1:length(input[[x]][,1]))
binary_data[[i]] <- paste(readBin(input[[x]][[i, 'datapath']], what = "raw", n=1e6), collapse = "-")
filenames[[i]] <- input[[x]][[i, 'name']]
bin_data_frame <- data.frame(ATTACHMENT = as.character(unlist(binary_data)), FILENAME = as.character(unlist(filenames)))
bin_data_frame$LOAN_NUMBER <- input$LOAN_NUMBER
bin_data_frame$REO_ID <- input$REO_ID
attachement_data <- rbind(attachement_data, bin_data_frame)
save(attachement_data, file="attachement_data.RData")
attachement_data
)
output$table <- DT::renderDataTable(
bin_data()
)
shinyApp(ui = ui, server = server)
【讨论】:
以上是关于如何以闪亮的方式遍历多个上传小部件?的主要内容,如果未能解决你的问题,请参考以下文章