R下载并解压缩文件以存储在数据框中 [英] R download and unzip file to store in data frame

查看:93
本文介绍了R下载并解压缩文件以存储在数据框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对发布在此处,但建议的解决方案对我不起作用。

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

我只想从github下载一个压缩文件,将其解压缩并存储数据在数据框架上(这是Coursera项目,但主要目的是创建Markdown文档,而不是下载/解压缩文件……因此,我不问如何做作业)。

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).

我的代码如下:

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?

推荐答案

问题不在于您的代码,而在于GitHub:它不支持直接下载部分存储库,即使使用原始访问URL,用于二进制文件。您的代码下载了一个文件,但不会解压缩。请参阅从GitHub下载单个文件以获取更详细的说明。

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.

例如,这可行:

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)

这篇关于R下载并解压缩文件以存储在数据框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