当下载链接以“/download”结尾时,将 zip 文件下载到 R

吉莱斯皮

我的问题与此帖子类似,但解决方案建议似乎不适用。

我有很多压缩数据存储在在线服务器 (B2Drop) 上,它提供了一个带有扩展名“/download”而不是“.zip”的下载链接。我一直无法使用此处描述的方法来工作。

我已经创建了一个测试下载页面https://b2drop.eudat.eu/s/K9sPPjWz3jxtXEq,其中下载链接https://b2drop.eudat.eu/s/K9sPPjWz3jxtXEq/download可以通过右键单击下载按钮获得。这是我的脚本:

temp <- tempfile()
download.file("https://b2drop.eudat.eu/s/K9sPPjWz3jxtXEq/download",temp, mode="wb")
data <- read.table(unz(temp, "Test_file1.csv"))
unlink(temp)

当我运行它时,我收到错误:

> download.file("https://b2drop.eudat.eu/s/K9sPPjWz3jxtXEq/download",temp, mode="wb")
trying URL 'https://b2drop.eudat.eu/s/K9sPPjWz3jxtXEq/download'
Content type 'application/zip' length 558 bytes
downloaded 558 bytes

> data <- read.table(unz(temp, "Test_file1.csv"))
Error in open.connection(file, "rt") : cannot open the connection
In addition: Warning message:
In open.connection(file, "rt") :
  cannot locate file 'Test_file1.csv' in zip file 'C:\Users\User_name\AppData\Local\Temp\RtmpMZ6gXi\file3e881b1f230e'

这通常表明 R 正在查找文件的工作目录存在问题。在这种情况下,这应该是 temp wd。

有没有人遇到过这个问题并找到了解决方案?提前致谢

r2evans

你的内部路径是错误的。您可以使用list=TRUE来列出存档中的文件,类似于命令行实用程序的-l参数。

unzip(temp, list=TRUE)
#                  Name Length                Date
# 1 Test/Test_file1.csv    256 2021-09-27 10:13:00
# 2 Test/Test_file2.csv    286 2021-09-27 10:14:00

read.table但是,使用要好read.csv因为它是逗号分隔的。

data <- read.csv(unz(temp, "Test/Test_file1.csv"))
head(data, 3)
#   ID Variable1 Variable2 Variable Variable3
# 1  1         f     54654       25        t1
# 2  2         t       421       64        t2
# 3  3         x      4521       85        t3

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章