Skip to content

Commit

Permalink
style
Browse files Browse the repository at this point in the history
  • Loading branch information
ddsjoberg committed Feb 6, 2024
1 parent 6d85d9b commit 0a61c00
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 75 deletions.
15 changes: 7 additions & 8 deletions R/as_gt.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ table_styling_to_gt_calls <- function(x, ...) {
# gt -------------------------------------------------------------------------
groupname_col <-
switch("groupname_col" %in% x$table_styling$header$column,
"groupname_col"
"groupname_col"
)
caption <-
switch(!is.null(x$table_styling$caption),
rlang::call2(
attr(x$table_styling$caption, "text_interpret"),
x$table_styling$caption
)
rlang::call2(
attr(x$table_styling$caption, "text_interpret"),
x$table_styling$caption
)
)
gt_calls[["gt"]] <-
expr(gt::gt(
Expand Down Expand Up @@ -213,10 +213,9 @@ table_styling_to_gt_calls <- function(x, ...) {

# tab_footnote ---------------------------------------------------------------
if (nrow(x$table_styling$footnote) == 0 &&
nrow(x$table_styling$footnote_abbrev) == 0) {
nrow(x$table_styling$footnote_abbrev) == 0) {
gt_calls[["tab_footnote"]] <- list()
}
else {
} else {
df_footnotes <-
dplyr::bind_rows(
x$table_styling$footnote,
Expand Down
3 changes: 2 additions & 1 deletion R/as_tibble.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ as_tibble.gtsummary <- function(x, include = everything(), col_labels = TRUE,
as.data.frame.gtsummary <- function(...) {
res <- as_tibble(...)

if (inherits(res, "data.frame"))
if (inherits(res, "data.frame")) {
return(as.data.frame(res))
}

res
}
Expand Down
17 changes: 12 additions & 5 deletions R/assign_summary_digits.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,13 @@ assign_summary_digits <- function(data, statistic, type, digits = NULL) {
x,
function(value, stat_name) {
# if not an integer, simply return the value
if (!is_integerish(value)) return(value)
if (!is_integerish(value)) {
return(value)
}
# if an integer is passed for a percentage, process stat with style_percent()
if (stat_name %in% c("p", "p_miss", "p_nonmiss", "p_unweighted"))
if (stat_name %in% c("p", "p_miss", "p_nonmiss", "p_unweighted")) {
return(styfn_percent(digits = value))
}
# otherwise, use style_numer() to style number
return(styfn_number(digits = value))
}
Expand All @@ -96,7 +99,9 @@ assign_summary_digits <- function(data, statistic, type, digits = NULL) {

.guess_continuous_summary_digits <- function(x) {
# if all missing, return 0
if (all(is.na(x))) return(styfn_number(digits = 0L))
if (all(is.na(x))) {
return(styfn_number(digits = 0L))
}

# if class is integer, then round everything to nearest integer
if (inherits(x, "integer")) {
Expand All @@ -122,9 +127,11 @@ assign_summary_digits <- function(data, statistic, type, digits = NULL) {
}

.categorical_summary_functions <-
function(statistics = c("
function(statistics = c(
"
N", "N_obs", "N_miss", "N_nonmiss", "n_unweighted", "N_unweighted",
"p_miss", "p_nonmiss", "p_unweighted")) {
"p_miss", "p_nonmiss", "p_unweighted"
)) {
lst_defaults <-
c(
c("n", "N", "N_obs", "N_miss", "N_nonmiss", "n_unweighted", "N_unweighted") |>
Expand Down
12 changes: 6 additions & 6 deletions R/assign_summary_type.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ assign_summary_type <- function(data, variables, value, type = NULL, cat_thresho

# numeric variables with fewer than 'cat_threshold' levels will be categorical
if (inherits(data[[variable]], base_numeric_classes) &&
length(unique(stats::na.omit(data[[variable]]))) < cat_threshold) {
length(unique(stats::na.omit(data[[variable]]))) < cat_threshold) {
return("categorical")
}

Expand All @@ -93,21 +93,21 @@ assign_summary_type <- function(data, variables, value, type = NULL, cat_thresho

# numeric variables that are 0 and 1 only, will be dichotomous
if (inherits(x, c("integer", "numeric")) &&
length(setdiff(stats::na.omit(x), c(0, 1))) == 0) {
length(setdiff(stats::na.omit(x), c(0, 1))) == 0) {
return(stats::na.omit(x) |> unique() |> sort() |> dplyr::last())
}

# factor variables that are "No" and "Yes" only, will be dichotomous
if (inherits(x, "factor") &&
length(levels(x)) == 2L &&
setequal(toupper(levels(x)), c("NO", "YES"))) {
length(levels(x)) == 2L &&
setequal(toupper(levels(x)), c("NO", "YES"))) {
return(levels(x)[toupper(levels(x)) %in% "YES"])
}

# character variables that are "No" and "Yes" only, will be dichotomous
if (inherits(x, "character") &&
setequal(toupper(stats::na.omit(x)), c("NO", "YES")) &&
length(stats::na.omit(x)) == 2L) {
setequal(toupper(stats::na.omit(x)), c("NO", "YES")) &&
length(stats::na.omit(x)) == 2L) {
return(unique(x)[toupper(unique(x)) %in% "YES"])
}

Expand Down
15 changes: 9 additions & 6 deletions R/modify_header.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


modify_header <- function(x, ..., text_interpret = c("md", "html"),
quiet = NULL, update = NULL) {
# process inputs -------------------------------------------------------------
Expand All @@ -12,8 +10,11 @@ modify_header <- function(x, ..., text_interpret = c("md", "html"),
"2.0.0", "gtsummary::modify_header(update=)",
details = "Use `modify_header(...)` input instead."
)
if (is.factor(dots)) dots <- c(list(dots), update)
else dots <- c(list(dots), update)
if (is.factor(dots)) {
dots <- c(list(dots), update)
} else {
dots <- c(list(dots), update)
}
}

cards::process_formula_selectors(data = x$table_body, dots = dots)
Expand All @@ -22,7 +23,8 @@ modify_header <- function(x, ..., text_interpret = c("md", "html"),
predicate = function(x) is_string(x),
error_msg =
c("All values passed in {.arg ...} must be strings.",
"i" = "For example, {.code label = '**Variable**'}")
"i" = "For example, {.code label = '**Variable**'}"
)
)

# evaluate the strings with glue
Expand Down Expand Up @@ -69,8 +71,9 @@ modify_header <- function(x, ..., text_interpret = c("md", "html"),
data = df_header_subset
)

if (!is.null(glued_value$result))
if (!is.null(glued_value$result)) {
return(glued_value$result)
}

cli::cli_abort("There was an error the {.fun glue::glue} evaluation of {.val {value}}.")
}
Expand Down
36 changes: 16 additions & 20 deletions R/modify_table_styling.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ modify_table_styling <- function(x,
indentation = ifelse("indent" %in% text_format, 4L, 8L),
text_format =
text_format |>
setdiff(c("indent", "indent2")) %>%
{.ifelse1(is_empty(.), NULL, .)} #nolint
setdiff(c("indent", "indent2")) %>%
{.ifelse1(is_empty(.), NULL, .)} # styler: off
)
env_bind(.env = current_env(), !!!lst_new_args)

Expand Down Expand Up @@ -157,7 +157,7 @@ modify_table_styling <- function(x,
rows_eval_error <-
tryCatch(
eval_tidy(rows, data = x$table_body) %>%
{!is.null(.) && !is.logical(.)}, #nolint
{!is.null(.) && !is.logical(.)}, # styler: off
error = function(e) TRUE
)
if (rows_eval_error) {
Expand Down Expand Up @@ -271,8 +271,8 @@ modify_table_styling <- function(x,
format_type = text_format,
undo_text_format = FALSE
) %>%
{tidyr::expand_grid(!!!.)} %>% # nolint
{dplyr::bind_rows(x$table_styling$text_format, .)} # nolint
{tidyr::expand_grid(!!!.)} %>% # styler: off
{dplyr::bind_rows(x$table_styling$text_format, .)} # styler: off
}
if (!is.null(undo_text_format)) {
x$table_styling$text_format <-
Expand All @@ -282,8 +282,8 @@ modify_table_styling <- function(x,
format_type = undo_text_format,
undo_text_format = TRUE
) %>%
{tidyr::expand_grid(!!!.)} %>% # nolint
{dplyr::bind_rows(x$table_styling$text_format, .)} # nolint
{tidyr::expand_grid(!!!.)} %>% # styler: off
{dplyr::bind_rows(x$table_styling$text_format, .)} # styler: off
}

# indentation ----------------------------------------------------------------
Expand All @@ -292,14 +292,14 @@ modify_table_styling <- function(x,
cli::cli_abort("The {.arg indentation} argument must be a scalar integer.")
}
x$table_styling$indentation <-
dplyr::bind_rows(
x$table_styling$indentation,
dplyr::tibble(
column = columns,
rows = list(rows),
n_spaces = as.integer(indentation)
dplyr::bind_rows(
x$table_styling$indentation,
dplyr::tibble(
column = columns,
rows = list(rows),
n_spaces = as.integer(indentation)
)
)
)
}

# missing_symbol -------------------------------------------------------------
Expand All @@ -310,12 +310,8 @@ modify_table_styling <- function(x,
rows = list(rows),
symbol = missing_symbol
) %>%
{
tidyr::expand_grid(!!!.)
} %>%
{
dplyr::bind_rows(x$table_styling$fmt_missing, .)
}
{tidyr::expand_grid(!!!.)} %>% # styler: off
{dplyr::bind_rows(x$table_styling$fmt_missing, .)} # styler: off
}

# cols_merge_pattern ---------------------------------------------------------
Expand Down
1 change: 0 additions & 1 deletion R/print.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ print.gtsummary <- function(x,
) %>%
print()
}

3 changes: 2 additions & 1 deletion R/standalone-checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ check_class <- function(x, class, length = NULL, allow_empty = FALSE,
if (!inherits(x, class)) {
cli::cli_abort(
c("Argument {.arg {arg_name}} must be class {.cls {class}}.",
"i" = "The class of {.arg {arg_name}} is {.cls {class(x)}}."),
"i" = "The class of {.arg {arg_name}} is {.cls {class(x)}}."
),
call = call
)
}
Expand Down
8 changes: 6 additions & 2 deletions R/standalone-purrr.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,19 @@ every <- function(.x, .p, ...) {
.p <- as_function(.p, env = global_env())

for (i in seq_along(.x)) {
if (!rlang::is_true(.p(.x[[i]], ...))) return(FALSE)
if (!rlang::is_true(.p(.x[[i]], ...))) {
return(FALSE)
}
}
TRUE
}
some <- function(.x, .p, ...) {
.p <- as_function(.p, env = global_env())

for (i in seq_along(.x)) {
if (rlang::is_true(.p(.x[[i]], ...))) return(TRUE)
if (rlang::is_true(.p(.x[[i]], ...))) {
return(TRUE)
}
}
FALSE
}
Expand Down
1 change: 0 additions & 1 deletion R/styfn.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,3 @@ styfn_ratio <- function(digits = 2, big.mark = NULL, decimal.mark = NULL, ...) {
styfn_percent <- function(symbol = FALSE, digits = 0, big.mark = NULL, decimal.mark = NULL, ...) {
function(x) style_percent(x, symbol = symbol, digits = digits, big.mark = big.mark, decimal.mark = decimal.mark, ...)
}

2 changes: 1 addition & 1 deletion R/style_number.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ style_number <- function(x, digits = 0, big.mark = NULL, decimal.mark = NULL,
# this function assures that 5s are rounded up (and not to even, the default in `round()`)
# code taken from https://github.com/sfirke/janitor/blob/main/R/round_half_up.R
round2 <- function(x, digits = 0) {
trunc(abs(x) * 10 ^ digits + 0.5 + sqrt(.Machine$double.eps)) / 10 ^ digits * sign(as.numeric(x))
trunc(abs(x) * 10^digits + 0.5 + sqrt(.Machine$double.eps)) / 10^digits * sign(as.numeric(x))
}
16 changes: 8 additions & 8 deletions R/utils-as.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

# takes a table_body and a character rows expression, and returns the resulting row numbers
.rows_expr_to_row_numbers <- function(table_body, rows, return_when_null = NA) {
rows_evaluated <- rlang::eval_tidy(rows, data = table_body)

# if a single lgl value, then expand it to the length of the tabel_body
if (is_scalar_logical(rows_evaluated))
if (is_scalar_logical(rows_evaluated)) {
rows_evaluated <- rep_len(rows_evaluated, length.out = nrow(table_body))
}

if (is.null(rows_evaluated)) {
return(return_when_null)
Expand Down Expand Up @@ -78,13 +78,13 @@
dplyr::mutate(
row_numbers =
switch(nrow(.) == 0,
integer(0)
integer(0)
) %||%
.rows_expr_to_row_numbers(
x$table_body, .data$rows,
return_when_null = seq_len(nrow(x$table_body))
) %>%
list(),
.rows_expr_to_row_numbers(
x$table_body, .data$rows,
return_when_null = seq_len(nrow(x$table_body))
) %>%
list(),
) %>%
dplyr::select(-"rows") %>%
tidyr::unnest("row_numbers") %>%
Expand Down
6 changes: 2 additions & 4 deletions R/utils-gtsummary_core.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ construct_initial_table_styling <- function(x) {
rows = list(rlang::expr(TRUE)),
n_spaces = 0L
)
}
else {
} else {
dplyr::tibble(column = character(), rows = list(), n_spaces = integer())
}

Expand Down Expand Up @@ -95,7 +94,7 @@ construct_initial_table_styling <- function(x) {
for (styling_element in names(x$table_styling)) {
# if element is a tibble with a column called 'column'
if (is.data.frame(x$table_styling[[styling_element]]) &&
"column" %in% names(x$table_styling[[styling_element]])) {
"column" %in% names(x$table_styling[[styling_element]])) {
x$table_styling[[styling_element]] <-
x$table_styling[[styling_element]] %>%
dplyr::filter(!.data$column %in% deleted_columns)
Expand Down Expand Up @@ -135,4 +134,3 @@ construct_initial_table_styling <- function(x) {
by = "column"
)
}

5 changes: 3 additions & 2 deletions R/utils-misc.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

.extract_glue_elements <- function(x) {
regmatches(x, gregexpr("\\{([^\\}]*)\\}", x)) |>
unlist() %>%
{substr(., 2, nchar(.) - 1)}
{
substr(., 2, nchar(.) - 1)
}
}

.ifelse1 <- function(test, yes, no) {
Expand Down
18 changes: 9 additions & 9 deletions man/bridge_summary.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0a61c00

Please sign in to comment.