insightsengineering/teal.slice

[Bug]: The choices are not sorted for factor and ordered data types

Closed this issue · 0 comments

What happened?

The factor and ordered choices are not sorted anymore. They used to be sorted before.

Screenshot 2024-04-23 at 12 33 08 PM
Sample app to reproduce the error
library(teal)
library(shiny)
library(scda)
devtools::load_all("teal.slice")

ADSL <- synthetic_cdisc_data("latest")$adsl
ADSL$empty <- NA
ADSL$logical1 <- FALSE
ADSL$logical <- sample(c(TRUE, FALSE), size = nrow(ADSL), replace = TRUE)
ADSL$numeric <- rnorm(nrow(ADSL))
ADSL$numeric_categorical <- sample(1:3, size = nrow(ADSL), replace = TRUE)
ADSL$categorical <- sample(letters[1:10], size = nrow(ADSL), replace = TRUE)
ADSL$ordered <- ordered(sample(letters[1:10], size = nrow(ADSL), replace = TRUE))
ADSL$date <- Sys.Date() + seq_len(nrow(ADSL))
ADSL$datetime <- Sys.time() + seq_len(nrow(ADSL)) * 3600 * 12
ADSL$factor <- factor(sample(letters[1:4], size = nrow(ADSL), replace = TRUE))

ADSL$numeric[sample(1:nrow(ADSL), size = 10, )] <- NA
ADSL$numeric[sample(1:nrow(ADSL), size = 10, )] <- Inf
ADSL$numeric[sample(1:nrow(ADSL), size = 10, )] <- NA
ADSL$logical[sample(1:nrow(ADSL), size = 10, )] <- NA
ADSL$date[sample(1:nrow(ADSL), size = 10, )] <- NA
ADSL$datetime[sample(1:nrow(ADSL), size = 10, )] <- NA
ADSL$categorical[sample(1:nrow(ADSL), size = 10, )] <- NA

ADTTE <- synthetic_cdisc_data("latest")$adtte
ADRS <- synthetic_cdisc_data("latest")$adrs


app <- init(
data = teal_data(ADSL = ADSL, ADTTE = ADTTE, ADRS = ADRS),
modules = list(example_module()),
filter = teal_slices(
  teal_slice("ADSL", "empty"),
  teal_slice("ADSL", "logical1"),
  teal_slice("ADSL", "logical"),
  teal_slice("ADSL", "numeric"),
  teal_slice("ADSL", "numeric_categorical"),
  teal_slice("ADSL", "categorical"),
  teal_slice("ADSL", "ordered"),
  teal_slice("ADSL", "factor"),
  teal_slice("ADSL", "date"),
  teal_slice("ADSL", "datetime")
)
)

runApp(app)
pkgload::load_all()

# Factor
data <- data.frame(var = factor(c("small", "medium", "large")))
fstate <- teal.slice:::init_filter_state(
  x = data$var,
  slice = teal_slice(
    dataname = "x",
    varname = "var",
    selected = c("small", "medium"),
  )
)
fstate$.__enclos_env__$private$get_choices()

# Ordered
data <- data.frame(var = ordered(c("small", "medium", "large")))
fstate <- teal.slice:::init_filter_state(
  x = data$var,
  slice = teal_slice(
    dataname = "x",
    varname = "var",
    selected = c("small", "medium"),
  )
)
fstate$.__enclos_env__$private$get_choices()

Expectation is that we will get the choices in a sorted order: "large", "medium", "small"
But, we get: "small", "medium", "large" which is the order observed in the raw data.

sessionInfo()

No response

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct.

Contribution Guidelines

  • I agree to follow this project's Contribution Guidelines.

Security Policy

  • I agree to follow this project's Security Policy.