Skip to content
Draft
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
6 changes: 6 additions & 0 deletions R/utils_format.R
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,19 @@
# iterate all factors in the data and check if any factor was used in the model
for (fn in names(factors)) {
f <- factors[[fn]]
# for models from pscl, we have "count_" and "zero_" prefixes, which
# we need to add to "f" names, so that we can match them with the parameters
if (inherits(model, c("zeroinfl", "hurdle"))) {
f <- c(paste0("count_", f), paste0("zero_", f))
}
Comment on lines +258 to +262
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

It would be beneficial to add a comment explaining why these prefixes are necessary for pscl models. This enhances code understanding and maintainability.

    # for models from pscl, we have "count_" and "zero_" prefixes,
    # which we need to add to "f" names, so that we can match them with the parameters
    # (pscl models require these prefixes for parameter identification)
    if (inherits(model, c("zeroinfl", "hurdle"))) {

# "f" contains all combinations of factor name and levels from the data,
# which we can match with the names of the pretty_names vector
found <- which(names(pretty_names) %in% f)
# if we have a match, we add the reference level to the pretty_names vector
if (length(found)) {
# the reference level is *not* in the pretty names yet
reference_level <- f[!f %in% names(pretty_names)]
reference_level <- gsub("^(count_|zero_)", "", reference_level, fixed = TRUE)
Comment on lines 269 to +270
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Consider adding a comment explaining why this gsub call is necessary. What potential issues does it resolve?

      # Remove the count_ or zero_ prefix from the reference level
      reference_level <- gsub("^(count_|zero_)", "", reference_level, fixed = TRUE)


# for on-the-fly conversion of factors, the names of the factors can
# can also contain "factor()" or "as.factor()" - we need to remove these
Expand All @@ -279,11 +285,11 @@
if (all(grepl(pattern_cut_right, pretty_level))) {
lower_bounds <- gsub(pattern_cut_right, "\\2", pretty_level)
upper_bounds <- gsub(pattern_cut_right, "\\3", pretty_level)
pretty_level <- gsub(pattern_cut_right, paste0("\\1>", as.numeric(lower_bounds), "-", upper_bounds, "]"), pretty_level)

Check warning on line 288 in R/utils_format.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/utils_format.R,line=288,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 127 characters.
} else if (all(grepl(pattern_cut_left, pretty_level))) {
lower_bounds <- gsub(pattern_cut_left, "\\2", pretty_level)
upper_bounds <- gsub(pattern_cut_left, "\\3", pretty_level)
pretty_level <- gsub(pattern_cut_left, paste0("\\1", as.numeric(lower_bounds), "-<", upper_bounds, "]"), pretty_level)

Check warning on line 292 in R/utils_format.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/utils_format.R,line=292,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 126 characters.
}
# insert new pretty level at the correct position in "pretty_names"
pretty_names <- .insert_element_at(
Expand Down Expand Up @@ -407,7 +413,7 @@

# helper to format the header / subheader of different model components --------------

.format_model_component_header <- function(x,

Check warning on line 416 in R/utils_format.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/utils_format.R,line=416,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this expression from 51 to at most 40.
type,
split_column,
is_zero_inflated,
Expand Down Expand Up @@ -697,7 +703,7 @@
# or edge cases...

#' @keywords internal
.format_columns_multiple_components <- function(x,

Check warning on line 706 in R/utils_format.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/utils_format.R,line=706,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this expression from 97 to at most 40.
pretty_names,
split_column = "Component",
digits = 2,
Expand Down
Loading