Skip to content

Instantly share code, notes, and snippets.

@jrnold
Created January 21, 2018 04:26
Show Gist options
  • Select an option

  • Save jrnold/a43bcc83424c841b53d17afbe81e1d1e to your computer and use it in GitHub Desktop.

Select an option

Save jrnold/a43bcc83424c841b53d17afbe81e1d1e to your computer and use it in GitHub Desktop.
Using file.exists to conditionally download a file - and multiple ways to access the files inside a zipfile in R
# Using file.exists to conditionally download a file - and multiple ways to access
# the files inside a g
library("tidyverse")
library("haven")
# name of output directory
OUTDIR <- file.path("data", "leaders")
# URL of the file
# file.path() is a "safe" way to create paths
URL <- "https://www.aeaweb.org/aej/mac/data/2008-0058_data.zip"
# Name of path it will be downloaded to
zipfile <- file.path(OUTDIR, basename(URL))
# basename returns the file or last directory of a path "/foo/bar.zip" -> "bar.zip"
# URLs are paths to files not on your computer, and basename also works on them.
# Use file.exists to only download the zipfile if it doesn't exist.
if (!file.exists(zipfile)) {
download.file(URL, zipfile)
}
# see that it exists !
list.files(OUTDIR)
# There's two things you can do with it
# Method 1 - unzip it all to you can access the files normally.
# This is what you are used to doing on your computer when you click on a
# zip file and unzip it.
# Use the `exdir` argument so it doesn't unzip in the project root directory
# WARNING: be careful - some zipfiles include all their files inside a directory
# so when you unzip them they are nicely organized in a directory. Others,
# don't, and when you unzip them they are all placed in the current directory.
# This is an example of the later.
unzip(zipfile, exdir = OUTDIR)
read_dta(file.path(OUTDIR, ""))
# Method 2 - we don't need to unzip the files first.
# we can access and extract individual files from the zipfile directly
# unzip(..., list = TRUE) will just list files in a zipfile
unzip(zipfile, list = TRUE)
# it returns the filenames as a character vector which you could reuse in code
# even though we won't here
# Use the unz file to get a file inside a zipfile
read_dta(unz(zipfile, filename = "mergeddata.dta"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment