Skip to contents

This function creates a new Excel workbook or uses an existing template as a base, adding multiple data frames as separate worksheets and Excel tables. The function offers flexibility in sheet naming, consolidated documentation, and template usage.

Usage

write_nice_xlsx(
  output_path,
  data = NULL,
  documentation = NULL,
  template_path = NULL,
  as_tables = TRUE,
  overwrite = FALSE,
  auto_width = FALSE
)

Arguments

output_path

Character string specifying the path where the new Excel file should be saved.

data

A single data frame (will create a sheet named "data"), an unnamed list of data frames (will create sheets named "data 1", "data 2", etc.), or a named list of data frames (sheets will be named after the list elements). If NULL, no data sheets will be added.

documentation

A single data frame or a list of data frames for documentation. All documentation tables will be added to a single "DOKUMENTACE" sheet, separated by empty rows. If a list is provided and named, the names will be used as section titles. If NULL, no documentation sheet will be added.

template_path

Optional path to an existing Excel file to use as a template. If provided, the function will copy the template and add new data to the copy. The original template file is never modified.

as_tables

Logical, whether to format the data frames as Excel tables (default: TRUE).

overwrite

Logical, whether to overwrite the output file if it exists (default: FALSE).

auto_width

Logical, whether to automatically set the width of all columns (default: FALSE).

Value

Output path, invisibly.

Examples

if (FALSE) { # \dontrun{
# Single data frame - creates "data" sheet
create_excel_workbook(
  output_path = "output/single_data.xlsx",
  data_frames = my_df
)

# Unnamed list of data frames - creates "data 1", "data 2", etc.
create_excel_workbook(
  output_path = "output/multiple_data.xlsx",
  data_frames = list(monthly_df, annual_df)
)

# Named list of data frames - uses custom names
create_excel_workbook(
  output_path = "output/results.xlsx",
  data_frames = list(
    "Monthly Data" = monthly_df,
    "Annual Summary" = annual_df
  )
)

# Single documentation data frame
create_excel_workbook(
  output_path = "output/with_single_doc.xlsx",
  data_frames = my_data,
  doc_tables = guide_df
)

# Using with template and named documentation tables
create_excel_workbook(
  output_path = "output/detailed_report.xlsx",
  data_frames = list(
    "Raw Data" = raw_df,
    "Processed Data" = processed_df
  ),
  doc_tables = list(
    "Guide" = guide_df,
    "Codebook" = codebook_df
  ),
  template_path = "templates/report_template.xlsx"
)
} # }