Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
58 changes: 58 additions & 0 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,61 @@
)
return(rset)
}


# Schema helper functions for workbook creation --------------------------------

col_pos <- function(schema, col_name) {

@Damonamajor Damonamajor Aug 17, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only pulled the shared helpers into this file (API and workbook) in model_res_avm. This means 2 out of the 4 small functions. But, then I noticed when doing this version that we may not even want to put them in the helper since nothing is shared (since we don't have the API workbook). Do you have any preference in how we want these 4 small functions stored between both repositories?
Just let me know if you want changes to this or the other one.
ccao-data/model-res-avm#538 (comment)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion, optional] I don't have strong feelings! I think it makes sense in the res model to define the functions that are specific to one script in the body of that script, and factor out shared functions in helpers.R; for the condo model, I think it makes sense to just match the organization that the res model uses, even though functions are not shared in the same way -- I find it useful for the organization of the two projects to be as close as possible, for the sake of making it easier to find things. So I would just copy the way the res model organizes these functions, even though the logic of the organization is less apparent in this context.

idx <- which(names(schema) == col_name)
if (length(idx) == 0) stop(glue::glue("Column '{col_name}' not found in schema"))

Check warning on line 327 in R/helpers.R

View workflow job for this annotation

GitHub Actions / pre-commit

file=/home/runner/work/model-condo-avm/model-condo-avm/R/helpers.R,line=327,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 83 characters.
idx
}

cols_with_style <- function(schema, style_name) {
which(vapply(schema, function(x) identical(x$style, style_name), logical(1)))
}

cols_with_cond <- function(schema, cond_name) {
which(vapply(schema, function(x) identical(x$cond, cond_name), logical(1)))
}

cols_hidden <- function(schema) {
which(vapply(schema, function(x) isTRUE(x$hidden), logical(1)))
}

validate_schema_vs_template <- function(schema, template_path, sheet_name,
header_row) {
normalize <- function(x) gsub("\\s+", " ", trimws(x))
wb_tmpl <- loadWorkbook(template_path)
tmpl_df <- readWorkbook(
wb_tmpl,
sheet = sheet_name,
colNames = FALSE,
skipEmptyRows = FALSE
)
tmpl_headers <- normalize(as.character(unlist(tmpl_df[header_row, ])))
schema_names <- normalize(
vapply(schema, function(x) x$display_name, character(1))
)
n_schema <- length(schema_names)
n_tmpl <- length(tmpl_headers)
if (n_schema != n_tmpl) {
stop(glue::glue(
"Schema has {n_schema} columns but '{sheet_name}' template has ",
"{n_tmpl}. Update the schema or template so they match."
))
}
mismatches <- which(schema_names != tmpl_headers)
if (length(mismatches) > 0) {
mismatch_msg <- paste(vapply(mismatches, function(i) {
glue::glue(
" Col {i}: schema='{schema_names[i]}'",
" vs template='{tmpl_headers[i]}'"
)
}, character(1)), collapse = "\n")
stop(glue::glue(
"Column display name mismatches in '{sheet_name}':\n{mismatch_msg}"
))
}
invisible(NULL)
}
Loading
Loading