R download and unzip file to store in data frame

末鹿安然 提交于 2020-01-13 03:10:15

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/32647779/r-download-and-unzip-file-to-store-in-data-frame

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