Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spotfire POC #796

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft

Spotfire POC #796

wants to merge 25 commits into from

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants