Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 76 additions & 16 deletions R/tm_outliers.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,54 @@
#' {{ next_example }}
#' @examples
#'
#' # simple data example without join keys
#' data <- teal_data()
#' data <- within(data, {
#' CO2 <- CO2
#' })
#'
#' vars <- choices_selected(variable_choices(data[["CO2"]], c("Plant", "Type", "Treatment")))
#'
#' app <- init(
#' data = data,
#' modules = modules(
#' tm_outliers(
#' outlier_var = list(
#' data_extract_spec(
#' dataname = "CO2",
#' select = select_spec(
#' label = "Select variable:",
#' choices = variable_choices(data[["CO2"]], c("conc", "uptake")),
#' selected = "uptake",
#' multiple = FALSE,
#' fixed = FALSE
#' )
#' )
#' ),
#' categorical_var = list(
#' data_extract_spec(
#' dataname = "CO2",
#' filter = filter_spec(
#' vars = vars,
#' choices = value_choices(data[["CO2"]], vars$selected),
#' selected = value_choices(data[["CO2"]], vars$selected),
#' multiple = TRUE
#' )
#' )
#' )
#' )
#' )
#' )
#' if (interactive()) {
#' shinyApp(app$ui, app$server)
#' }
#'
#' @examplesShinylive
#' library(teal.modules.general)
#' interactive <- function() TRUE
#' {{ next_example }}
#' @examples
#'
#' # general data example
#' data <- teal_data()
#' data <- within(data, {
Expand Down Expand Up @@ -616,25 +664,37 @@ srv_outliers <- function(id, data, reporter, filter_panel_api, outlier_var,
)

# ANL_OUTLIER_EXTENDED is the base table
qenv <- teal.code::eval_code(
qenv,
substitute(
expr = {
ANL_OUTLIER_EXTENDED <- dplyr::left_join(
ANL_OUTLIER,
dplyr::select(
dataname,
dplyr::setdiff(names(dataname), dplyr::setdiff(names(ANL_OUTLIER), join_keys))
),
by = join_keys
join_keys <- as.character(teal.data::join_keys(data())[dataname_first, dataname_first])

if (length(join_keys) == 0) {
# No join keys defined - working with single dataset, no join needed
# ANL_OUTLIER already contains all necessary columns
qenv <- teal.code::eval_code(
qenv,
quote(ANL_OUTLIER_EXTENDED <- ANL_OUTLIER)
)
} else {
# Join keys exist - perform left join as before
qenv <- teal.code::eval_code(
qenv,
substitute(
expr = {
ANL_OUTLIER_EXTENDED <- dplyr::left_join(
ANL_OUTLIER,
dplyr::select(
dataname,
dplyr::setdiff(names(dataname), dplyr::setdiff(names(ANL_OUTLIER), join_keys))
),
by = join_keys
)
},
env = list(
dataname = as.name(dataname_first),
join_keys = join_keys
)
},
env = list(
dataname = as.name(dataname_first),
join_keys = as.character(teal.data::join_keys(data())[dataname_first, dataname_first])
)
)
)
}

qenv <- if (length(categorical_var) > 0) {
qenv <- teal.code::eval_code(
Expand Down
50 changes: 49 additions & 1 deletion man/tm_outliers.Rd

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

66 changes: 66 additions & 0 deletions tests/testthat/test-shinytest2-tm_outliers.R
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,69 @@ testthat::test_that("e2e - tm_outliers: Outlier table is displayed with proper c

app_driver$stop()
})

testthat::test_that("e2e - tm_outliers: works without join keys", {
# Test for issue where tm_outliers fails if data has no joining keys
# This test reproduces the exact scenario described in the issue
testthat::skip("chromium")
skip_if_too_deep(5)

# Create data without join keys (as per issue description)
data <- teal.data::teal_data()
data <- within(data, {
CO2 <- CO2 # nolint: object_name
})
# Note: No join_keys defined here, which should NOT cause the issue after fix

vars <- teal.transform::choices_selected(
teal.transform::variable_choices(
data[["CO2"]],
c("Plant", "Type", "Treatment")
)
)

# This should NOT fail after the fix
app_driver <- init_teal_app_driver(
data = data,
modules = tm_outliers(
outlier_var = list(
teal.transform::data_extract_spec(
dataname = "CO2",
select = teal.transform::select_spec(
label = "Select variable:",
choices = teal.transform::variable_choices(data[["CO2"]], c("conc", "uptake")),
selected = "uptake",
multiple = FALSE,
fixed = FALSE
)
)
),
categorical_var = list(
teal.transform::data_extract_spec(
dataname = "CO2",
filter = teal.transform::filter_spec(
vars = vars,
choices = teal.transform::value_choices(data[["CO2"]], vars$selected),
selected = teal.transform::value_choices(data[["CO2"]], vars$selected),
multiple = TRUE
)
)
),
ggplot2_args = list(
teal.widgets::ggplot2_args(
labs = list(subtitle = "Plot generated by Outliers Module")
)
)
)
)

app_driver$expect_no_shiny_error()

# Verify the module loads correctly
testthat::expect_equal(
app_driver$get_text("#teal-teal_modules-active_tab .active"),
"Outliers Module"
)

app_driver$stop()
})
Loading