insightsengineering / teal.modules.general

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

`tm_t_crosstable` fails if data has no joining keys #649

Open chlebowa opened 6 months ago

chlebowa commented 6 months ago

Run an app with normal data and select the same variable for both row values and column values.

normal data
library(teal)

data <- teal_data()
data <- within(data, {
  CO2 <- CO2
})
datanames(data) <- "CO2"
app
library(teal.modules.general)
library(teal.widgets)

app <- init(
  data = data,
  modules = modules(
    tm_t_crosstable(
      label = "Cross Table",
      x = data_extract_spec(
        dataname = "CO2",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["CO2"]], c("Plant", "Type", "Treatment")),
          selected = "Plant",
          multiple = TRUE,
          ordered = TRUE,
          fixed = FALSE
        )
      ),
      y = data_extract_spec(
        dataname = "CO2",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["CO2"]], c("Plant", "Type", "Treatment")),
          selected = "Treatment",
          multiple = FALSE,
          fixed = FALSE
        )
      ),
      basic_table_args = basic_table_args(
        subtitles = "Table generated by Crosstable Module"
      )
    )
  )
)
runApp(app, launch.browser = TRUE)
error

image

Now run the app with data that has a primary key added:

data <- teal_data()
data <- within(data, {
  CO2 <- CO2
  CO2[["primary_key"]] <- seq_len(nrow(CO2))
})
datanames(data) <- "CO2"
join_keys(data) <- join_keys(join_key("CO2", "CO2", "primary_key"))

The app now works as intended.

kumamiao commented 3 months ago

also check if other modules have this error

chlebowa commented 3 months ago

also check if other modules have this error

Already have. Note that all modules have general and CDISC data examples now, I logged all erors like this during https://github.com/insightsengineering/teal.modules.general/pull/651