--- title: "Data Merge" author: "NEST CoreDev" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Data Merge} %\VignetteEncoding{UTF-8} %\VignetteEngine{knitr::rmarkdown} editor_options: chunk_output_type: console --- ```{r setup, include = FALSE, echo=FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ``` Joining datasets is an essential step when working with relational datasets. To support this, two functions are provided depending on how to process the `data_extract_spec` object: 1. `merge_expression_module` can be used when there is no need to process the list of `data_extract_spec`. This function reads the data and the list of `data_extract_spec` objects and applies the merging. Essentially, it serves as a wrapper that combines `data_extract_multiple_srv()` and `merge_expression_srv()`. 2. `merge_expression_srv` and `data_extract_multiple_srv` can be used in scenarios where additional processing of the list of `data_extract_spec` is necessary or `data_extract_srv()` to customize the `selector_list` input. The following sections provide examples for both scenarios. ### `merge_expression_module` Using `merge_expression_module` alone requires a list of `data_extract_spec` objects for the `data_extract` argument, a list of reactive or non-reactive `data.frame` objects, and a list of join keys corresponding to each `data.frame` object. #### Step 1/5 - Preparing the Data ```{r} library(teal.transform) library(teal.data) library(shiny) # Define data.frame objects ADSL <- rADSL ADTTE <- rADTTE # create a list of reactive data.frame objects datasets <- list( ADSL = reactive(ADSL), ADTTE = reactive(ADTTE) ) # create join_keys join_keys <- join_keys( join_key("ADSL", "ADSL", c("STUDYID", "USUBJID")), join_key("ADSL", "ADTTE", c("STUDYID", "USUBJID")), join_key("ADTTE", "ADTTE", c("STUDYID", "USUBJID", "PARAMCD")) ) ``` #### Step 2/5 - Creating the Data Extracts ```{r} adsl_extract <- data_extract_spec( dataname = "ADSL", select = select_spec( label = "Select variable:", choices = c("AGE", "BMRKR1"), selected = "AGE", multiple = TRUE, fixed = FALSE ) ) adtte_extract <- data_extract_spec( dataname = "ADTTE", select = select_spec( choices = c("AVAL", "ASEQ"), selected = "AVAL", multiple = TRUE, fixed = FALSE ) ) data_extracts <- list(adsl_extract = adsl_extract, adtte_extract = adtte_extract) ``` #### Step 3/5 - Creating the UI ```{r} merge_ui <- function(id, data_extracts) { ns <- NS(id) sidebarLayout( sidebarPanel( h3("Encoding"), tags$div( data_extract_ui( ns("adsl_extract"), # must correspond with data_extracts list names label = "ADSL extract", data_extracts[[1]] ), data_extract_ui( ns("adtte_extract"), # must correspond with data_extracts list names label = "ADTTE extract", data_extracts[[2]] ) ) ), mainPanel( h3("Output"), verbatimTextOutput(ns("expr")), dataTableOutput(ns("data")) ) ) } ``` #### Step 4/5 - Creating the Server Logic ```{r} merge_srv <- function(id, datasets, data_extracts, join_keys) { moduleServer(id, function(input, output, session) { merged_data <- merge_expression_module( data_extract = data_extracts, datasets = datasets, join_keys = join_keys, merge_function = "dplyr::left_join" ) ANL <- reactive({ data_list <- lapply(datasets, function(ds) ds()) eval(envir = list2env(data_list), expr = as.expression(merged_data()$expr)) }) output$expr <- renderText(paste(merged_data()$expr, collapse = "\n")) output$data <- renderDataTable(ANL()) }) } ``` #### Step 5/5 - Creating the `shiny` App ```{r eval=FALSE} shinyApp( ui = fluidPage(merge_ui("data_merge", data_extracts)), server = function(input, output, session) { merge_srv("data_merge", datasets, data_extracts, join_keys) } ) ```