Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when knitr has to download a zip file

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.

like image 618
Jonno Bourne Avatar asked Aug 16 '14 14:08

Jonno Bourne


Video Answer


1 Answers

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")
like image 195
Jonno Bourne Avatar answered Sep 22 '22 15:09

Jonno Bourne