简体   繁体   English

如何获取R中的tempfile中包含的文件名的向量?

[英]How to get a vector of the file names contained in a tempfile in R?

I am trying to automatically download a bunch of zipfiles using R. These files contain a wide variety of files, I only need to load one as a data.frame to post-process it. 我正在尝试使用R自动下载一堆zip文件。这些文件包含各种各样的文件,我只需要将其中一个作为data.frame加载即可对其进行后处理。 It has a unique name so I could catch it with str_detect() . 它具有唯一的名称,因此我可以使用str_detect()来捕获它。 However, using tempfile() , I cannot get a list of all files within it using list.files() . 但是,使用tempfile() ,我无法使用list.files()获得其中所有文件的列表。

This is what I've tried so far: 到目前为止,这是我尝试过的:

temp <- tempfile()

download.file("https://url/file.zip", destfile = temp) 

files <- list.files(temp) # this is where I only get "character(0)"

# After, I'd like to use something along the lines of:
data <- read.table(unz(temp, str_detect(files, "^file123.txt"), header = TRUE, sep = ";")

unlink(temp)

I know that the read.table() command probably won't work, but I think I'll be able to figure that out once I get a vector with the list of the files within temp . 我知道read.table()命令可能不起作用,但是我认为一旦获得包含tempfiles列表的向量,我就能弄清楚这一点。

I am on a Windows 7 machine and I am using R 3.6.0. 我在Windows 7计算机上,正在使用R 3.6.0。

Following what was said before, this structure should allow you to check the correct download with a temporary file structure : 按照前面所说的,此结构应允许您使用临时文件结构检查正确的下载:

temp <- tempfile("test.zip")
download.file("https://url/file.zip", destfile = temp) 
files <- list.files(temp)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM