Skip to content

Commit

Permalink
Lintr (#869)
Browse files Browse the repository at this point in the history
* Lintr

* Merge branch 'main' into lintr2

* fix

* fix
  • Loading branch information
strengejacke authored Apr 29, 2024
1 parent a3769f0 commit 5446137
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 99 deletions.
26 changes: 13 additions & 13 deletions R/format_bf.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ format_bf <- function(bf,

digits <- ifelse(is.na(bf), 0, ifelse(bf < 1, 3, 2)) # nolint

text <- paste0(
bf_text <- paste0(
"= ",
ifelse(is_small, "1/", ""),
format_value(bf, digits = digits)
Expand All @@ -52,37 +52,37 @@ format_bf <- function(bf,
is_extreme <- bf_orig > 1000 | bf_orig < 1 / 1000
if (any(is_extreme)) {
if (exact) {
text[is_extreme] <- ifelse(bf_orig[is_extreme] > 1000,
bf_text[is_extreme] <- ifelse(bf_orig[is_extreme] > 1000,
sprintf("= %.2e", bf_orig[is_extreme]),
text[is_extreme]
bf_text[is_extreme]
)
text[is_extreme] <- ifelse(bf_orig[is_extreme] < 1 / 1000,
bf_text[is_extreme] <- ifelse(bf_orig[is_extreme] < 1 / 1000,
ifelse(is_small[is_extreme], # nolint
sprintf("= 1/%.2e", bf[is_extreme]),
sprintf("= %.2e", bf_orig[is_extreme])
),
text[is_extreme]
bf_text[is_extreme]
)
} else {
text[is_extreme] <- ifelse(bf_orig[is_extreme] > 1000,
bf_text[is_extreme] <- ifelse(bf_orig[is_extreme] > 1000,
"> 1000",
text[is_extreme]
bf_text[is_extreme]
)
text[is_extreme] <- ifelse(bf_orig[is_extreme] < 1 / 1000,
bf_text[is_extreme] <- ifelse(bf_orig[is_extreme] < 1 / 1000,
ifelse(is_small[is_extreme], "< 1/1000", "< 0.001"), # nolint
text[is_extreme]
bf_text[is_extreme]
)
}
}

## Add stars
text <- ifelse(bf_orig > 30, paste0(text, "***"),
ifelse(bf_orig > 10, paste0(text, "**"), # nolint
ifelse(bf_orig > 3, paste0(text, "*"), text) # nolint
bf_text <- ifelse(bf_orig > 30, paste0(bf_text, "***"),
ifelse(bf_orig > 10, paste0(bf_text, "**"), # nolint
ifelse(bf_orig > 3, paste0(bf_text, "*"), bf_text) # nolint
)
)

out <- .add_prefix_and_remove_stars(text, stars, stars_only, name)
out <- .add_prefix_and_remove_stars(p_text = bf_text, stars, stars_only, name)
if (is.na(na_reference)) out[bad_bf] <- ""
out
}
40 changes: 22 additions & 18 deletions R/format_p.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,12 @@ format_p <- function(p,
# only convert p if it's a valid numeric, or at least coercible to
# valid numeric values...
if (!is.numeric(p)) {
if (.is_numeric_character(p)) {
p <- .factor_to_numeric(p)
} else {
if (!.is_numeric_character(p)) {
return(p)
}
p <- .factor_to_numeric(p)
}


if (identical(stars, "only")) {
stars <- TRUE
stars_only <- TRUE
Expand All @@ -67,7 +65,7 @@ format_p <- function(p,
if (is.na(digits)) {
digits <- 5
}
text <- ifelse(is.na(p), NA,
p_text <- ifelse(is.na(p), NA,
ifelse(p < 0.001, sprintf("= %.*e***", digits, p), # nolint
ifelse(p < 0.01, sprintf("= %.*e**", digits, p), # nolint
ifelse(p < 0.05, sprintf("= %.*e*", digits, p), # nolint
Expand All @@ -79,7 +77,7 @@ format_p <- function(p,
)
)
} else if (digits <= 3) {
text <- ifelse(is.na(p), NA,
p_text <- ifelse(is.na(p), NA,
ifelse(p < 0.001, "< .001***", # nolint
ifelse(p < 0.01, paste0("= ", format_value(p, digits), "**"), # nolint
ifelse(p < 0.05, paste0("= ", format_value(p, digits), "*"), # nolint
Expand All @@ -91,7 +89,7 @@ format_p <- function(p,
)
)
} else {
text <- ifelse(is.na(p), NA,
p_text <- ifelse(is.na(p), NA,
ifelse(p < 0.001, paste0("= ", format_value(p, digits), "***"), # nolint
ifelse(p < 0.01, paste0("= ", format_value(p, digits), "**"), # nolint
ifelse(p < 0.05, paste0("= ", format_value(p, digits), "*"), # nolint
Expand All @@ -102,38 +100,44 @@ format_p <- function(p,
)
}

.add_prefix_and_remove_stars(text, stars, stars_only, name, missing, whitespace, decimal_separator)
.add_prefix_and_remove_stars(p_text, stars, stars_only, name, missing, whitespace, decimal_separator)
}


#' @keywords internal
.add_prefix_and_remove_stars <- function(text, stars, stars_only, name, missing = "", whitespace = TRUE, decimal_separator = NULL) {
missing_index <- is.na(text)
.add_prefix_and_remove_stars <- function(p_text,
stars,
stars_only,
name,
missing = "",
whitespace = TRUE,
decimal_separator = NULL) {
missing_index <- is.na(p_text)

if (is.null(name)) {
text <- gsub("= ", "", text, fixed = TRUE)
p_text <- gsub("= ", "", p_text, fixed = TRUE)
} else {
text <- paste(name, text)
p_text <- paste(name, p_text)
}

if (stars_only) {
text <- gsub("[^\\*]", "", text)
p_text <- gsub("[^\\*]", "", p_text)
} else if (!stars) {
text <- gsub("*", "", text, fixed = TRUE)
p_text <- gsub("*", "", p_text, fixed = TRUE)
}

# replace missing with related string
text[missing_index] <- missing
p_text[missing_index] <- missing

# remove whitespace around < and >
if (isFALSE(whitespace)) {
text <- gsub(" ", "", text, fixed = TRUE)
p_text <- gsub(" ", "", p_text, fixed = TRUE)
}

# replace decimal separator
if (!is.null(decimal_separator)) {
text <- gsub(".", decimal_separator, text, fixed = TRUE)
p_text <- gsub(".", decimal_separator, p_text, fixed = TRUE)
}

text
p_text
}
4 changes: 2 additions & 2 deletions R/format_pd.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' format_pd(c(0.12, 1, 0.9999, 0.98, 0.995, 0.96), stars = TRUE)
#' @export
format_pd <- function(pd, stars = FALSE, stars_only = FALSE, name = "pd") {
text <- ifelse(pd >= 1, "= 100%***",
p_text <- ifelse(pd >= 1, "= 100%***",
ifelse(pd > 0.999, paste0("= ", format_value(pd * 100), "%***"), # nolint
ifelse(pd > 0.99, paste0("= ", format_value(pd * 100), "%**"), # nolint
ifelse(pd > 0.97, paste0("= ", format_value(pd * 100), "%*"), # nolint
Expand All @@ -20,5 +20,5 @@ format_pd <- function(pd, stars = FALSE, stars_only = FALSE, name = "pd") {
)
)

.add_prefix_and_remove_stars(text, stars, stars_only, name)
.add_prefix_and_remove_stars(p_text, stars, stars_only, name)
}
34 changes: 17 additions & 17 deletions R/format_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -390,22 +390,22 @@ format_table <- function(x,
for (stats in c("t", "Chi2")) {
if (stats %in% names(x) && "df" %in% names(x)) {
if (is.character(x$df)) {
x$df[x$df == ""] <- NA_character_
x$df[x$df == ""] <- NA_character_ # nolint
}
df <- unique(x$df)
df <- df[!is.na(df)]
if (length(df) == 1 && !all(is.infinite(df))) {
names(x)[names(x) == stats] <- paste0(stats, "(", df, ")")
dof <- unique(x$df)
dof <- dof[!is.na(dof)]
if (length(dof) == 1 && !all(is.infinite(dof))) {
names(x)[names(x) == stats] <- paste0(stats, "(", dof, ")")
x$df <- NULL
}
} else if (stats %in% names(x) && "df_error" %in% names(x)) {
if (is.character(x$df_error)) {
x$df_error[x$df_error == ""] <- NA_character_
x$df_error[x$df_error == ""] <- NA_character_ # nolint
}
df <- unique(x$df_error)
df <- df[!is.na(df)]
if (length(df) == 1 && !all(is.infinite(df))) {
names(x)[names(x) == stats] <- paste0(stats, "(", df, ")")
dof <- unique(x$df_error)
dof <- dof[!is.na(dof)]
if (length(dof) == 1 && !all(is.infinite(dof))) {
names(x)[names(x) == stats] <- paste0(stats, "(", dof, ")")
x$df_error <- NULL
}
}
Expand All @@ -414,10 +414,10 @@ format_table <- function(x,
for (stats in c("Baseline", "Chi2")) {
df_col <- paste0(stats, "_df")
if (stats %in% names(x) && df_col %in% names(x)) {
df <- unique(x[[df_col]])
df <- df[!is.na(df)]
if (length(df) == 1 && !all(is.infinite(df))) {
names(x)[names(x) == stats] <- paste0(stats, "(", df, ")")
dof <- unique(x[[df_col]])
dof <- dof[!is.na(dof)]
if (length(dof) == 1 && !all(is.infinite(dof))) {
names(x)[names(x) == stats] <- paste0(stats, "(", dof, ")")
x[[df_col]] <- NULL
}
}
Expand Down Expand Up @@ -831,10 +831,10 @@ format_table <- function(x,


.additional_arguments <- function(x, value, default) {
args <- attributes(x)$additional_arguments
my_args <- attributes(x)$additional_arguments

if (length(args) > 0 && value %in% names(args)) {
out <- args[[value]]
if (length(my_args) > 0 && value %in% names(my_args)) {
out <- my_args[[value]]
} else {
out <- attributes(x)[[value]]
}
Expand Down
6 changes: 3 additions & 3 deletions R/get_loglikelihood.R
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ get_loglikelihood.afex_aov <- function(x, ...) {
NA
},
gaussian = {
nobs <- length(resp)
-((log(dev / nobs * 2 * pi) + 1) - log(w)) / 2
model_nobs <- length(resp)
-((log(dev / model_nobs * 2 * pi) + 1) - log(w)) / 2
},
inverse.gaussian = {
-((log(disp * 2 * pi) + 1) + 3 * log(resp)) / 2
Expand Down Expand Up @@ -475,7 +475,7 @@ get_loglikelihood.phyloglm <- get_loglikelihood.phylolm
{
trans <- find_transformation(x)

if (trans == "identity") {
if (trans == "identity") { # nolint
.weighted_sum(log(get_response(x, as_proportion = TRUE)), w = model_weights)
} else if (trans == "log") {
.weighted_sum(log(1 / get_response(x, as_proportion = TRUE)), w = model_weights)
Expand Down
Loading

0 comments on commit 5446137

Please sign in to comment.