R/nhs_year.R

Defines functions nhs_year_pc nhs_years_web

Documented in nhs_year_pc nhs_years_web

#' available Year in NHANES database
#' @param range whether to show year interval
#' @name nhs_year
#' @return available year, two years interval
#' @export
#'
#' @examples
#' nhs_years_web()
nhs_years_web <- function(range=TRUE){
    home_url = 'https://wwwn.cdc.gov/nchs/nhanes'
    html = xml2::read_html(home_url)
    years <- html |>
        rvest::html_nodes(xpath = '//div[@class="col-md-3 d-flex"]/a') |>
        rvest::html_text() |>
        do::Replace0(c('\t','\n','\r','NHANES'))
    if (!range) years <- do::Replace0(years,'-.*')
    years
}


#' @rdname nhs_year
#' @export
nhs_year_pc <- function(range=TRUE){
    get_config_path() |>
        list.files() |>
        stringr::str_extract('[0-9]{4}-[0-9]{4}') |>
        unique() |>
        do::increase()
}
yikeshu0611/nhanesR documentation built on Jan. 29, 2022, 6:08 a.m.