insightsengineering / teal.modules.general

General Purpose Teal Modules
https://insightsengineering.github.io/teal.modules.general/
Other
9 stars 13 forks source link

[Bug]: tm_outliers module - not able to use categorical_var #600

Closed Mia-data closed 11 months ago

Mia-data commented 11 months ago

What is your question?

Hi,

In the "tm_outliers" module I'm not able to add "categorical_var" parameter, because if I do there is an error in the app "no data loaded". I looked at the example https://insightsengineering.github.io/teal.modules.general/latest-tag/articles/usingoutliers-module.html but it does not help to understand an issue. Could you advise what is wrong? Below a reproducible example using iris data

library(teal.modules.general)
library(dplyr)
library(tidyr)
options(shiny.useragg = FALSE)

# prepare data
iris2 <- iris %>%
  mutate(
    grp_slength = cut(Sepal.Length, breaks = c(0, 5, 8, Inf)),
    grp_swidth = cut(Sepal.Width, breaks = c(0, 1, 2, 3, 4, Inf)),
    petal_color = as.factor(rep(c("orange", "blue", "magenta"), 50))
  )

# App
app <- init(
  data = teal_data(
    dataset("iris2", iris2)
  ),
  modules = modules(
    tm_data_table("Data Table"),
    tm_outliers(
      "Outliers",
      outlier_var = data_extract_spec(
        dataname = "iris2",
        select = select_spec(
          choices = variable_choices(iris2, subset = names(Filter(isTRUE, sapply(iris2, is.numeric)))),
          selected = "Sepal.Length",
          multiple = FALSE,
          fixed = FALSE
        )
      ),
      categorical_var = data_extract_spec(
        dataname = "iris2",
        select = select_spec(
          choices = variable_choices(iris2, subset = names(Filter(isTRUE, sapply(iris, is.factor)))),
          selected = "Species",
          multiple = FALSE,
          fixed = FALSE
        )
      )
    )
  ),
  header = "Reproducible example"
)
shinyApp(app$ui, app$server)

Code of Conduct

Contribution Guidelines

Security Policy

pawelru commented 11 months ago

Thank you for the report. It's a bug and I have found the root couse of this. We need to fix and release. Until this time please use the workaround of artificial "id" column that is a dataset key:

iris2 <- iris %>%
    mutate(
        grp_slength = cut(Sepal.Length, breaks = c(0, 5, 8, Inf)),
        grp_swidth = cut(Sepal.Width, breaks = c(0, 1, 2, 3, 4, Inf)),
        petal_color = as.factor(rep(c("orange", "blue", "magenta"), 50)),
        id = row_number()
    )

(...)

app <- init(
    data = teal_data(
        dataset("iris2", iris2, key = "id")
    ),
(...)
pawelru commented 11 months ago

The issue is here:

https://github.com/insightsengineering/teal.modules.general/blob/eef36c1dda918fcbebe948d8bef189bd7e545974/R/tm_outliers.R#L330-L340

Reprex example:

ANL <- iris[, c("Sepal.Length", "Species")]
categorical_var <- "Species"
teal::validate_has_data(ANL[, names(ANL) != categorical_var])
r$> ANL <- iris[, c("Sepal.Length", "Species")]
    categorical_var <- "Species"
    teal::validate_has_data(ANL[, names(ANL) != categorical_var])
Error: No data left.

Fix proposal: