add_labelled_sheet: Adds a sheet to an excel workbook with with both variable...

View source: R/add_labelled_sheet.R

add_labelled_sheetR Documentation

Adds a sheet to an excel workbook with with both variable names and labels.

Description

Row 1 contains variable labels; row 2 contains variable names, freeze panes, and filters. Header styling is applied. You can add additional styling options to sheet with the openxlsx::add functions after executed.

Usage

add_labelled_sheet(data, sheet_name = NULL, wrkbk, start_row = 1L)

Arguments

data

labelled data to add to sheet

sheet_name

optional sheet name; if none provided, sheet will be assigned name of input data set

wrkbk

expects a workbook object created from openxlsx::createWorkbook(), if not supplied by user, function will search for an object called 'wb' in the calling environment.

start_row

integer row position where the labels must be placed, data is placed at start_row+1, defaults to 1L

Value

a workbook object

Examples

## Not run: 
library(openxlsx)
library(croquet)
options("openxlsx.dateFormat" = "yyyy-mm-dd")

# example 1: single data frame ----
dat1 <- tibble::tibble(
  var_1 = 1:3,
  var_2 = LETTERS[1:3],
  var_3 = Sys.Date() - 0:2
  ) %>%
  labelled::set_variable_labels(
    var_1 = "Variable 1 (numeric)",
    var_2 = "Variable 2 (character)",
    var_3 = "Variable 3 (date)"
  )
wb <- createWorkbook()
add_labelled_sheet(dat1)
add_labelled_sheet(dat1, sheet_name = "example sheet")
saveWorkbook(wb, "checkwb.xlsx")


# example 2: list of data frames ----
dat2 <- tibble::tibble(
  var_1 = 4:6,
  var_2 = LETTERS[4:6],
  var_3 = Sys.Date() - 0:2
  ) %>%
  labelled::set_variable_labels(
    var_1 = "Variable 1 (numeric)",
    var_2 = "Variable 2 (character)",
    var_3 = "Variable 3 (date)"
  )

out <- tibble::lst(dat1, dat2)
wb <- createWorkbook()
add_labelled_sheet(out)
saveWorkbook(wb, "checkwb.xlsx")

## End(Not run)


pcctc/croquet documentation built on Aug. 10, 2024, 7:04 a.m.