Skip to content

Commit

Permalink
webr-alone
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelru committed Apr 24, 2024
1 parent 55fb68a commit f55b85b
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 15 deletions.
1 change: 1 addition & 0 deletions book/_quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ webr:
repos:
- https://insightsengineering.r-universe.dev/
show-startup-message: false
autoload-packages: false

filters:
- insightsengineering/pattern-strip
Expand Down
46 changes: 46 additions & 0 deletions book/shinylive-alone.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!--
This shortcode will include chunk returning WebR instance.
Global shinylive configuration is available in the `_quarto.yaml` file.
-->

::: {.callout-warning appearance="simple"}
## Experimental use!
:::

Modify the code below and run it to see the results.

<!--
we have to put some code upfront
- need to setup repos on r-universe - currently it's not possible to setup this elsewhere (e.g. shinylive config or chunk attribute)
- currently shinylive is using [email protected] which is missing a feature of shimming `library()` calls - need to add this manually
-->

```{r shinylive-constructor, echo=FALSE, results='asis'}
repo_url <- ifelse(identical(Sys.getenv("QUARTO_PROFILE"), "stable"), "https://insightsengineering.r-universe.dev", "https://pharmaverse.r-universe.dev")
text <- unlist(c(
"```{shinylive-r}",
"#| standalone: true",
"#| viewerHeight: 800",
"#| components: [viewer, editor]",
"#| layout: vertical",
"",
"# -- WEBR HELPERS --",
sprintf("options(webr_pkg_repos = c(\"r-universe\" = \"%s\", getOption(\"webr_pkg_repos\")))", repo_url),
"if (packageVersion(\"webr\") < \"0.3.0\") {",
" .e <- as.environment(\"webr_shims\")",
" .e[[\"library\"]] <- function(pkg, ...) {",
" package <- as.character(substitute(pkg))",
" if (length(find.package(package, quiet = TRUE)) == 0) {",
" webr::install(package)",
" }",
" base::library(package, character.only = TRUE, ...)",
" }",
"}",
"",
"# -- APP CODE --",
knitr::knit_code$get(c("teal")),
"```"
))
cat(text, sep = "\n")
```
73 changes: 58 additions & 15 deletions book/tables/adverse-events/aet02.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ subtitle: Adverse Events

{{< include ../../test-utils/envir_hook.qmd >}}

::: panel-tabset
:::: panel-tabset
## Data Setup

```{r setup, message = FALSE}
Expand Down Expand Up @@ -35,7 +35,7 @@ split_fun <- drop_split_levels

## Standard Table

```{r variant1, test = list(result_v1 = "result")}
```{r variant1, test = list(result_v1 = "result"), include = FALSE}
lyt <- basic_table(show_colcounts = TRUE) %>%
split_cols_by(var = "ACTARM") %>%
add_overall_col(label = "All Patients") %>%
Expand Down Expand Up @@ -72,11 +72,14 @@ lyt <- basic_table(show_colcounts = TRUE) %>%
result <- build_table(lyt, df = adae, alt_counts_df = adsl)
```

`r webr_code_labels <- c("setup", "variant1")`
{{< include ../../webr-alone.qmd >}}

The variable `result` corresponds to the adverse events table.
However, it includes many empty rows accounting for events which were not reported.
The table can be post-processed to prune empty rows and to sort rows, for example by occurrence.

```{r variant2, test = list(result_v2 = "result")}
```{r variant2, test = list(result_v2 = "result"), include = FALSE}
result <- result %>%
prune_table() %>%
sort_at_path(
Expand All @@ -91,9 +94,12 @@ result <- result %>%
result
```

`r webr_code_labels <- c("setup", "variant1", "variant2")`
{{< include ../../webr-alone.qmd >}}

## Table with <br/> Event Totals

