2013-01-03 178 views
2

我想提取一些存储在Dropbox(在一个文件夹中)的函数。在Dropbox中提取Dropbox文件夹

这一切都很好,直到我试图解开文件。这里有一个例子:

library("R.utils") 
temp <- tempfile() 
temp<-paste(temp,".gz",sep="") 
download.file("http://www.dropbox.com/sh/dzgrfdd18dljpj5/OyoTBMj8-v?dl=1",temp) 
untar(temp,compressed="gzip",exdir=dirname(temp)) 

在这里,我得到一个错误:

Error in rawToChar(block[seq_len(ns)]) : 
    embedded nul in string: 'PK\003\004\024\0\b\b\b.... 

理想我想,然后加载在文件夹中的所有功能,像这样:

sourceDirectory(dirname(temp)) 

...但我需要能够先解开它们。我可以打开Windows中的档案,但在R我得到上述错误。谁能帮忙?我试过使用unzip,但这只适用于从Dropbox下载的较小文件夹(比如上面的文件夹),较大的文件夹只能用作gzip格式(至少在我的经验中)。

+0

您的链接似乎指向一个ZIP文件。也许这就是为什么'untar'不能工作。 –

回答

3
# use the httr package 
library(httr) 

# define your desired file 
u <- "http://www.dropbox.com/sh/dzgrfdd18dljpj5/OyoTBMj8-v?dl=1" 

# save the file to a .zip 
tf <- paste0(tempfile() , '.zip') 

# create a temporary directory 
td <- tempdir() 

# get the file 
fc <- GET(u) 

# write the content of the download to a binary file 
writeBin(content(fc, "raw"), tf) 

# unzip it. 
unzip(tf , exdir = td) 

# locate all files in this directory 
af <- list.files(td , recursive = TRUE) 

# subset the files to the ones ending with R 
R.files <- af[ substr(af , nchar(af) , nchar(af)) == 'R' ] 

# set your working directory 
setwd(td) 

# source 'em 
for (i in R.files) source(i) 

# see that they're loaded 
ls() 
+0

安东尼,再次感谢。 – maycobra

-1

也许你必须使用选项mode='wb'作为download.file。

+4

不幸的是,它没有任何区别。或许你的意思是什么?当你使用'wb'时它对你有用吗?该文件下载正常,这似乎给我的问题untar。 – maycobra