Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R download and unzip file to store in data frame

I have a similar question to the one posted in here but the solution suggested doesn't work for me.

I want to simply download a zipped file from github, unzip it and store the data on a data frame (it is a Coursera project but the main purpose is to create a Markdown doc and not to download/unzip the file...so I am not asking how to do my homework).

My code is the following:

activity_url <- "https://github.com/rdpeng/RepData_PeerAssessment1/blob/master/activity.zip"
temp <- tempfile()
download.file(activity_url, temp, method = "libcurl", mode = "wb")
unzip(temp, "activity.csv")
mydata <- read.table("activity.csv", header = "TRUE", sep = ",")
unlink(temp)

I believe the error occurs at the moment of unzipping the file. The error I get is the following:

Error in file(file, "rt") : impossible d'ouvrir la connexion
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> read.table -> file
Exécution arrêtée

Does anyone has an hint on where the error is?

like image 947
atalantafugiens Avatar asked Sep 18 '15 08:09

atalantafugiens


People also ask

How do I load a zip file in R?

To read a zip file and extract data from it to R environment, we can use the download. file() to download the zip, then unzip() allows to unzip the same and extract files using read. csv().


1 Answers

The problem is not with your code, but rather with GitHub: it does not support direct download of parts of repositories, even with the "raw" access URL, for binary files. Your code downloads a file, but it will not unzip. See Download single files from GitHub for a more detailed explanation.

So for instance this works:

activity_url <- "http://kenbenoit.net/files/activity.zip"
temp <- tempfile()
download.file(activity_url, temp)
unzip(temp, "activity.csv")
# note that here I modified your original read.table() which did not work
mydata <- read.csv("activity.csv")
unlink(temp)
like image 150
Ken Benoit Avatar answered Oct 26 '22 22:10

Ken Benoit