Error when knitr has to download a zip file

穿精又带淫゛_ 提交于 2019-12-07 04:06:30

问题


Due to clash with the network security system the only way I cand download a data file from the internet to an area in the network where it can be used in R it is by downloading it through R itself. When I run the script in RStudio it works fine. When I try to knit the script I will get either the message Unsupported URL or I will get

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

below is the code that works under normal running but not during the knitting process.

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

If the file wasn't a zip I could download it using RCurl, but when I tried that R crashed, I have also tried method = "curl", setInternet2(TRUE), and trying to remove the s from https but none of them have worked.

The result is that I cannot produce a knitted document which is a problem. I have previous on a very similar problem ( a CSV file not a zipped CSV file, see link below) and have tried the advice but without success: R produces "unsupported URL scheme" error when getting data from https sites

I am using Windows 7 and RStudio

To repeat: this is ONLY a problem WHEN KNITTING the document, not when running the script.


回答1:


The problem was both the https and that the file is a binary. By changing the URL to http and setting file.download to mode="wb" the problem was resolved and the script could be knitted successfully.

Final code chunk is as follows

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


来源:https://stackoverflow.com/questions/25341285/error-when-knitr-has-to-download-a-zip-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!