R/get_documentation.R

Defines functions file_open get_documentation

Documented in get_documentation

#' Downloads a Microsoft Word file containing the data documentation for a given dataset
#'
#' @inheritParams get_data
#'
#' @export

get_documentation <- function(name) {
  link <- documentation[
            documentation$TABLE_NAME == name,
            "Documentation",
            drop = TRUE
          ]

  if (grepl("lunch_program", name)) {
    link <- "https://nces.ed.gov/ccd/xls/2019-20_School_Lunch_Companion.xlsx"
  }
  if (.Platform$OS.type != "unix") {
    message(link)
    return(link)
  }
  ext <- substr(link, nchar(link) - 4, nchar(link))
  file <- paste0(name, ext)
  file_path <- file.path(getwd(),"data-documentation", file)

  if (file.exists(file_path)) {
    file_open(file_path)
    return(message("Opening documentation file ", file))
  }

  if (!file.exists("data-documentation")) {
    dir.create(file.path(getwd(), "data-documentation"))
  }

  download.file(link, file_path)
  file_open(file_path)
}

file_open <- function(file) {
  system(paste("open", file))
}
datalorax/edld652 documentation built on Jan. 28, 2022, 12:17 p.m.