我需要自动化R来读取一个zip文件中的csv数据文件.
例如,我会输入:
read.zip(file = "myfile.zip")
Run Code Online (Sandbox Code Playgroud)
在内部,将要做的是:
myfile.zip到临时文件夹read.csv如果zip文件中有多个文件,则会引发错误.
我的问题是获取包含在zip文件中的文件的名称,在orded中提供它来执行read.csv命令.有谁知道怎么做?
UPDATE
这是我根据@Paul答案写的函数:
read.zip <- function(zipfile, row.names=NULL, dec=".") {
# Create a name for the dir where we'll unzip
zipdir <- tempfile()
# Create the dir using that name
dir.create(zipdir)
# Unzip the file into the dir
unzip(zipfile, exdir=zipdir)
# Get the files into the dir
files <- list.files(zipdir)
# Throw an error if there's more than one
if(length(files)>1) stop("More than one data file inside zip") …Run Code Online (Sandbox Code Playgroud)