title
Downloading files
subtitle
author
Jeffrey Leek
job
Johns Hopkins Bloomberg School of Public Health
logo
bloomberg_shield.png
framework
io2012
highlighter
highlight.js
hitheme
tomorrow
url
lib
assets
../../libraries
../../assets
widgets
mode
selfcontained
Get/set your working directory
A basic component of working with data is knowing your working directory
The two main commands are getwd() and setwd().
Be aware of relative versus absolute paths
Relative - setwd("./data"), setwd("../")
Absolute - setwd("/Users/jtleek/data/")
Important difference in Windows setwd("C:\\Users\\Andrew\\Downloads")
Checking for and creating directories
file.exists("directoryName") will check to see if the directory exists
dir.create("directoryName") will create a directory if it doesn't exist
Here is an example checking for a "data" directory and creating it if it doesn't exist
if (! file.exists(" data" )) {
dir.create(" data" )
}
Getting data from the internet - download.file()
Downloads a file from the internet
Even if you could do this by hand, helps with reproducibility
Important parameters are url , destfile , method
Useful for downloading tab-delimited, csv, and other files
Example - Baltimore camera data
https://data.baltimorecity.gov/Transportation/Baltimore-Fixed-Speed-Cameras/dz54-2aru
Example - Baltimore camera data
https://data.baltimorecity.gov/Transportation/Baltimore-Fixed-Speed-Cameras/dz54-2aru
Download a file from the web
fileUrl <- " https://data.baltimorecity.gov/api/views/dz54-2aru/rows.csv?accessType=DOWNLOAD"
download.file(fileUrl , destfile = " ./data/cameras.csv" , method = " curl" )
list.files(" ./data" )
dateDownloaded <- date()
dateDownloaded
## [1] "Sun Jan 12 21:37:44 2014"
Some notes about download.file()
If the url starts with http you can use download.file()
If the url starts with https on Windows you may be ok
If the url starts with https on Mac you may need to set method="curl"
If the file is big, this might take a while
Be sure to record when you downloaded.