Skip to content

Commit

Permalink
added ui tests for assumptiopns, correlations, statisticla tests, tte…
Browse files Browse the repository at this point in the history
…st and visualisation
  • Loading branch information
Konrad1991 committed Nov 18, 2024
1 parent fc35c5b commit a78557e
Show file tree
Hide file tree
Showing 5 changed files with 551 additions and 37 deletions.
50 changes: 50 additions & 0 deletions bs/inst/tinytest/Assumptions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
library(shinytest2)
library(tinytest)
app <- bs::app()
app <- shiny::shinyApp(app$ui, app$server)
app <- AppDriver$new(app)
app$upload_file(
file = system.file("/test_data/CO2.csv", package = "bs")
)
app$set_inputs(conditionedPanels = "Assumption")
app$set_window_size(width = 2259, height = 1326)
app$click("ASS-open_formula_editor")
app$set_inputs(`FO-colnames-dropdown_0` = "uptake")
app$click("FO-colnames_Treatment_0")
app$click("FO-create_formula")
app$run_js("$('.modal-footer button:contains(\"Close\")').click();")
app$click("ASS-shapiro")
res <- app$get_values()$export
expected <- rbind(
broom::tidy(shapiro.test(CO2[CO2$Treatment == "nonchilled", "uptake"])),
broom::tidy(shapiro.test(CO2[CO2$Treatment == "chilled", "uptake"]))
)
expected$variable <- c("nonchilled", "chilled")
expected$`Normal distributed` <- expected$p.value > 0.05
expected
tinytest::expect_equal(res[[1]], expected)

# Update output value
app$click("ASS-shapiroResiduals")
res <- app$get_values()$export
fit <- lm(uptake ~ Treatment, data = CO2)
r <- resid(fit)
expected <- broom::tidy(shapiro.test(r))
expected$`Residuals normal distributed` <- expected$p.value > 0.05
tinytest::expect_equal(res[[1]], expected)

# Update output value
app$click("ASS-levene")
res <- app$get_values()$export
expected <- broom::tidy(car::leveneTest(uptake ~ Treatment,
data = CO2, center = "mean"))
expected$`Variance homogenity` <- expected$p.value > 0.05
tinytest::expect_equal(res[[1]], expected)

# Update output value
app$click("ASS-DiagnosticPlot")
res <- app$get_values()$export
tinytest::expect_equal(inherits(res[[1]], "ggplot"), TRUE)
# TODO: add internal backend test for diagnostic plot functions

app$stop()
31 changes: 31 additions & 0 deletions bs/inst/tinytest/Correlation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
library(shinytest2)
library(tinytest)
app <- bs::app()
app <- shiny::shinyApp(app$ui, app$server)
app <- AppDriver$new(app)
app$upload_file(
file = system.file("/test_data/CO2.csv", package = "bs")
)
app$set_window_size(width = 2259, height = 1326)
app$set_inputs(conditionedPanels = "Correlation")
app$click("CORR-open_formula_editor")
app$set_inputs(`FO-colnames-dropdown_0` = "uptake")
app$click("FO-colnames_conc_0")
app$click("FO-create_formula")
app$run_js("$('.modal-footer button:contains(\"Close\")').click();")
app$click("CORR-pear")
res <- app$get_values()$export
expected <- cor.test(CO2$uptake, CO2$conc, method = "pearson")
expected <- broom::tidy(expected)
tinytest::expect_equal(res[[1]], expected)
app$click("CORR-spear")
res <- app$get_values()$export
expected <- cor.test(CO2$uptake, CO2$conc, method = "spearman")
expected <- broom::tidy(expected)
tinytest::expect_equal(res[[1]], expected)
app$click("CORR-kendall")
res <- app$get_values()$export
expected <- cor.test(CO2$uptake, CO2$conc, method = "kendall")
expected <- broom::tidy(expected)
tinytest::expect_equal(res[[1]], expected)
app$stop()
96 changes: 96 additions & 0 deletions bs/inst/tinytest/StatisticalTests.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
library(shinytest2)
library(tinytest)
app <- bs::app()
app <- shiny::shinyApp(app$ui, app$server)
app <- AppDriver$new(app)
app$upload_file(
file = system.file("/test_data/CO2.csv", package = "bs")
)
app$set_window_size(width = 2259, height = 1326)
app$set_inputs(conditionedPanels = "Tests")

# Define formula
app$click("TESTS-open_formula_editor")
app$set_inputs(`FO-colnames-dropdown_0` = "uptake")
app$set_inputs(`FO-editable_code` = "conc * Treatment + Type")
app$click("FO-create_formula")
app$run_js("$('.modal-footer button:contains(\"Close\")').click();")

# ANOVA
app$set_inputs(TestsConditionedPanels = "More than two groups")
app$click("TESTS-aovTest")
res <- app$get_values()$export
CO2$Treatment <- as.character(CO2$Treatment)
CO2$Type <- as.character(CO2$Type)
expected <- broom::tidy(aov(uptake ~ conc*Treatment + Type, data = CO2))
expected <- cbind(expected, row.names(expected))
names(expected)[ncol(expected)] <- paste0("conc * Treatment + Type", collapse = ".")
tinytest::expect_equal(res[[1]], expected)

