insightsengineering / teal

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

errors from teal.code::init_chunks #726

Closed KaiHTL closed 2 years ago

KaiHTL commented 2 years ago

What is your question?

When I call function teal.code::init_chunks() inside a server function, I got below errors,

Warning: Error in [[: wrong arguments for subsetting an environment 48: teal.code::init_chunks 47: server [...xxx.R#104] 1: runApp Error in *tmp*[[session$ns(character(0))]] : wrong arguments for subsetting an environment

do you have any ideas about why I can't initialize a chunks inside a server function? Thank you in advance!

Below is my code,

library(tidyverse)
library(shiny)
library(teal.slice)
library(teal.widgets)
library(teal.data)
library(tern)

adsl <- scda::synthetic_cdisc_data("latest")$adsl %>% 
  select(STUDYID, USUBJID, AGE, AGEU, SEX, ARM, ARMCD, TRT01A, TRT01P)

adae <- scda::synthetic_cdisc_data("latest")$adae %>% 
  select(STUDYID, USUBJID, AETERM, AEDECOD, AEBODSYS, ASTDTM, AENDTM, AESEQ, TRT01A, TRT01P)

datasets <- teal.slice::init_filtered_data(
  teal.data::cdisc_data(
    teal.data::cdisc_dataset('ADSL', adsl),
    teal.data::cdisc_dataset('ADAE', adae),
    join_keys = join_keys(
      join_key("ADSL",
               "ADAE",
               c("STUDYID" = "STUDYID", "USUBJID" = "USUBJID"))
    ),
    code = 'adsl <- scda::synthetic_cdisc_data("latest")$adsl
            adae <- scda::synthetic_cdisc_data("latest")$adae'
  )
)

ui <- fluidPage(
  title = 'This is for test',
  fluidRow(
    column(width = 9, 
           sidebarPanel(
      optionalSelectInput('trt', 'Treatment group', 
                          c('TRT01P', 'TRT01A'), selected = c('TRT01P'))
      #teal::get_rcode_ui("rcode")
    ),
    mainPanel(
      table_with_settings_ui(id = "table1"),
      shiny::div(verbatimTextOutput("code1"))
    )),

    column(width = 3, datasets$ui_filter_panel("filter_panel"))
  )
)

server <-  function(input, output, session) {

  datasets$srv_filter_panel("filter_panel",
                            active_datanames = function() {
                              c("ADSL", "ADAE")
                            })

  teal.code::init_chunks()

  table_r <- reactive({

    adsl <- datasets$get_data(dataname = "ADSL", filtered = TRUE)
    adae <- datasets$get_data(dataname = "ADAE", filtered = TRUE)

    teal.code::chunks_push(
      rlang::expr({
        final <- basic_table() %>% 
          split_cols_by(!!input$trt) %>% 
          add_colcounts() %>%
          add_overall_col(label = "All Patients") %>%
          summarize_num_patients(var = "USUBJID", 
                                 .stats = "unique", 
                                 .labels = c(unique = "Total number of patients with at least one AE")) %>%
          split_rows_by('AEBODSYS', 
                        child_labels = "visible",
                        labels_var = 'AEBODSYS', 
                        indent_mod = -1L) %>%
          summarize_num_patients(var = "USUBJID",
                                 .stats = "unique",
                                 .labels = "N") %>% 
          count_occurrences(vars = c('AEDECOD')) %>% 
          build_table(df = adae, alt_counts_df = adsl)

        final
      }),
      id = 'test'
    )

    teal.code::chunks_safe_eval()

  })

  table_with_settings_srv(id = "table1", table_r = table_r)

}

shinyApp(ui, server)

Code of Conduct

Contribution Guidelines

Security Policy

nikolas-burkoff commented 2 years ago

Hi @KaiHTL - this is actually a known issue with chunks - see here which also shows a workaround to make it work.

KaiHTL commented 2 years ago

@nikolas-burkoff Thanks!