当下载 link 以 '/download' 结尾时将 zip 文件下载到 R

Download zip file to R when download link ends in '/download'

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

我有很多压缩数据存储在在线服务器 (B2Drop) 上,该服务器提供扩展名为 "/download" instead of ".zip" 的下载 link。我无法使 here 描述的方法起作用。

我已经创建了一个测试下载页面https://b2drop.eudat.eu/s/K9sPPjWz3jxtXEq, where the download link 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 正在查找文件的工作目录存在问题。在这种情况下,应该是温度 wd.

你的内部路径是错误的。您可以使用 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