Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: lighthouse.codebook
Title: Summarize Datasets for Lighthouse Institute Projects
Version: 0.3.1
Version: 0.3.2
Authors@R: c(
person("Casey", "Sarapas",
email = "ccsarapas@chestnut.org",
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# lighthouse.codebook 0.3.2

## Fixed

* Fixed spacing between decked heads on grouped summary tabs (fixes #29).

## Internal

* Added `cb_create_spss_impl()` and `cb_write_impl()` for SPSS extension command interface / future-proofing.

# lighthouse.codebook 0.3.1

## Fixed
Expand Down
12 changes: 12 additions & 0 deletions R/cb_create_spss.r
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ cb_create_spss <- function(data,
.split_var_labels = NULL,
.options = cb_create_options()) {
check_options(.options)
cb_create_spss_impl(
data = data,
.user_missing = .user_missing,
.split_var_labels = !!rlang::enexpr(.split_var_labels),
.options = .options
)
}

cb_create_spss_impl <- function(data,
.user_missing = NULL,
.split_var_labels = NULL,
.options = cb_create_options()) {
data |>
cb_init() |>
cb_clean_fields_spss(
Expand Down
92 changes: 69 additions & 23 deletions R/cb_write.r
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,6 @@ cb_write <- function(cb,
overwrite = TRUE) {
check_codebook(cb)
detail_missing <- match.arg(detail_missing)
detail_missing <- detail_missing == "yes" || (
detail_missing == "if_any_user_missing" && length(attr(cb, "user_missing"))
)
summaries <- list(
num = cb_summarize_numeric_impl(cb),
cat = cb_summarize_categorical_impl(cb, detail_missing = detail_missing),
txt = cb_summarize_text_impl(
cb,
n_text_vals = n_text_vals,
detail_missing = detail_missing
)
)
group_by <- cb_untidyselect(cb, {{ group_by }})
group_rows <- cb_untidyselect(cb, {{ group_rows }})
if (missing(group_rows_numeric)) {
Expand All @@ -97,7 +85,48 @@ cb_write <- function(cb,
check_group_rows_arg(group_rows, group_by)
check_group_rows_arg(group_rows_numeric, group_by)
check_group_rows_arg(group_rows_categorical, group_by)

cb_write_impl(
cb = cb,
file = file,
dataset_name = dataset_name,
group_by = group_by,
group_rows = group_rows,
group_rows_numeric = group_rows_numeric,
group_rows_categorical = group_rows_categorical,
detail_missing = detail_missing,
n_text_vals = n_text_vals,
incl_date = incl_date,
incl_dims = incl_dims,
hyperlinks = hyperlinks,
overwrite = overwrite
)
}

cb_write_impl <- function(cb,
file,
dataset_name = NULL,
group_by = NULL,
group_rows = NULL,
group_rows_numeric = group_rows,
group_rows_categorical = group_rows,
detail_missing = c("if_any_user_missing", "yes", "no"),
n_text_vals = 5,
incl_date = TRUE,
incl_dims = TRUE,
hyperlinks = TRUE,
overwrite = TRUE) {
detail_missing <- detail_missing == "yes" || (
detail_missing == "if_any_user_missing" && length(attr(cb, "user_missing"))
)
summaries <- list(
num = cb_summarize_numeric_impl(cb),
cat = cb_summarize_categorical_impl(cb, detail_missing = detail_missing),
txt = cb_summarize_text_impl(
cb,
n_text_vals = n_text_vals,
detail_missing = detail_missing
)
)
if (!is.null(group_by)) {
summaries$num_grp <- cb_summarize_numeric_impl(
cb,
Expand Down Expand Up @@ -393,6 +422,27 @@ var_name_hyperlinks <- function(params) {
params
}

wb_set_col_auto_min_max <- function(wb,
sheet = openxlsx2::current_sheet(),
cols,
min_width = 7,
max_width = 70,
hidden = FALSE) {
# note `wb_set_col_auto_min_max()` must be used before setting any column
# widths outside [min_width, max_width]. otherwise those columns will be reset
# to within [min_width, max_width].

opts <- options(
openxlsx2.minWidth = min_width,
openxlsx2.maxWidth = max_width
)
on.exit(options(opts))

openxlsx2::wb_set_col_widths(
wb, sheet = sheet, cols = cols, widths = "auto", hidden = hidden
)
}

cb_prep_sheet_data <- function(data,
sheet_name = NULL,
header = NULL,
Expand Down Expand Up @@ -434,6 +484,9 @@ cb_prep_sheet_data <- function(data,
num = which(data_nms %in% cols_num),
pct = which(data_nms %in% cols_pct),
int = which(data_nms %in% cols_int),
spacer = which(
data_nms == "" & vapply(data, \(x) all(is.na(x)), logical(1))
),
border = lapply(
rows_border_by,
\(rbb) compute_border_cols(data, cols = all, start_col = rbb)
Expand Down Expand Up @@ -634,7 +687,8 @@ cb_write_sheet <- function(wb, data, params) {

# Set column widths and freeze panes
wb <- wb |>
openxlsx2::wb_set_col_widths(cols = pm$cols$all, widths = "auto") |>
wb_set_col_auto_min_max(cols = setdiff(pm$cols$all, pm$cols$spacer)) |>
openxlsx2::wb_set_col_widths(cols = pm$cols$spacer, widths = 2.6) |>
openxlsx2::wb_freeze_pane(
first_active_row = pm$rows$dat_start + 1, first_active_col = 2
)
Expand Down Expand Up @@ -667,9 +721,7 @@ cb_write_codebook <- function(cb,
incl_date = TRUE,
incl_dims = TRUE,
hyperlinks = TRUE,
overwrite = TRUE,
min_col_width = 7,
max_col_width = 70) {
overwrite = TRUE) {
# create headers
cb_name <- cb_dims <- cb_date <- NULL
if (!is.null(dataset_name)) cb_name <- glue_chr("Dataset: {dataset_name}")
Expand Down Expand Up @@ -802,12 +854,6 @@ cb_write_codebook <- function(cb,
}

if (hyperlinks) params <- var_name_hyperlinks(params)

opts <- options(
openxlsx2.minWidth = min_col_width,
openxlsx2.maxWidth = max_col_width
)
on.exit(options(opts))

# initialize workbook
wb <- openxlsx2::wb_workbook()
Expand Down