Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dropbox folder extraction in R

Tags:

r

tar

I'm trying to extract some functions stored on dropbox (in a folder).

It all goes well until I try to untar the file. Here's an example:

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

Here I get an error:

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

Ideally I would then load all the functions in folder like this:

sourceDirectory(dirname(temp))

...but I need to be able to untar them first. I can open the archive in Windows but in R I get the error above. Can anyone help? I've tried to use unzip but this only works with smaller folders downloaded from dropbox (such as the one above), bigger ones only work as gzip format (at least in my experience).

like image 361
maycobra Avatar asked Feb 27 '26 12:02

maycobra


1 Answers

# 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()
like image 139
Anthony Damico Avatar answered Mar 02 '26 05:03

Anthony Damico



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!