# Kruskal-Wallis
app$click("TESTS-open_formula_editor")
app$set_inputs(`FO-colnames-dropdown_0` = "uptake")
app$set_inputs(`FO-editable_code` = "conc")
app$click("FO-create_formula")
app$run_js("$('.modal-footer button:contains(\"Close\")').click();")

app$click("TESTS-kruskalTest")
res <- app$get_values()$export
CO2$Treatment <- as.character(CO2$Treatment)
expected <- broom::tidy(kruskal.test(uptake ~conc, data = CO2))
expected <- cbind(expected, row.names(expected))
names(expected)[ncol(expected)] <- paste0("conc", collapse = ".")
tinytest::expect_equal(res[[1]], expected)

# PostHoc tests
# TukeyHSD
app$set_inputs(TestsConditionedPanels = "Posthoc tests")
app$set_inputs(`TESTS-PostHocTests` = "HSD")
app$click("TESTS-PostHocTest")
res <- app$get_values()$export
fit <- aov(uptake ~ conc, data = CO2)
fit <- agricolae::HSD.test(fit,
trt = "conc",
alpha = 0.05, group = TRUE, unbalanced = TRUE
)$groups
expected <- cbind(fit, row.names(fit))
names(expected)[ncol(expected)] <- paste0("conc", collapse = ".")
tinytest::expect_equal(res[[1]], expected)

app$set_inputs(TestsConditionedPanels = "Posthoc tests")
app$set_inputs(`TESTS-PostHocTests` = "HSD")
app$set_inputs(`TESTS-design` = "ub")
app$click("TESTS-PostHocTest")
res <- app$get_values()$export
fit <- aov(uptake ~ conc, data = CO2)
fit <- agricolae::HSD.test(fit,
trt = "conc",
alpha = 0.05, group = TRUE, unbalanced = FALSE
)$groups
expected <- cbind(fit, row.names(fit))
names(expected)[ncol(expected)] <- paste0("conc", collapse = ".")
tinytest::expect_equal(res[[1]], expected)

# Kruskal-Wallis test
app$set_inputs(`TESTS-PostHocTests` = "kruskalTest")
app$click("TESTS-PostHocTest")
res <- app$get_values()$export
df <- CO2
dep <- "uptake"
fit <- with(df, agricolae::kruskal(df[, dep],df[, "conc"]),
alpha = 0.05, p.adj = 0.05, group = TRUE
)$groups
expected <- cbind(fit, row.names(fit))
names(expected)[ncol(expected)] <- paste0("conc", collapse = ".")
tinytest::expect_equal(res[[1]], expected)

# LSD


# scheffe


# REGW

app$view()

64 changes: 64 additions & 0 deletions bs/inst/tinytest/TTest.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
library(shinytest2)
library(tinytest)
app <- bs::app()
app <- shiny::shinyApp(app$ui, app$server)
app <- AppDriver$new(app)
app$upload_file(
file = system.file("/test_data/CO2.csv", package = "bs")
)
app$set_window_size(width = 2259, height = 1326)
app$set_inputs(conditionedPanels = "Tests")
app$click("TESTS-open_formula_editor")
app$set_inputs(`FO-colnames-dropdown_0` = "uptake")
app$click("FO-colnames_Treatment_0")
app$click("FO-create_formula")
app$run_js("$('.modal-footer button:contains(\"Close\")').click();")

app$click("TESTS-tTest")
res <- app$get_values()$export
CO2$Treatment <- as.character(CO2$Treatment)
expected <- broom::tidy(
t.test(
uptake ~ Treatment, data = CO2,
var.equal = TRUE, conf.level = 0.95,
alternative = "two.sided"
)
)
tinytest::expect_equal(res[[1]], expected)
# Update output value
app$set_inputs(`TESTS-altHyp` = "less")
app$click("TESTS-tTest")
res <- app$get_values()$export
expected <- broom::tidy(
t.test(
uptake ~ Treatment, data = CO2,
var.equal = TRUE, conf.level = 0.95,
alternative = "less"
)
)
tinytest::expect_equal(res[[1]], expected)
# Update output value
app$set_inputs(`TESTS-altHyp` = "greater")
app$click("TESTS-tTest")
res <- app$get_values()$export
expected <- broom::tidy(
t.test(
uptake ~ Treatment, data = CO2,
var.equal = TRUE, conf.level = 0.95,
alternative = "greater"
)
)
tinytest::expect_equal(res[[1]], expected)
# Update output value
app$set_inputs(`TESTS-varEq` = "noeq")
app$click("TESTS-tTest")
res <- app$get_values()$export
expected <- broom::tidy(
t.test(
uptake ~ Treatment, data = CO2,
var.equal = FALSE, conf.level = 0.95,
alternative = "greater"
)
)
tinytest::expect_equal(res[[1]], expected)
app$stop()
Loading

0 comments on commit a78557e

Please sign in to comment.