setup_nhanes: Set up directories

Description Usage Arguments Value Examples

View source: R/download_files.R

Description

This function sets up the directories and subdirectories on your computer for subsequent downloading by other functions. This is the basic setup piece to facilitate the entire download and extraction process. The returned object governs the rest of the process.

Usage

1
setup_nhanes(data_dir = NULL, yr = 2011)

Arguments

data_dir

This is directory on computer in which subdirectories will be made for all nhanes files (can end in "/" on Mac but CANNOT on Windows). This will be created as a temporary directory if it doesn't exist. Use data_dir = "." to use the current working directory.

yr

This is the first year of the NHANES wave of interest (always odd, starting in 1999 and ending in 2011)

Value

A list is returned with 4 items: the url to download data from, the url to download death data from, the target directory into which subdirectories should be placed for the NHANES wave, and the years of the wave to be downloaded.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
## Not run: 
# Basic example of function
n <- setup_nhanes(data_dir = "./data", yr = 2011)

## End(Not run)

## Not run: 
# Example of entire workflow
# Get entire NHANES directory and read into subdirectory as .rds objects
# Note:  may work better doing this one or two waves at a time
# Can use waves[1:7] in outer for loop to choose waves to load
# Similarly can use filenames[1:20] in inner for loop to choose filenames to load
waves <- seq(1999, 2011, 2)
for(wave in waves){
    message("Starting wave: ", wave)
    n <- setup_nhanes(data_dir = "./data", yr = wave)
    filenames <- get_nhanes_filenames(n)
    for(file in filenames) {
        download_nhanes(file, n)
    }
    message("Finished wave: ", wave)
}

# Example of parallel download process
# Not quite twice as fast (on my computer)
# Returns a list of completed files at the end.  Set console = FALSE in above functions.
# Need to use foreach syntax for nested loops to redo above in completely parallel fashion
library(foreach)
library(doMC) # use library(doSNOW) on Windows
registerDoMC(cores = 4) # set number of cores for your computer
foreach(file = filenames, .packages = c("foreign", "downloader"), .combine = rbind) %dopar% {
    download_nhanes(file, n, console = FALSE)
}

## End(Not run)

outcomesinsights/nhanes.tools documentation built on May 24, 2019, 5:54 p.m.