R/create_wkbk.R

Defines functions create_wkbk

Documented in create_wkbk

#' Creates an Excel workbook with worksheets.
#'
#' @param file filename of workbook to be created
#' @param df_list list of data frames to be added as worksheets to workbook
#' @param sheetnames character vector of worksheet names
#' @param create Specifies if the file should be created if it does not
#' already exist (default is FALSE). Note that create = TRUE has
#' no effect if the specified file exists, i.e. an existing file is
#' loaded and not being recreated if create = TRUE.
#' @import WriteXLS
#' @export
create_wkbk <- function(file, df_list, sheetnames, create = TRUE) {
  if (length(df_list) != length(sheetnames))
    stop("Number of dataframes does not match number of worksheet names")

  if (file.exists(file) & create)
    file.remove(file)

  WriteXLS(x = df_list, ExcelFileName = file, SheetNames = sheetnames,
           Encoding = "UTF-8", col.names = TRUE, AdjWidth = TRUE)
}
rmsharp/rmsutilityr documentation built on Feb. 13, 2024, 6:01 p.m.