r-file.copy:克服Windows长路径/文件名限制



我在R中有一个表,其中包含许多需要复制到目标文件夹的文件。这些文件分布在几十个文件夹中,每个文件夹下都有几个子文件夹。我已经成功地使用以下代码来查找所有文件及其位置:

(fastq_files <- list.files(Illumina_output, ".fastq.gz", recursive = TRUE, include.dirs = TRUE) %>% as_tibble)

在添加完整路径后,我有一个tibble,看起来像这样:

full_path>
Q:/IlluminaOutput/2019/091119 AB NGS/Data/Intensities/BaseCalls/19-15897-HLA-091119-AB-NGS_S1_L001_R1_001.fastq.gz
Q:/IlluminaOutput/2019/091119 AB NGS/Data/Intensities/BaseCalls/19-15236-HLA-091119-AB-NGS_S1_L001_R2_001.fastq.gz
Q:/IlluminaOutput/2018/0621818AB NGS/Data/Intensities/BaseCalls/18-06875-HLA-062818-NGS_S11_L001_R1_001.fastq.gz

假设file_and_path$filefile_and_path$path都是字符向量,并且Trusight_output是一个绝对路径,这就可以了:

f <- function(file, from, to) {
cwd <- setwd(from)
on.exit(setwd(cwd))
file.copy(file, to)
}
Map(f, file = file_and_path$file, from = file_and_path$path, to = Trusight_output)

我们在这里使用Map而不是lapply,因为我们正在应用一个多个参数的函数。FWIW,像这样的操作通常更适合PowerShell。

最新更新