R/make_header_template.R

Defines functions make_header_template

Documented in make_header_template

#' Creates a header file template for header recoding
#'
#' @param x a tbl() to from which to create the template
#' @param path the path to save the template into
#' @param ... additional variables passed to openxlsx::write.xlsx or to readr::write_excel_csv
#'
#' @examples
#' mtcars %>% make_header_template(path = "mtcar_headers.csv")
#' mtcars %>% make_header_template(path = "mtcar_headers.xlsx")
#'
#' @importFrom magrittr %>%
#'
#' @export
make_header_template <- function(x, path, ...){

  create_template <- tibble::tibble(old_name = names(x),
                                    new_name = names(janitor::clean_names(x, case = "snake")))

  is_csv <- stringr::str_sub(path, start = -3) == "csv"
  is_xlsx <- stringr::str_sub(path, start = -4) == "xlsx"

  if (!is_csv & !is_xlsx) {
    warning("Could not verify desired output from file name (should end with .csv or .xlsx)\n
            Continuing with xlsx output.")
    path <- paste0(path, ".xlsx")
  }

  if (is_csv) {
    readr::write_excel_csv(create_template, path, ...)
  } else {
    openxlsx::write.xlsx(create_template, path, ...)
  }

}
sarid-ins/saridr documentation built on Nov. 10, 2020, 9:07 p.m.