insightsengineering / teal

Exploratory Web Apps for Analyzing Clinical Trial Data
https://insightsengineering.github.io/teal/
Other
176 stars 35 forks source link

setting primary keys in teal v0.15.0 #1164

Closed Mia-data closed 6 months ago

Mia-data commented 6 months ago

Hi,

I'm trying to translate my teal app based on teal version < v.0.14 to the latest release v.0.15. The problem is with setting primary and foreign keys. I've read the materials + join key vignitte https://insightsengineering.github.io/teal.data/latest-tag/articles/join-keys.html#anatomy-of-join_keys but still I cannot figure out the issue of the app - it still throws an error that "extracted data has not correctly set joining keys"

#I need to simply translate old code to teal migration:
app <- init(
  data = teal_data(
    dataset("x", x,  keys=c("id"))
), 
modules=modules(
tm_g_response("Response",
                  response = response2,
                  x = response1,
                  row_facet = NULL,
                  col_facet = data_extract_spec(
                    dataname = "x",
                    filter = filter_spec(
                      vars = vars,
                      choices = value_choices(x, vars$selected),
                      selected = value_choices(x, vars$selected),
                      multiple = T
                    )
                  ),
                  coord_flip = FALSE
    )
  ))
)

#I tried this but it didn't worked in v0.15.0:
data<- within(teal_data(), {
  x=x
  response1=response1
  response2=response2
})

datanames <- c("x", "response1", "resposne2")
datanames(data) <- datanames

join_keys(data) <- join_keys(
  join_key("x", keys = c("id")),
 join_key("response1", keys = c("id")),
 join_key("response2", keys = c("id")),
 join_key("response1", "response2" keys = c("id"="id"))
)
app <- init(data=data,
modules=modules(
tm_g_response("Response",
                  response = response2,
                  x = response1,
                  row_facet = NULL,
                  col_facet = data_extract_spec(
                    dataname = "x",
                    filter = filter_spec(
                      vars = vars,
                      choices = value_choices(x, vars$selected),
                      selected = value_choices(x, vars$selected),
                      multiple = T
                    )
                  ),
                  coord_flip = FALSE
    )
  )
)

#I would appreciate your help

Originally posted by @Mia-data in https://github.com/insightsengineering/teal/discussions/988#discussioncomment-8817155

chlebowa commented 6 months ago

Can you please post a more complete example of what you are trying to do? What is x? What are response1 and response2? Also, could you copy the error message verbatim? I am unable find the one you quoted.

Mia-data commented 6 months ago

Hi @chlebowa , Hi @gogonzo

When I was preparing R reproducible example for this issue (making the app much simpler), I found out the bug and resolved it.

Maybe for someone it would be a helpful example so let me publish it the debugging results. My issue was with setting correctly primary keys for a dataset "iris2" for the teal v.0.15.0. The primary keys are required for the module "tm_g_response". I solved the issue and an exemplary and correctly working app is below.

Nevertheless, thank you for quick quick reaction -I appreciate it, as it feels that the teal app is actively developed and maintained so it encourage me to further use it for other projects as I know that I may get support here whenever I will be stacked alone on debugging :). This issue can be closed. I have no futher questions :)

# Reproduce example

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

# prepare data
mtcars2<-mtcars

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())

extracted_response_fct <- data_extract_spec(
  dataname = "iris2",
  select = select_spec(
    choices = variable_choices(iris2, c("grp_slength", "grp_swidth")),
    selected = "grp_slength",
    multiple = FALSE,
    fixed = FALSE
  )
)

extracted_fct  <-  data_extract_spec(
  dataname = "iris2",
  select = select_spec(
    choices = variable_choices(iris2, c("petal_color")),
    selected = "petal_color",
    multiple = FALSE,
    fixed = FALSE
  )
)
extracted_fct2 <- data_extract_spec(
  dataname = "iris2",
  select = select_spec(
    choices = variable_choices(iris2, c("petal_color")),
    selected = "petal_color",
    multiple = FALSE,
    fixed = FALSE
  )
)

# objects needed for tm_g_response for rwo_facet/col_facet
idx <- vapply(iris2, is.factor, logical(1))
cat_fat_choices <- names(iris2)[idx]
vars <- choices_selected(variable_choices(iris2, cat_fat_choices))

# defining primary keys using newest teal version v0.15.0
data<-teal_data(iris2=iris2,
                mtcars2=mtcars2)

datanames(data)<-c("iris2", "mtcars2")

join_keys(data) <- join_keys(
  join_key("iris2", keys = c("id"))
)

# App --------------------------------------------------------------------------
app <- init(
  data = data,
  modules = modules(
    tm_data_table("Data Table"), 
    tm_g_response("Response",
                  response = extracted_response_fct,
                  x = extracted_fct,
                  row_facet = data_extract_spec(
                    dataname = "iris2",
                    filter = filter_spec(
                      vars = vars,
                      choices = value_choices(iris2, vars$selected),
                      selected = value_choices(iris2, vars$selected),
                      multiple = TRUE
                    )
                  ),
                  col_facet = NULL,
                  coord_flip = FALSE
    )
  ),
  header = "Reproducible example"
)
shinyApp(app$ui, app$server)
gogonzo commented 6 months ago

@Mia-data We are glad you made it

Nevertheless, thank you for quick quick reaction -I appreciate it, as it feels that the teal app is actively developed and maintained so it encourage me to further use it for other projects as I know that I may get support here whenever I will be stacked alone on debugging :). This issue can be closed. I have no futher questions :)

This is very encouraging, thank you! Don't hesitate to raise an issue in the future