if (FALSE) {
# Get information for most recent extract requests.
# By default gets the most recent 10 extracts
get_extract_history("usa")
# Return only the most recent 3 extract definitions
get_extract_history("cps", how_many = 3)
# To get the most recent extract (for instance, if you have forgotten its
# extract number), use `get_last_extract_info()`
get_last_extract_info("nhgis")
}
# To browse your extract history by particular criteria, you can
# loop through the extract objects. We'll create a sample list of 2 extracts:
extract1 <- define_extract_micro(
collection = "usa",
description = "2013 ACS",
samples = "us2013a",
variables = var_spec(
"SEX",
case_selections = "2",
data_quality_flags = TRUE
)
)
extract2 <- define_extract_micro(
collection = "usa",
description = "2014 ACS",
samples = "us2014a",
variables = list(
var_spec("RACE"),
var_spec(
"SEX",
case_selections = "1",
data_quality_flags = FALSE
)
)
)
extracts <- list(extract1, extract2)
# `purrr::keep()`` is particularly useful for filtering:
purrr::keep(extracts, ~ "RACE" %in% names(.x$variables))
purrr::keep(extracts, ~ grepl("2014 ACS", .x$description))
# You can also filter on variable-specific criteria
purrr::keep(extracts, ~ isTRUE(.x$variables[["SEX"]]$data_quality_flags))
# To filter based on all variables in an extract, you'll need to
# create a nested loop. For instance, to find all extracts that have
# any variables with data_quality_flags:
purrr::keep(
extracts,
function(extract) {
any(purrr::map_lgl(
names(extract$variables),
function(var) isTRUE(extract$variables[[var]]$data_quality_flags)
))
}
)
# To peruse your extract history without filtering, `purrr::map()` is more
# useful
purrr::map(extracts, ~ names(.x$variables))
purrr::map(extracts, ~ names(.x$samples))
purrr::map(extracts, ~ .x$variables[["RACE"]]$case_selections)
# Once you have identified a past extract, you can easily download or
# resubmit it
if (FALSE) {
extracts <- get_extract_history("nhgis")
extract <- purrr::keep(
extracts,
~ "CW3" %in% names(.x$time_series_tables)
)
download_extract(extract[[1]])
}
Run the code above in your browser using DataLab