Skip to content

Conversation

@gogonzo
Copy link
Contributor

@gogonzo gogonzo commented Nov 15, 2024

Fixes https://github.com/insightsengineering/coredev-tasks/issues/596

  • POC example in inst/ directory
  • Install dependencies:
remotes::install_github("insightsengineering/teal.widgets@spotfire_poc2")
remotes::install_github("insightsengineering/teal@spotfire_poc2")
example 1
pkgload::load_all("teal")
pkgload::load_all("teal.widgets")
pkgload::load_all("teal.modules.general")

# Example data
data <- within(teal_data(), {
  library(dplyr)
  library(tidyr)

  set.seed(123) # Setting a seed for reproducibility
  # Define possible maximum study days
  .possible_end_days <- c(50, 60, 70)

  # Create sample data
  synthetic_data <- tibble(subjid = c(1:15), strata = rep(c("category 1", "category 2"), length.out = 15)) |>
    rowwise() |>
    mutate(
      max_study_day = sample(.possible_end_days, 1),
      study_day = list(seq(10, max_study_day, by = 10))
    ) |>
    unnest(study_day) |>
    group_by(subjid) |>
    mutate(
      assigned_drug = sample(c("Drug A", "Drug B"), 1)
    ) |>
    ungroup() |>
    mutate(
      response_type = sample(c("CR", "PR"), n(), replace = TRUE),
      subjid = reorder(as.character(subjid), max_study_day)
    ) |>
    select(-max_study_day)
})

app <- init(
  data = data,
  modules = modules(
    tm_data_table(),
    tm_p_swimlane(
      label = "Swimlane",
      geom_specs = list(
        list(
          geom = str2lang("ggplot2::geom_col"),
          data = quote(synthetic_data),
          mapping = list(y = quote(subjid), x = quote(max(study_day))),
          width = 0.2
        ), # geom_col(data = synthetic_data, mapping = aes(x = subjid, x = max(study_day), width = 0.2)
        list(
          geom = quote(geom_point),
          data = quote(synthetic_data),
          mapping = list(
            y = quote(subjid), x = quote(study_day), color = quote(assigned_drug), shape = quote(assigned_drug)
          )
        ),
        list(
          geom = quote(geom_point),
          data = quote(synthetic_data),
          mapping = list(
            y = quote(subjid), x = quote(study_day), color = quote(response_type), shape = quote(response_type)
          )
        ),
        list(
          geom = quote(facet_wrap),
          facets = quote(vars(strata))
        )
      ),
      title = "Swimlane Efficacy Plot"
    )
  )
)

shinyApp(app$ui, app$server)
example adam
pkgload::load_all("teal")
pkgload::load_all("teal.widgets")
pkgload::load_all("teal.modules.general")

# Example data
data <- within(teal_data(), {
  library(dplyr)
  library(tidyr)
  ADSL <- teal.data::rADSL |> mutate(
    EOTSTT2 = case_when(
      !is.na(DCSREAS) ~ DCSREAS,
      TRUE ~ EOTSTT
    )
  )

  ADAE <- teal.data::rADAE
  ADRS <- teal.data::rADRS
})

join_keys(data) <- default_cdisc_join_keys

app <- init(
  data = data,
  modules = modules(
    tm_data_table(),
    tm_p_swimlane(
      label = "Swimlane",
      geom_specs = list(
        list(
          geom = quote(geom_col),
          data = quote(ADSL),
          mapping = list(y = quote(USUBJID), x = quote(EOSDY)),
          width = 0.2
        ), # geom_col(data = synthetic_data, mapping = aes(x = subjid, x = max(study_day), width = 0.2)
        list(
          geom = quote(geom_point),
          data = quote(ADSL),
          mapping = list(
            y = quote(USUBJID), x = quote(EOSDY), color = quote(EOTSTT2), shape = quote(EOTSTT2)
          )
        ),
        list(
          geom = quote(geom_point),
          data = quote(ADRS),
          mapping = list(
            y = quote(USUBJID), x = quote(ADY), color = quote(PARAMCD), shape = quote(PARAMCD)
          )
        ),
        list(
          geom = quote(geom_point),
          data = quote(ADAE),
          mapping = list(
            y = quote(USUBJID), x = quote(ASTDY), color = quote(AETERM), shape = quote(AETERM)
          )
        ),
        list(
          geom = quote(geom_point),
          data = quote(ADAE),
          mapping = list(
            y = quote(USUBJID), x = quote(AENDY), color = quote(AEOUT), shape = quote(AEOUT)
          )
        ),
        list(
          geom = quote(facet_grid),
          facets = quote(ARM ~ .),
          scales = "free"
        )
      ),
      title = "Swimlane Efficacy Plot"
    )
  )
)

shinyApp(app$ui, app$server)

@gogonzo gogonzo added the core label Nov 15, 2024
@gogonzo gogonzo marked this pull request as draft November 15, 2024 08:45
@gogonzo
Copy link
Contributor Author

gogonzo commented Nov 19, 2024

At this moment we propose a very generic module which could be named tm_p_gglayers. Module can build any ggplot object bases on the list of specs provided through the module arguments. Please consider list below and see that list contain all informations necessary to create a ggplot layer.

list(
  geom = quote(geom_point),
  data = quote(synthetic_data),
  mapping = list(
    y = quote(subjid), x = quote(study_day), color = quote(assigned_drug), shape = quote(assigned_drug)
  )
)

# above is convertible:

geom_point(
  data = synthetic_data,
  mapping = aes(y = subjid, x = study_day, color = assigned_drug, shape = assigned_drug)
)

At this moment I was able to create such applications:

Skärmavbild 2024-11-19 kl  14 45 14 image

@gogonzo
Copy link
Contributor Author

gogonzo commented Mar 5, 2025

Old PR

@gogonzo gogonzo closed this Mar 5, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Mar 5, 2025
@gogonzo gogonzo deleted the spotfire_poc2 branch March 5, 2025 07:06
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants