Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R exdir does not exist error

Tags:

r

unzip

rstudio

I'm trying to download and extract a zip file using R. Whenever I do so I get the error message

Error in unzip(temp, list = TRUE) : 'exdir' does not exist

I'm using code based on the Stack Overflow question Using R to download zipped data file, extract, and import data

To give a simplified example:

# Create a temporary file
temp <- tempfile()

# Download ZIP archive into temporary file
download.file("http://cran.r-project.org/bin/windows/contrib/r-release/ggmap_2.2.zip",temp)

# ZIP is downloaded successfully:

# trying URL 'http://cran.r-project.org/bin/windows/contrib/r-release/ggmap_2.2.zip'
# Content type 'application/zip' length 4533970 bytes (4.3 Mb)
# opened URL
# downloaded 4.3 Mb

# Try to do something with the downloaded file
unzip(temp,list=TRUE)

# Error in unzip(temp, list = TRUE) : 'exdir' does not exist

What I've tried so far:

  • Accessing the temp file manually and unzipping it with 7zip: Can do this no problem, file is there and accessible.
  • Changing the temp directory to c:\temp. Again, the file is downloaded successfully, I can access it and unzip it with 7zip but R throws the exdir error message when it tries to access it.

R version 2.15.2

R-Studio version 0.97.306

Edit: The code works if I use unz instead of unzip but I haven't been able to figure out why one works and the other doesn't. From CRAN guidance:

  • unz reads (only) single files within zip files...
  • unzip extracts files from or list a zip archive
like image 968
Tumbledown Avatar asked Mar 05 '13 14:03

Tumbledown


2 Answers

This can manifest another way, and the documentation doesn't make clear the cause. Your exdir cannot end in a "/", it must be just the name of the target folder.

For example, this was failing with 'exdir' does not exist:

unzip(temp, overwrite = F, exdir = "data_raw/system-data/")

And this worked fine:

unzip(temp, overwrite = F, exdir = "data_raw/system-data")

Presumably when unzip sees the "/" at the end of the exdir path it keeps looking; whereas omitting the "/" tells unzip "you've found it, unzip here".

like image 120
worksong Avatar answered Nov 20 '22 23:11

worksong


On a windows setup: I had this error when I had exdir specified as a path. For me the solution was removing the trailing / or \\ in the path name.

Here's an example and it did create the new folder if it didn't already exist

locFile <- pathOfMyZipFile
outPath <- "Y:/Folders/MyFolder"
# OR
outPath <- "Y:\\Folders\\MyFolder"

unzip(locFile, exdir=outPath)
like image 35
jNorris Avatar answered Nov 20 '22 21:11

jNorris