Skip to content

Commit 2fd5402

Browse files
committed
docs: vignette cleanup
1 parent 51b0964 commit 2fd5402

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

vignettes/decorate-module-output.Rmd

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ We modify the title and x-axis label of plot:
6161
```{r}
6262
library(teal)
6363
static_decorator <- teal_transform_module(
64+
label = "Static decorator",
6465
server = function(id, data) {
6566
moduleServer(id, function(input, output, session) {
6667
reactive({
@@ -82,6 +83,7 @@ you can use the [`make_teal_transform_server()`](https://insightsengineering.git
8283

8384
```{r}
8485
static_decorator_lang <- teal_transform_module(
86+
label = "Static decorator (language)",
8587
server = make_teal_transform_server(
8688
expression(
8789
plot <- plot +
@@ -100,6 +102,7 @@ Note how the input parameters are passed to the [`within`](https://insightsengin
100102

101103
```{r}
102104
interactive_decorator <- teal_transform_module(
105+
label = "Interactive decorator",
103106
ui = function(id) {
104107
ns <- NS(id)
105108
div(
@@ -129,6 +132,7 @@ This wrapper requires you to use `input` object names directly in the expression
129132

130133
```{r}
131134
interactive_decorator_lang <- teal_transform_module(
135+
label = "Interactive decorator (language)",
132136
ui = function(id) {
133137
ns <- NS(id)
134138
div(
@@ -155,6 +159,7 @@ In the following example, focus on the `output_name` parameter to see how decora
155159
```{r}
156160
gg_xlab_decorator <- function(output_name) {
157161
teal_transform_module(
162+
label = "X-axis decorator",
158163
ui = function(id) {
159164
ns <- NS(id)
160165
div(
@@ -186,6 +191,7 @@ If a decorator fails, the outputs will not be shown, and an appropriate error me
186191

187192
```{r}
188193
failing_decorator <- teal_transform_module(
194+
label = "Failing decorator",
189195
ui = function(id) {
190196
ns <- NS(id)
191197
div(
@@ -194,7 +200,7 @@ failing_decorator <- teal_transform_module(
194200
},
195201
server = function(id, data) {
196202
moduleServer(id, function(input, output, session) {
197-
reactive(stop("This is error"))
203+
reactive(stop("\nThis is an error produced by decorator\n"))
198204
})
199205
}
200206
)
@@ -357,8 +363,9 @@ tm_decorated_plot <- function(label = "module", decorators = NULL) {
357363
updateSelectInput(inputId = "dataname", choices = names(data()))
358364
})
359365
360-
observeEvent(input$dataname, {
361-
req(input$dataname)
366+
dataname <- reactive(req(input$dataname))
367+
368+
observeEvent(dataname(), {
362369
updateSelectInput(inputId = "x", choices = colnames(data()[[input$dataname]]))
363370
updateSelectInput(inputId = "y", choices = colnames(data()[[input$dataname]]))
364371
})
@@ -374,7 +381,6 @@ tm_decorated_plot <- function(label = "module", decorators = NULL) {
374381
})
375382
})
376383
377-
dataname <- reactive(req(input$dataname))
378384
x <- reactive({
379385
req(input$x, input$x %in% colnames(data()[[dataname()]]))
380386
input$x
@@ -403,7 +409,7 @@ tm_decorated_plot <- function(label = "module", decorators = NULL) {
403409
})
404410
405411
decorated_data_no_print <- srv_transform_teal_data(
406-
paste0("decorate_", selected_decorator()),
412+
sprintf("decorate_%s", selected_decorator()),
407413
data = plot_data,
408414
transformators = decorators[[selected_decorator()]]
409415
)
@@ -430,6 +436,7 @@ By order of the decorator we will:
430436

431437
```{r}
432438
interactive_decorator_1 <- teal_transform_module(
439+
label = "Interactive decorator 1",
433440
ui = function(id) {
434441
ns <- NS(id)
435442
div(
@@ -453,6 +460,7 @@ interactive_decorator_1 <- teal_transform_module(
453460
)
454461
455462
interactive_decorator_2 <- teal_transform_module(
463+
label = "Interactive decorator 2",
456464
ui = function(id) {
457465
ns <- NS(id)
458466
div(
@@ -476,6 +484,7 @@ interactive_decorator_2 <- teal_transform_module(
476484
)
477485
478486
interactive_decorator_3 <- teal_transform_module(
487+
label = "Interactive decorator 3",
479488
ui = function(id) {
480489
ns <- NS(id)
481490
div(
@@ -540,7 +549,7 @@ The following module generates both a scatter plot and a summary table. Each of
540549
```{r}
541550
tm_decorated_plot_table <- function(label = "module with two outputs", decorators = list()) {
542551
checkmate::assert_list(decorators, "teal_transform_module", null.ok = TRUE)
543-
552+
544553
module(
545554
label = label,
546555
ui = function(id, decorators) {
@@ -562,13 +571,13 @@ tm_decorated_plot_table <- function(label = "module with two outputs", decorator
562571
updateSelectInput(inputId = "dataname", choices = names(data()))
563572
})
564573
565-
observeEvent(input$dataname, {
566-
req(input$dataname)
574+
dataname <- reactive(req(input$dataname))
575+
576+
observeEvent(dataname(), {
567577
updateSelectInput(inputId = "x", choices = colnames(data()[[input$dataname]]))
568578
updateSelectInput(inputId = "y", choices = colnames(data()[[input$dataname]]))
569579
})
570580
571-
dataname <- reactive(req(input$dataname))
572581
x <- reactive({
573582
req(input$x, input$x %in% colnames(data()[[dataname()]]))
574583
input$x
@@ -597,11 +606,11 @@ tm_decorated_plot_table <- function(label = "module with two outputs", decorator
597606
table_data <- reactive({
598607
req(dataname())
599608
within(data(),
600-
{
601-
table_data <- dataname %>%
602-
dplyr::summarise(dplyr::across(dplyr::everything(), mean, na.rm = TRUE))
603-
},
604-
dataname = as.name(dataname())
609+
{
610+
table_data <- dataname %>%
611+
dplyr::summarise(dplyr::across(dplyr::everything(), mean, na.rm = TRUE))
612+
},
613+
dataname = as.name(dataname())
605614
)
606615
})
607616
@@ -619,28 +628,21 @@ tm_decorated_plot_table <- function(label = "module with two outputs", decorator
619628
transformators = decorators$table
620629
)
621630
622-
output$plot <- renderPlot({
623-
req(decorated_plot())
624-
decorated_plot()[["plot"]]
625-
})
631+
output$plot <- renderPlot(decorated_plot()[["plot"]])
626632
627-
output$table <- renderTable({
628-
req(decorated_table())
629-
decorated_table()[["table_data"]]
630-
})
633+
output$table <- renderTable(decorated_table()[["table_data"]])
631634
632635
output$text <- renderText({
633636
plot_code <- teal.code::get_code(req(decorated_plot()))
634637
table_code <- teal.code::get_code(req(decorated_table()))
635-
paste("Plot Code:", plot_code, "\nTable Code:", table_code)
638+
paste("# Plot Code:", plot_code, "\n\n# Table Code:", table_code)
636639
})
637640
})
638641
},
639642
ui_args = list(decorators = decorators),
640643
server_args = list(decorators = decorators)
641644
)
642645
}
643-
644646
```
645647

646648

@@ -659,11 +661,10 @@ plot_decorator <- teal_transform_module(
659661
reactive({
660662
req(data())
661663
within(data(),
662-
{
663-
plot <- plot +
664-
ggplot2::ggtitle(ptitle)
665-
},
666-
ptitle = input$plot_title
664+
{
665+
plot <- plot + ggplot2::ggtitle(ptitle)
666+
},
667+
ptitle = input$plot_title
667668
)
668669
})
669670
})
@@ -676,13 +677,15 @@ plot_decorator <- teal_transform_module(
676677

677678
```{r}
678679
table_decorator <- teal_transform_module(
680+
label = "Decorate plot",
679681
server = make_teal_transform_server(
680682
expression({
681-
rownames(table_data) <- paste0("Row ", seq_len(nrow(table_data)))
683+
table_data$id <- paste0("Row ", seq_len(nrow(table_data)))
684+
table_data <- table_data[, union("id", colnames(table_data))]
685+
colnames(table_data)[1] <- "ID (decorated)"
682686
})
683687
)
684688
)
685-
686689
```
687690

688691

@@ -705,5 +708,4 @@ app <- init(
705708
if (interactive()) {
706709
shinyApp(app$ui, app$server)
707710
}
708-
709711
```

0 commit comments

Comments
 (0)