Skip to content

Commit

Permalink
docs: reorder the example code
Browse files Browse the repository at this point in the history
  • Loading branch information
vedhav committed Feb 16, 2024
1 parent 6bd5081 commit e08d752
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 91 deletions.
53 changes: 27 additions & 26 deletions vignettes/data-extract-merge.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,37 @@ Note that primary key columns are maintained when selecting columns from dataset

Let's see how to achieve this dynamic `select`, `filter`, and `merge` operations in a `shiny` app using `teal.transform`.

#### Step 1/5 - Creating data extract specifications

In the following code block, we create a `data_extract_spec` object for each dataset, as illustrated above.
#### Step 1/5 - Preparing the Data

```{r}
library(teal.transform)
library(teal.data)
library(shiny)
# Define data.frame objects
ADSL <- teal.transform::rADSL
ADTTE <- teal.transform::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 data extract specifications

In the following code block, we create a `data_extract_spec` object for each dataset, as illustrated above.

```{r}
adsl_extract <- data_extract_spec(
dataname = "ADSL",
select = select_spec(
Expand Down Expand Up @@ -70,7 +92,7 @@ adtte_extract <- data_extract_spec(
data_extracts <- list(adsl_extract = adsl_extract, adtte_extract = adtte_extract)
```

#### Step 2/5 - Creating the UI
#### Step 3/5 - Creating the UI

Here, we define the `merge_ui` function, which will be used to create the UI components for the `shiny` app.

Expand Down Expand Up @@ -104,7 +126,7 @@ merge_ui <- function(id, data_extracts) {
}
```

#### Step 3/5 - Creating the Server Logic
#### Step 4/5 - Creating the Server Logic

Here, we define the `merge_srv` function, which will be used to create the server logic for the `shiny` app.

Expand Down Expand Up @@ -133,27 +155,6 @@ merge_srv <- function(id, datasets, data_extracts, join_keys) {
}
```

#### Step 4/5 - Preparing the Data

```{r}
# Define data.frame objects
ADSL <- teal.transform::rADSL
ADTTE <- teal.transform::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 5/5 - Creating the `shiny` App

Finally, we include `merge_ui` and `merge_srv` in the UI and server components of the `shinyApp`, respectively,
Expand Down
75 changes: 37 additions & 38 deletions vignettes/data-extract.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -30,49 +30,13 @@ The role of `data_extract_spec` is twofold: to create a UI component in a `shiny
from the UI to the module itself.
Let's delve into how it fulfills both of these responsibilities.

#### Step 1/4 - Creating the `shiny` UI and Server Modules

To demonstrate different initialization options of `data_extract_spec`, let's first define a `shiny` module that
utilizes `data_extract_ui` and `data_extract_srv` to handle `data_extract_spec` objects.
This module creates a UI component for a single `data_extract_spec` and prints a list of values returned from the `data_extract_srv` module.
For more information about `data_extract_ui` and `data_extract_srv`, please refer to the package documentation.
#### Step 1/4 - Preparing the Data

```{r}
library(teal.transform)
library(teal.data)
library(shiny)
extract_ui <- function(id, data_extract) {
ns <- NS(id)
sidebarLayout(
sidebarPanel(
h3("Encoding"),
data_extract_ui(ns("data_extract"), label = "variable", data_extract)
),
mainPanel(
h3("Output"),
verbatimTextOutput(ns("output"))
)
)
}
extract_srv <- function(id, datasets, data_extract, join_keys) {
moduleServer(id, function(input, output, session) {
reactive_extract_input <- data_extract_srv("data_extract", datasets, data_extract, join_keys)
s <- reactive({
format_data_extract(reactive_extract_input())
})
output$output <- renderPrint({
cat(s())
})
})
}
```


#### Step 2/4 - Preparing the Data

```{r}
# Define data.frame objects
ADSL <- teal.transform::rADSL
ADTTE <- teal.transform::rADTTE
Expand All @@ -90,7 +54,7 @@ join_keys <- join_keys(
)
```

#### Step 3/4 - Creating a `data_extract_spec` Object
#### Step 2/4 - Creating a `data_extract_spec` Object

Consider the following example, where we create two UI elements, one to filter on a specific level from `SEX` variable,
and a second one to select a variable from `c("BMRKR1", "AGE")`.
Expand All @@ -104,6 +68,41 @@ simple_des <- data_extract_spec(
)
```

#### Step 3/4 - Creating the `shiny` UI and Server Modules

To demonstrate different initialization options of `data_extract_spec`, let's first define a `shiny` module that
utilizes `data_extract_ui` and `data_extract_srv` to handle `data_extract_spec` objects.
This module creates a UI component for a single `data_extract_spec` and prints a list of values returned from the `data_extract_srv` module.
For more information about `data_extract_ui` and `data_extract_srv`, please refer to the package documentation.

```{r}
extract_ui <- function(id, data_extract) {
ns <- NS(id)
sidebarLayout(
sidebarPanel(
h3("Encoding"),
data_extract_ui(ns("data_extract"), label = "variable", data_extract)
),
mainPanel(
h3("Output"),
verbatimTextOutput(ns("output"))
)
)
}
extract_srv <- function(id, datasets, data_extract, join_keys) {
moduleServer(id, function(input, output, session) {
reactive_extract_input <- data_extract_srv("data_extract", datasets, data_extract, join_keys)
s <- reactive({
format_data_extract(reactive_extract_input())
})
output$output <- renderPrint({
cat(s())
})
})
}
```

#### Step 4/4 - Creating the `shiny` App

Finally, we include `extract_ui` in the UI of the `shinyApp`, and utilize `extract_srv` in the server function of the `shinyApp`:
Expand Down
56 changes: 29 additions & 27 deletions vignettes/data-merge.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ knitr::opts_chunk$set(

Combining datasets is an essential step when working with relational datasets.
Within the context of `teal`, we use the term "merge" to refer to the process of combining datasets.
To support this, two functions are provided:

1. `merge_expression_module` can be used when there is no need to process the `data_extract` list.
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 `data_extract` list is necessary or `data_extract_srv()` to customize the `selector_list` input.
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.

Expand All @@ -33,13 +34,35 @@ The following sections provide examples for both scenarios.
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 - Creating the Data Extracts
#### Step 1/5 - Preparing the Data

```{r}
library(teal.transform)
library(teal.data)
library(shiny)
# Define data.frame objects
ADSL <- teal.transform::rADSL
ADTTE <- teal.transform::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(
Expand All @@ -64,7 +87,7 @@ adtte_extract <- data_extract_spec(
data_extracts <- list(adsl_extract = adsl_extract, adtte_extract = adtte_extract)
```

#### Step 2/5 - Creating the UI
#### Step 3/5 - Creating the UI

```{r}
merge_ui <- function(id, data_extracts) {
Expand Down Expand Up @@ -94,7 +117,7 @@ merge_ui <- function(id, data_extracts) {
}
```

#### Step 3/5 - Creating the Server Logic
#### Step 4/5 - Creating the Server Logic

```{r}
merge_srv <- function(id, datasets, data_extracts, join_keys) {
Expand All @@ -116,27 +139,6 @@ merge_srv <- function(id, datasets, data_extracts, join_keys) {
}
```

#### Step 4/5 - Preparing the Data

```{r}
# Define data.frame objects
ADSL <- teal.transform::rADSL
ADTTE <- teal.transform::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 5/5 - Creating the `shiny` App

```{r eval=FALSE}
Expand Down

0 comments on commit e08d752

Please sign in to comment.