R 从shiny应用程序中的文件名中提取文本,并在写回本地计算机之前添加到数据框中

R 从shiny应用程序中的文件名中提取文本,并在写回本地计算机之前添加到数据框中,r,regex,string,shiny,shinyapps,R,Regex,String,Shiny,Shinyapps,我希望能够将我在工作流程中使用的纸条转换为shinyapp,供其他非用户使用。我对shiny和Rstudio都是新手。我能够让基本功能正常工作,但我正在努力提取特定字符串,将它们添加为id列,就像我在源代码脚本中所做的那样 这是我希望在我的闪亮应用程序中实现的功能 ##### function to read tab-separated flat file(s) while adding a string from the filename to a column in the table my

我希望能够将我在工作流程中使用的纸条转换为shinyapp,供其他非用户使用。我对shiny和Rstudio都是新手。我能够让基本功能正常工作,但我正在努力提取特定字符串,将它们添加为id列,就像我在源代码脚本中所做的那样

这是我希望在我的闪亮应用程序中实现的功能

##### function to read tab-separated flat file(s) while adding a string from the filename to a column in the table
my_read_tsv <- function(x) {
  out <- read_tsv(x, skip = 4) ## skips for header lines
  id_code <-
    paste(
      str_extract(x, "[0-9]{1,6}), 
      str_extract(x, "[A-Za-z0-9]{1,3}-[A-Za-z0-9]{1,2}_[A-Za-z0-9]{1,3}")
    )
  cbind(ID = id_code, out)
}

是否有方法查看通过选择文件生成的列表?我假设问题是由于列表不是允许通过正则表达式提取的格式造成的?我可以使用browser()找到问题的根源。文件路径存储在tmp位置,并丢失正则表达式提取该路径所需的文件名。上传时是否可以强制文件名保持与我的计算机上的文件名相同?通过调试解决。。。。需要引用名称列而不是数据路径。infle$datapath在更改为infle$name时起作用
tbl <- lapply(desiredlist, my_read_tsv) %>% bind_rows()
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)

ui <- fluidPage(
    fluidPage(
        titlePanel("Combine files"),
        sidebarLayout(
            sidebarPanel(
                fileInput("fileinput",
                          "Choose files from a directory",
                          multiple = TRUE,
                          accept=c('text/csv', 
                                   'text/comma-separated-values,text/plain', 
                                   '.csv')),
                downloadButton('downloadData', 'Download')
            ),
            mainPanel(
                tableOutput('contents')
            )
        )
    )
)

library(shiny)
library(dplyr)
library(readr)
library(tidyr)
library(stringr)

options(shiny.maxRequestSize = 200*1024^2) ## scales file size above 5mb limit
server <-  function(input, output) {
    getData <- reactive({
        inFile <- input$fileinput
        if (is.null(inFile)){
            return(NULL)
        }else {  
            csvfiles <- lapply(inFile$datapath, function(y){
                    out <- read_tsv(y, skip = 4)
                    id_code <-
                        paste(
                            str_extract(y,"WR[0-9]{1,6}"),
                            str_extract(y, "[A-Za-z0-9]{1,3}-[A-Za-z0-9]{1,2}_[A-Za-z0-9]{1,3}")
                        )
                    cbind(ID = id_code, out)
                }
            )
            do.call(bind_rows, csvfiles)
        }
    })
    output$contents <- renderTable( 
        getData() 
    )
    output$downloadData <- downloadHandler(
        filename = function() { 
            paste("combined_file", ".csv", sep="")
        },
        content = function(file) { 
            write.csv(getData(), file, row.names=FALSE)   
        })
}

shinyApp(ui = ui, server = server)
EX:
header
header
header 
header
row.names 
1   2    3    4    5    6
1   2    3    4    5    6