Skip to content

Commit

Permalink
[skip style] [skip vbump] Restyle files
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Nov 19, 2024
1 parent a1994fa commit a8bd470
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 51 deletions.
90 changes: 50 additions & 40 deletions R/analyze_variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,8 @@ s_summary.logical <- function(x,
#' a_summary(rnorm(10), .N_col = 10, .N_row = 20, .var = "bla")
#' a_summary(rnorm(10, 5, 1), .ref_group = rnorm(20, -5, 1), .var = "bla", compare = TRUE)
#' a_summary(
#' rnorm(10, 5, 1), .ref_group = rnorm(20, -5, 1), .var = "bla", compare = TRUE,
#' rnorm(10, 5, 1),
#' .ref_group = rnorm(20, -5, 1), .var = "bla", compare = TRUE,
#' .stats = "mean",
#' fmts_df_var = "variant1",
#' d = 2
Expand All @@ -463,28 +464,33 @@ s_summary.logical <- function(x,
#' xref <- rnorm(20, -5, 1)
#'
#' a_summary(
#' x1, .ref_group = xref, .var = "bla", compare = TRUE,
#' x1,
#' .ref_group = xref, .var = "bla", compare = TRUE,
#' .stats = c("mean", "sd"),
#' .formats = c("mean" = format_xx("xx.xxx"), "sd" = format_xx("xx.x"))
#' )
#' a_summary(
#' x1, .ref_group = xref, .var = "bla", compare = TRUE,
#' x1,
#' .ref_group = xref, .var = "bla", compare = TRUE,
#' .stats = "mean_sd",
#' fmt_specs = list(
#' fmts_df_var = "variant2",
#' d = 1,
#' formatting_function = "format_xx")
#' fmts_df_var = "variant2",
#' d = 1,
#' formatting_function = "format_xx"
#' )
#' )
#' a_summary(
#' x1, .ref_group = xref, .var = "bla", compare = TRUE,
#' x1,
#' .ref_group = xref, .var = "bla", compare = TRUE,
#' .stats = c("mean", "mean_sd", "mean_pval")
#' )
#'
#' our_fmt_specs_variant <- list(
#' fmts_df = tern_formats_custom_df(),
#' fmts_df_var = "default",
#' formatting_function = "format_xx_fixed_dp",
#' d = 0)
#' our_fmt_specs_variant <- list(
#' fmts_df = tern_formats_custom_df(),
#' fmts_df_var = "default",
#' formatting_function = "format_xx_fixed_dp",
#' d = 0
#' )

#' a_summary(
#' x1, .ref_group = xref, .var = "bla", compare = TRUE,
Expand Down Expand Up @@ -550,10 +556,11 @@ a_summary <- function(x,
# note that is it most safe to apply formatting functions, as many of the final formats will not belong to
# list_valid_format_labels()

Check warning on line 557 in R/analyze_variables.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=R/analyze_variables.R,line=557,col=7,[commented_code_linter] Commented code should be removed.
.formats_all <- get_formats_from_stats_custom(
.stats,
formats_in = .formats,
### variant specific arguments
fmts_specs = fmt_specs)
.stats,
formats_in = .formats,
### variant specific arguments
fmts_specs = fmt_specs
)
.formats <- .formats_all$fmt
.formats_char <- .formats_all$fmt_char
}
Expand Down Expand Up @@ -666,46 +673,49 @@ a_summary <- function(x,
#' build_table(dt)
#'
#' # custom format
#' our_fmt_specs_variant <- list(
#' fmts_df = tern_formats_custom_df(),
#' fmts_df_var = "variant2",
#' formatting_function = "format_xx_fixed_dp",
#' d = 0)
#' our_fmt_specs_variant <- list(
#' fmts_df = tern_formats_custom_df(),
#' fmts_df_var = "variant2",
#' formatting_function = "format_xx_fixed_dp",
#' d = 0
#' )
#'
#' dt <- data.frame("VAR" = c(0.001, 0.2, 0.0011000, 3, 4))
#' basic_table() %>%
#' analyze_vars(
#' vars = "VAR",
#' .stats = c("n", "mean", "mean_sd", "range"),
#' .formats = c("mean" = "xx.dxx"),
#' fmt_specs = our_fmt_specs_variant,
#' fmt_specs = our_fmt_specs_variant,
#' ) %>%
#' build_table(dt)
#'
#' # custom format
#' our_fmt_specs_variant2 <- list(
#' fmts_df = tern_formats_custom_df(),
#' fmts_df_var = "variant2",
#' formatting_function = "format_xx_fixed_dp",
#' d = "decimal")
#' our_fmt_specs_variant2 <- list(
#' fmts_df = tern_formats_custom_df(),
#' fmts_df_var = "variant2",
#' formatting_function = "format_xx_fixed_dp",
#' d = "decimal"
#' )
#' dt <- data.frame("VAR" = c(0.001, 0.2, 0.0011000, 3, 4), decimal = 2)
#' basic_table() %>%
#' analyze_vars(
#' vars = "VAR",
#' .stats = c("n", "mean", "mean_sd", "range"),
#' .formats = c("mean" = "xx.dxxxxxx"),
#' fmt_specs = our_fmt_specs_variant2,
#' fmt_specs = our_fmt_specs_variant2,
#' ) %>%
#' build_table(dt)
#'
#' # custom format
#' dt2 <- data.frame("VAR" = c(0.001, 0.2, 0.0011000, 3, 4, 0.002, 0.004, 0.006), decimal = c(rep(2, 4), rep(1, 4)), by = c(rep("by1", 4), rep("by2", 4)))

Check warning on line 711 in R/analyze_variables.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=R/analyze_variables.R,line=711,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 154 characters.
#' our_fmt_specs_variant2 <- list(
#' fmts_df = tern_formats_custom_df(),
#' fmts_df_var = "variant2",
#' formatting_function = "format_xx_fixed_dp",
#' d = "decimal",
#' d_cap = 3)
#' our_fmt_specs_variant2 <- list(
#' fmts_df = tern_formats_custom_df(),
#' fmts_df_var = "variant2",
#' formatting_function = "format_xx_fixed_dp",
#' d = "decimal",
#' d_cap = 3
#' )
#' basic_table() %>%
#' split_rows_by("by") %>%
#' analyze_vars(
Expand All @@ -731,13 +741,13 @@ analyze_vars <- function(lyt,
.formats = NULL,
.labels = NULL,
.indent_mods = NULL,

# varying precision arguments
fmt_specs = default_fmt_specs
) {
extra_args <- list(.stats = .stats, na.rm = na.rm, na_str = na_str,
fmt_specs = fmt_specs,
...)
fmt_specs = default_fmt_specs) {
extra_args <- list(
.stats = .stats, na.rm = na.rm, na_str = na_str,
fmt_specs = fmt_specs,
...
)

if (!is.null(.formats)) extra_args[[".formats"]] <- .formats
if (!is.null(.labels)) extra_args[[".labels"]] <- .labels
Expand Down
29 changes: 18 additions & 11 deletions R/formatting_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -272,40 +272,48 @@ format_xx_fixed_dp <- function(str, na_str) {
if (!grepl(pattern = "xx", x = str, fixed = TRUE)) {
stop("Error: format_xx_fixed_dp: input str should contain xx")
}
positions <- gregexpr(pattern = "xx\\.?x*", text = str,
perl = TRUE)
positions <- gregexpr(
pattern = "xx\\.?x*", text = str,
perl = TRUE
)
x_positions <- regmatches(x = str, m = positions)[[1]]
### str is splitted into pieces as xx. xx xx.xxx
### xx is no rounding
### xx. rounding to integer (is treated same as rounding to 0 decimal)
### xx.x rounding to 1 decimal, etc

no_round <- function(x){
if (is.na(x)) { return(na_str)
} else return(x)
no_round <- function(x) {
if (is.na(x)) {
return(na_str)
} else {
return(x)
}
}
roundfunc <- round

# Roundings depends on the number of x behind [.].
roundings <- lapply(
X = x_positions,
function(x) {
if (x == "xx"){
if (x == "xx") {
rounding <- no_round
} else {
y <- strsplit(split = "\\.", x = x)[[1]]
digits <- ifelse(length(y) > 1, nchar(y[2]), 0)

rounding <- function(x) {
if (is.na(x)) { return(na_str)
} else format(roundfunc(x,digits = digits),
nsmall = digits)
if (is.na(x)) {
return(na_str)
} else {
format(roundfunc(x, digits = digits),
nsmall = digits
)
}
}
}

return(rounding)
}

)

rtable_format <- function(x, output) {
Expand All @@ -315,7 +323,6 @@ format_xx_fixed_dp <- function(str, na_str) {
}

return(rtable_format)

}


Expand Down

0 comments on commit a8bd470

Please sign in to comment.