insightsengineering / teal.modules.general

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

[Bug]: The order of statistics in `tm_outliers` summary table is random #735

Open m7pr opened 7 months ago

m7pr commented 7 months ago

What happened?

The order of statistics in a drop-down menu is as follows

 [1] "Qn1" "Qn2" "Qn3" "Qc1" "Qc2" "Qc3" "Mn1" "Mn2" "Mn3" "Mc1" "Mc2" "Mc3"

but the order that is displayed is a bit random

 Qn1 Qn2 Qn3 Qc1 Qc3 Qc2 Mn3 Mn2 Mn1 Mc2 Mc3 Mc1
image

Code to reproduce

library(teal)
library(teal.modules.general)
app_driver_tm_outlier <- function() {
  data <- teal.data::teal_data()
  data <- within(data, {
    CO2 <- CO2 # nolint: object_name
    CO2[["primary_key"]] <- seq_len(nrow(CO2))
  })
  teal.data::datanames(data) <- "CO2"
  teal.data::join_keys(data) <- teal.data::join_keys(join_key("CO2", "CO2", "primary_key"))

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

  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 <- app_driver_tm_outlier()
app_driver$open_url()

sessionInfo()

No response

Relevant log output

No response

Code of Conduct

Contribution Guidelines

Security Policy

m7pr commented 7 months ago

The order is taken from order of factor levels in the data

> CO2$Plant
 [1] Qn1 Qn1 Qn1 Qn1 Qn1 Qn1 Qn1 Qn2 Qn2
[10] Qn2 Qn2 Qn2 Qn2 Qn2 Qn3 Qn3 Qn3 Qn3
[19] Qn3 Qn3 Qn3 Qc1 Qc1 Qc1 Qc1 Qc1 Qc1
[28] Qc1 Qc2 Qc2 Qc2 Qc2 Qc2 Qc2 Qc2 Qc3
[37] Qc3 Qc3 Qc3 Qc3 Qc3 Qc3 Mn1 Mn1 Mn1
[46] Mn1 Mn1 Mn1 Mn1 Mn2 Mn2 Mn2 Mn2 Mn2
[55] Mn2 Mn2 Mn3 Mn3 Mn3 Mn3 Mn3 Mn3 Mn3
[64] Mc1 Mc1 Mc1 Mc1 Mc1 Mc1 Mc1 Mc2 Mc2
[73] Mc2 Mc2 Mc2 Mc2 Mc2 Mc3 Mc3 Mc3 Mc3
[82] Mc3 Mc3 Mc3
12 Levels: Qn1 < Qn2 < Qn3 < ... < Mc1
> levels(CO2$Plant)
 [1] "Qn1" "Qn2" "Qn3" "Qc1" "Qc3" "Qc2"
 [7] "Mn3" "Mn2" "Mn1" "Mc2" "Mc3" "Mc1"
m7pr commented 7 months ago

will continue the discussion here https://github.com/insightsengineering/teal.transform/issues/213