2013-01-03 96 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

相關問題