```{r variant3, test = list(result_v3 = "result")}
```{r variant3, test = list(result_v3 = "result"), include = FALSE}
lyt <- basic_table(show_colcounts = TRUE) %>%
split_cols_by(var = "ACTARM") %>%
add_overall_col(label = "All Patients") %>%
Expand Down Expand Up @@ -144,9 +150,12 @@ result <- build_table(lyt, df = adae, alt_counts_df = adsl) %>%
result
```

`r webr_code_labels <- c("setup", "variant3")`
{{< include ../../webr-alone.qmd >}}

## Table with <br/> High-Level Term

```{r variant4, test = list(result_v4 = "result")}
```{r variant4, test = list(result_v4 = "result"), include = FALSE}
lyt <- basic_table(show_colcounts = TRUE) %>%
split_cols_by(var = "ACTARM") %>%
analyze_num_patients(
Expand Down Expand Up @@ -211,9 +220,12 @@ result <- build_table(lyt, df = adae, alt_counts_df = adsl) %>%
result
```

`r webr_code_labels <- c("setup", "variant4")`
{{< include ../../webr-alone.qmd >}}

## Table with Preferred <br/> Terms Only

```{r variant5, test = list(result_v5 = "result")}
```{r variant5, test = list(result_v5 = "result"), include = FALSE}
lyt <- basic_table(show_colcounts = TRUE) %>%
split_cols_by(var = "ACTARM") %>%
analyze_num_patients(
Expand All @@ -237,9 +249,12 @@ result <- build_table(lyt, df = adae, alt_counts_df = adsl) %>%
result
```

`r webr_code_labels <- c("setup", "variant5")`
{{< include ../../webr-alone.qmd >}}

## Table with Fill-In of <br/> Treatment Groups

```{r variant6, test = list(result_v6 = "result")}
```{r variant6, test = list(result_v6 = "result"), include = FALSE}
adae_5 <- adae %>% dplyr::filter(ACTARM != "C: Combination")
lyt <- basic_table(show_colcounts = TRUE) %>%
Expand Down Expand Up @@ -285,9 +300,12 @@ result <- build_table(lyt, df = adae_5, alt_counts_df = adsl) %>%
result
```

`r webr_code_labels <- c("setup", "variant6")`
{{< include ../../webr-alone.qmd >}}

## Table of AEs with an Incidence Rate <br/> $\geq$ 5% in Any Treatment Group (subsetting <br/> preferred terms based on frequency)

```{r variant7, test = list(result_v7 = "result")}
```{r variant7, test = list(result_v7 = "result"), include = FALSE}
lyt <- basic_table(show_colcounts = TRUE) %>%
split_cols_by(var = "ACTARM") %>%
split_rows_by(
Expand Down Expand Up @@ -333,9 +351,12 @@ result <- prune_table(result, keep_rows(row_condition))
result
```

`r webr_code_labels <- c("setup", "variant7")`
{{< include ../../webr-alone.qmd >}}

## Table of AEs with an Incidence Rate $\geq$ 5% in <br/> Any Treatment Group (subsetting preferred terms <br/> based on frequency with high-level terms)

```{r variant8, test = list(result_v8 = "result")}
```{r variant8, test = list(result_v8 = "result"), include = FALSE}
lyt <- basic_table(show_colcounts = TRUE) %>%
split_cols_by(var = "ACTARM") %>%
split_rows_by(
Expand Down Expand Up @@ -397,9 +418,12 @@ result <- prune_table(result, keep_rows(row_condition))
result
```

`r webr_code_labels <- c("setup", "variant8")`
{{< include ../../webr-alone.qmd >}}

## Table of AEs with an Incidence Rate $\geq$ 10% <br/> in Any Treatment Group (subsetting <br/> preferred terms based on frequency)

```{r variant9, test = list(result_v9 = "result")}
```{r variant9, test = list(result_v9 = "result"), include = FALSE}
lyt <- basic_table(show_colcounts = TRUE) %>%
split_cols_by(var = "ACTARM") %>%
split_rows_by(
Expand Down Expand Up @@ -445,9 +469,12 @@ result <- prune_table(result, keep_rows(row_condition))
result
```

`r webr_code_labels <- c("setup", "variant9")`
{{< include ../../webr-alone.qmd >}}

## Table of AEs with an Incidence Rate $\geq$ 3 <br/> Patients in Any Treatment Group (subsetting <br/> preferred terms based on number of patients)

```{r variant10, test = list(result_v10 = "result")}
```{r variant10, test = list(result_v10 = "result"), include = FALSE}
lyt <- basic_table(show_colcounts = TRUE) %>%
split_cols_by(var = "ACTARM") %>%
split_rows_by(
Expand Down Expand Up @@ -493,9 +520,12 @@ result <- prune_table(result, keep_rows(row_condition))
result
```

`r webr_code_labels <- c("setup", "variant10")`
{{< include ../../webr-alone.qmd >}}

## Table of AEs with a Difference in Incidence Rate $\geq$ 5% <br/> Between Any Treatment (subsetting preferred terms based <br/> on difference in percentage between treatment groups)

```{r variant11, test = list(result_v11 = "result")}
```{r variant11, test = list(result_v11 = "reresult"), include = FALSE}
lyt <- basic_table(show_colcounts = TRUE) %>%
split_cols_by(var = "ACTARM") %>%
split_rows_by(
Expand Down Expand Up @@ -541,9 +571,12 @@ result <- prune_table(result, keep_rows(row_condition))
result
```

`r webr_code_labels <- c("setup", "variant11")`
{{< include ../../webr-alone.qmd >}}

## Table of AEs with an Incidence Rate $\geq$ 5% <br/> in B: Placebo (subsetting preferred terms based <br/> on frequency for a particular treatment group)

```{r variant12, test = list(result_v12 = "result")}
```{r variant12, test = list(result_v12 = "result"), include = FALSE}
lyt <- basic_table(show_colcounts = TRUE) %>%
split_cols_by(var = "ACTARM") %>%
split_rows_by(
Expand Down Expand Up @@ -589,9 +622,12 @@ result <- prune_table(result, keep_rows(row_condition))
result
```

`r webr_code_labels <- c("setup", "variant12")`
{{< include ../../webr-alone.qmd >}}

## Table of AEs with a Difference in Incidence Rate $\geq$ 5% Between <br/> Arm A and Arm B or Arm C (displaying preferred terms with a <br/> difference of at least x% between selected treatment groups)

```{r variant13, test = list(result_v13 = "result")}
```{r variant13, test = list(result_v13 = "result"), include = FALSE}
lyt <- basic_table(show_colcounts = TRUE) %>%
split_cols_by(var = "ACTARM") %>%
split_rows_by(
Expand Down Expand Up @@ -636,17 +672,22 @@ result <- prune_table(result, keep_rows(row_condition))
result
```

`r webr_code_labels <- c("setup", "variant13")`
{{< include ../../webr-alone.qmd >}}

{{< include ../../test-utils/save_results.qmd >}}

## `teal` App

```{r teal, opts.label = c("skip_if_testing", "app")}
```{r teal, opts.label = c("skip_if_testing", "app"), include = FALSE}
library(teal.modules.clinical)
## Data reproducible code
data <- teal_data()
data <- within(data, {
library(scda)
library(scda.2022)
library(tern)
ADSL <- synthetic_cdisc_dataset("latest", "adsl")
ADAE <- synthetic_cdisc_dataset("latest", "adae")
Expand Down Expand Up @@ -686,5 +727,7 @@ app <- init(
shinyApp(app$ui, app$server)
```

{{< include ../../shinylive-alone.qmd >}}

{{< include ../../repro.qmd >}}
:::
33 changes: 33 additions & 0 deletions book/webr-alone.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!--
This shortcode will include chunk returning WebR instance.
It requires pre-set `webr_code_labels` variable (character vector) containing labels of chunks to be included.
Global WebR configuration is available in the `_quarto.yaml` file.
-->

::: {.callout-warning appearance="simple"}
## Experimental use!
:::

Modify the code below and run it to see the results. Note that this includes also the "Data Setup" step from previous tab to make the code fully executable.

```{r, echo=FALSE, results='asis'}
if (!exists("webr_code_labels")) {
webr_code_labels <- character(0L)
}
if (!all(webr_code_labels %in% knitr::all_labels())) {
stop(sprintf(
"Not all of provided labels exist in the current document!\nNot found labels are: %s.",
paste0(setdiff(webr_code_labels, knitr::all_labels()), collapse = ", ")
))
}
text <- unlist(c(
"```{webr-r}",
"#| editor-max-height: 500",
"#| autorun: true",
lapply(webr_code_labels, knitr::knit_code$get),
"```"
))
cat(text, sep = "\n")
```

0 comments on commit f55b85b

Please sign in to comment.