Skip to contents

[Experimental]

Usage

merge_expression_module(
  datasets,
  join_keys = NULL,
  data_extract,
  merge_function = "dplyr::full_join",
  anl_name = "ANL",
  id = "merge_id"
)

Arguments

datasets

(named list of reactive or non-reactive data.frame)
object containing data as a list of data.frame. When passing a list of non-reactive data.frame objects, they are converted to reactive data.frame objects internally.

join_keys

(JoinKeys)
of variables used as join keys for each of the datasets in datasets. This will be used to extract the keys of every dataset.

data_extract

(named list of data_extract_spec)

merge_function

(character(1))
A character string of a function that accepts the arguments x, y and by to perform the merging of datasets.

anl_name

(character(1))
Name of the analysis dataset.

id

An ID string that corresponds with the ID used to call the module's UI function.

Value

reactive expression with output from merge_expression_srv().

Details

This function is a convenient wrapper to combine data_extract_multiple_srv() and merge_expression_srv() when no additional processing is required. Compare the example below with that found in merge_expression_srv().

Examples

library(shiny)
ADSL <- data.frame(
  STUDYID = "A",
  USUBJID = LETTERS[1:10],
  SEX = rep(c("F", "M"), 5),
  AGE = rpois(10, 30),
  BMRKR1 = rlnorm(10)
)
ADLB <- expand.grid(
  STUDYID = "A",
  USUBJID = LETTERS[1:10],
  PARAMCD = c("ALT", "CRP", "IGA"),
  AVISIT = c("SCREENING", "BASELINE", "WEEK 1 DAY 8", "WEEK 2 DAY 15")
)
ADLB$AVAL <- rlnorm(120)
ADLB$CHG <- rnorm(120)

data_list <- list(
  ADSL = ADSL,
  ADLB = ADLB
)

join_keys <- teal.data::join_keys(
  teal.data::join_key("ADSL", "ADSL", c("STUDYID", "USUBJID")),
  teal.data::join_key("ADSL", "ADLB", c("STUDYID", "USUBJID")),
  teal.data::join_key("ADLB", "ADLB", c("STUDYID", "USUBJID", "PARAMCD", "AVISIT"))
)


adsl_extract <- data_extract_spec(
  dataname = "ADSL",
  select = select_spec(
    label = "Select variable:",
    choices = c("AGE", "BMRKR1"),
    selected = "AGE",
    multiple = TRUE,
    fixed = FALSE
  )
)
adlb_extract <- data_extract_spec(
  dataname = "ADLB",
  filter = filter_spec(vars = "PARAMCD", choices = c("ALT", "CRP", "IGA"), selected = "ALT"),
  select = select_spec(
    label = "Select variable:",
    choices = c("AVAL", "CHG"),
    selected = "AVAL",
    multiple = TRUE,
    fixed = FALSE
  )
)
app <- shinyApp(
  ui = fluidPage(
    teal.widgets::standard_layout(
      output = div(
        verbatimTextOutput("expr"),
        dataTableOutput("data")
      ),
      encoding = tagList(
        data_extract_ui("adsl_var", label = "ADSL selection", adsl_extract),
        data_extract_ui("adlb_var", label = "ADLB selection", adlb_extract)
      )
    )
  ),
  server = function(input, output, session) {
    chunks_h <- teal.code::chunks_new()

    teal.code::chunks_push(
      str2lang("ADSL <- data.frame(
      STUDYID = 'A',
      USUBJID = LETTERS[1:10],
      SEX = rep(c('F', 'M'), 5),
      AGE = rpois(10, 30),
      BMRKR1 = rlnorm(10))"),
      chunks = chunks_h
    )

    teal.code::chunks_push(
      str2lang("ADLB <- expand.grid(
      STUDYID = 'A',
      USUBJID = LETTERS[1:10],
      PARAMCD = c('ALT', 'CRP', 'IGA'),
      AVISIT = c('SCREENING', 'BASELINE', 'WEEK 1 DAY 8', 'WEEK 2 DAY 15'),
      AVAL = rlnorm(120),
      CHG = rlnorm(120))"),
      chunks = chunks_h
    )

    merged_data <- merge_expression_module(
      data_extract = list(adsl_var = adsl_extract, adlb_var = adlb_extract),
      datasets = data_list,
      join_keys = join_keys,
      merge_function = "dplyr::left_join"
    )

    ch_merge <- reactive({
      ch <- teal.code::chunks_deep_clone(chunks_h)
      for (chunk in merged_data()$expr) teal.code::chunks_push(chunks = ch, expression = chunk)
      ch$eval()
      ch
    })

    output$expr <- renderText(paste(merged_data()$expr, collapse = "\n"))
    output$data <- renderDataTable(ch_merge()$get("ANL"))
  }
)
if (FALSE) {
runApp(app)
}