#' 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, ...)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.