diff --git a/r_functionality.R b/r_functionality.R index b7122eb..857edcf 100644 --- a/r_functionality.R +++ b/r_functionality.R @@ -911,8 +911,10 @@ checkAssumptionsForAnova <- function(data, y, factors) { return("You must take the non-parametric ANOVA as Levene’s test is significant (p < 0.05).") } - print("You may take parametric ANOVA (function anova_test). See https://www.datanovia.com/en/lessons/anova-in-r/#check-assumptions-1 for more information.") -} + message("You may take parametric ANOVA (function anova_test). See https://www.datanovia.com/en/lessons/anova-in-r/#check-assumptions-1 for more information.") + + invisible(NULL) +} #' Generate the Latex-text based on the NPAV by Lüpsen (see \url{http://www.uni-koeln.de/~luepsen/R/}). @@ -1113,40 +1115,46 @@ reportNPAVChi <- function(model, dv = "Testdependentvariable", write_to_clipboar stringtowrite <- paste0("The NPAV found a significant main effect of \\", trimws(model$descriptions[i]), " on ", dv, " (\\chisq~(1)=", Chivalue, ", ", pValue, ")") } - effect_size_text <- "" - if (!is.null(sample_size) && is.numeric(sample_size) && sample_size > 0) { - effect_size <- tryCatch( - effectsize::chisq_to_w( - chi = Chivalue, - n = sample_size, - ci = 0.95 - ), - error = function(e) NULL - ) - - if (!is.null(effect_size)) { - effect_size <- as.data.frame(effect_size) - w_value <- effect_size$Cohens_w - ci_low <- effect_size$CI_low - ci_high <- effect_size$CI_high - if (!is.null(w_value) && !is.na(w_value)) { - effect_size_text <- paste0( - ", $w=", - sprintf("%.2f", w_value) - ) - if (!is.null(ci_low) && !is.null(ci_high) && !any(is.na(c(ci_low, ci_high)))) { - effect_size_text <- paste0( - effect_size_text, - " [", - sprintf("%.2f", ci_low), - ", ", - sprintf("%.2f", ci_high), - "]" - ) - } - } - } - } + effect_size_text <- "" + if (!is.null(sample_size) && is.numeric(sample_size) && sample_size > 0) { + effect_size <- tryCatch( + effectsize::chisq_to_w( + chi = Chivalue, + n = sample_size, + ci = 0.95 + ), + error = function(e) NULL + ) + + w_value <- sqrt(Chivalue / sample_size) + ci_low <- NULL + ci_high <- NULL + + if (!is.null(effect_size)) { + effect_size <- as.data.frame(effect_size) + w_value <- effect_size$Cohens_w %||% w_value + ci_low <- effect_size$CI_low + ci_high <- effect_size$CI_high + } + + if (!is.null(w_value) && !is.na(w_value)) { + effect_size_text <- paste0( + ", $w=", + sprintf("%.2f", w_value) + ) + + if (!is.null(ci_low) && !is.null(ci_high) && !any(is.na(c(ci_low, ci_high)))) { + effect_size_text <- paste0( + effect_size_text, + " [", + sprintf("%.2f", ci_low), + ", ", + sprintf("%.2f", ci_high), + "]" + ) + } + } + } stringtowrite <- paste0(stringtowrite, effect_size_text, ". ") @@ -1838,15 +1846,24 @@ reportDunnTestTable <- function(d = NULL, data, iv = "testiv", dv = "testdv", or # Format effect size table$r <- formatC(table$r, digits = 2, format = "f") - # Adjust the xtable call to handle the modified columns - xtable_obj <- xtable(table, - digits = c(0, 0, 4, 0, 0), - caption = paste0("Post-hoc comparisons for independent variable \\", iv, - " and dependent variable \\", dv, - ". Positive Z-values mean that the first-named level is sig. higher than the second-named. For negative Z-values, the opposite is true. Effect size reported as rank-biserial correlation (r)."), - label = paste0("tab:posthoc-", iv, "-", dv)) - - print(xtable_obj, type = "latex", size = latexSize, caption.placement = "top", include.rownames = FALSE) + # Adjust the xtable call to handle the modified columns + if (requireNamespace("xtable", quietly = TRUE)) { + xtable_obj <- xtable::xtable(table, + digits = c(0, 0, 4, 0, 0), + caption = paste0("Post-hoc comparisons for independent variable \\", iv, + " and dependent variable \\", dv, + ". Positive Z-values mean that the first-named level is sig. higher than the second-named. For negative Z-values, the opposite is true. Effect size reported as rank-biserial correlation (r)."), + label = paste0("tab:posthoc-", iv, "-", dv)) + + print(xtable_obj, type = "latex", size = latexSize, caption.placement = "top", include.rownames = FALSE) + } else { + cat(paste0( + "Post-hoc comparisons for independent variable \", iv, + " and dependent variable \", dv, + ". Positive Z-values mean that the first-named level is sig. higher than the second-named. For negative Z-values, the opposite is true. Effect size reported as rank-biserial correlation (r).\n" + )) + print(table) + } } #' Report statistical details for ggstatsplot. diff --git a/tests/testthat/test_r_functionality.R b/tests/testthat/test_r_functionality.R index e70a41e..5e2bffc 100644 --- a/tests/testthat/test_r_functionality.R +++ b/tests/testthat/test_r_functionality.R @@ -73,7 +73,10 @@ posthoc_stats <- list( basic_plot <- ggplot2::ggplot(sample_df, ggplot2::aes(x = ConditionID, y = value)) + ggplot2::geom_point() -with_mock <- testthat::with_mocked_bindings +# Helper wrapper to avoid relying on pkgload/devtools metadata when mocking +with_mock <- function(..., .env = parent.frame()) { + testthat::with_mocked_bindings(..., .package = "base", .env = .env) +} #### basic utilities ---------------------------------------------------------