当knitr需要下载zip文件时出错

5

由于与网络安全系统冲突,我只能通过R本身下载数据文件到网络中可用于R的区域。当我在RStudio中运行脚本时,它可以正常工作。但是当我尝试编织脚本时,我会收到"Unsupported URL"消息。

Error in file(file, "rt") : cannot open the connection 
Calls: <Anonymous> ... withVisible -> eval -> eval -> read.csv -> 
    read.table -> file
Execution halted

以下是在正常运行时可以工作但在编织过程中无法工作的代码。

url <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip"
download.file(url, "repdata-data-activity.zip")
unzip("repdata-data-activity.zip")

如果文件不是zip格式,我可以使用RCurl下载,但是当我尝试这样做时,R崩溃了。我也尝试了 method = "curl",setInternet2(TRUE),并尝试从https中删除s,但都没有成功。
结果是我无法生成一个正常的文档,这是个问题。我之前遇到过非常类似的问题(CSV文件而不是压缩的CSV文件,请参见下面的链接),并尝试了建议,但没有成功: R produces "unsupported URL scheme" error when getting data from https sites 我正在使用Windows 7和RStudio。
重申一下:只有在编织文档时才会出现这个问题,运行脚本时没有问题。
1个回答

5
问题在于https以及文件是二进制的。通过将URL更改为http并将file.download设置为mode="wb",问题得到解决,脚本可以成功地编织。
最终代码块如下:
url <- "http://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip"
download.file(url, "repdata-data-activity.zip", mode="wb")
unzip("repdata-data-activity.zip")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接