Skip to content

Commit 71556f4

Browse files
committed
docs: push local changes
1 parent e6006d0 commit 71556f4

File tree

1 file changed

+136
-17
lines changed

1 file changed

+136
-17
lines changed

vignettes/decorate-module-output.Rmd

Lines changed: 136 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,155 @@ library(teal.data)
1919
## Introduction
2020

2121
The outputs produced by `teal` modules, like graphs or tables, are created by the module developer and look a certain way.
22-
It is impossible to design an output that will satisfy every possible user, so the form of the output should be considered a default value that can be customized.
23-
Here we describe the concept of _decoration_, enabling you to tailor outputs to your specific requirements without rewriting the original module code.
22+
It is hard to design an output that will satisfy every possible user, so the form of the output should be considered a default value that can be customized.
23+
Here we describe the concept of _decoration_, enabling the app developer to tailor outputs to their specific requirements without rewriting the original module code.
2424

25-
The decoration process is build upon transformation procedures, introduced in `teal`. While transformators are meant to edit module's input, decorators are meant to adjust the module's output. To distinguish the difference, modules in
26-
`teal.modules.general` have 2 separate parameters: `transformators` and `decorators`.
25+
The decoration process is build upon transformation procedures, introduced in `teal`.
26+
While transformators are meant to edit module's input, decorators are meant to adjust the module's output.
27+
To distinguish the difference, modules in `teal.modules.general` have 2 separate parameters:
28+
`transformators` and `decorators`.
2729

28-
To get a fuller understanding, read more about:
30+
To get a complete understanding refer the following vignettes:
2931

30-
- changing the input data
31-
[in this vignette](https://insightsengineering.github.io/teal/latest-tag/articles/transform-input-data.html),
32-
- the concept of changing module output in [this vignette](https://insightsengineering.github.io/teal/latest-tag/articles/transform-input-data.html).
32+
- Transforming the input data in
33+
[this vignette](https://insightsengineering.github.io/teal/latest-tag/articles/transform-input-data.html),
34+
- Transforming module output in [this vignette](https://insightsengineering.github.io/teal/latest-tag/articles/transform-module-output.html).
3335

3436
## Decorators
3537

3638
Decorators are created with `teal_transform_module` and thus they are `shiny` modules.
3739
In `teal.modules.general`, they are passed to `teal` modules through `decorators` argument (see below).
3840
Their server logic will be used to modify objects such as plots or tables that exist in the server function of a module.
39-
An `ui` function can provide interactivity but that is optional. An app developer is free to use decorator modules that do not require user input.
41+
A `ui` function can provide interactivity but that is optional. An app developer is free to use decorator modules that do not require user input.
4042

41-
### Requirements and Limitations
43+
## Decorating a Module
4244

43-
Using decorators requires the following:
45+
In order to decorate/modify the output from a module from `teal.modules.general` you need to refer to the
46+
"Decorating Module" section of the function documentation that tells about the objects that can be decorated.
4447

45-
1. **Matching Object Names**:<br>
46-
Decorators will reference variables that exist in the `teal` module server function and therefore must use the appropriate variable names. The relevant object names available in each module are included in modules' documentations.
47-
2. **Maintaining Object Classes**:<br>
48-
A decorator must not alter the class of the object that it modifies.
49-
This is because a different class may require a different rendering function and that is part of the module structure, which beyond the control of decorators.
48+
In this vignette we will decorate the outputs from the `tm_g_distribution` module.
49+
The [function definition](../reference/tm_g_distribution.html#decorating-module) shows us that there are four output objects that can be decorated:
50+
`histogram_plot` (`ggplot2`), `qq_plot` (`ggplot2`), `summary_table` (`listing_df`) and `test_table` (`listing_df`).
5051

51-
## Building Decorators
52+
### Static Decoration
53+
Here we create a simple decorator that does not provide user.
54+
We will use this to change the title to a static title that we want.
55+
56+
57+
```{r static_decorator}
58+
static_decorator <- teal_transform_module(
59+
label = "Static decorator",
60+
server = function(id, data) {
61+
moduleServer(id, function(input, output, session) {
62+
reactive({
63+
req(data())
64+
within(data(), {
65+
histogram_plot <- histogram_plot +
66+
ggtitle("This is a better title")
67+
})
68+
})
69+
})
70+
}
71+
)
72+
73+
default_decorator <- teal_transform_module(
74+
label = "Static decorator",
75+
ui = function(id) {
76+
tags$div(
77+
textInput(NS(id, "title"), "Title", value = "This is a better title")
78+
)
79+
},
80+
server = function(id, data) {
81+
moduleServer(id, function(input, output, session) {
82+
reactive({
83+
req(data())
84+
within(data(),
85+
{
86+
if (exists("elbow_plot")) {
87+
elbow_plot <- elbow_plot +
88+
ggtitle(title)
89+
}
90+
# if (exists("circle_plot")) {
91+
# circle_plot <- circle_plot +
92+
# ggtitle(title)
93+
# }
94+
# if (exists("biplot")) {
95+
# biplot <- biplot +
96+
# ggtitle(title)
97+
# }
98+
# if (exists("eigenvector_plot")) {
99+
# eigenvector_plot <- eigenvector_plot +
100+
# ggtitle(title)
101+
# }
102+
},
103+
title = input$title
104+
)
105+
})
106+
})
107+
}
108+
)
109+
110+
library(teal.modules.general)
111+
data <- teal_data()
112+
data <- within(data, {
113+
require(nestcolor)
114+
USArrests <- USArrests
115+
})
116+
devtools::load_all()
117+
app <- init(
118+
data = data,
119+
modules = modules(
120+
tm_a_pca(
121+
"PCA",
122+
dat = data_extract_spec(
123+
dataname = "USArrests",
124+
select = select_spec(
125+
choices = variable_choices(
126+
data = data[["USArrests"]], c("Murder", "Assault", "UrbanPop", "Rape")
127+
),
128+
selected = c("Murder", "Assault"),
129+
multiple = TRUE
130+
),
131+
filter = NULL
132+
),
133+
decorators = list(default_decorator)
134+
)
135+
)
136+
)
137+
#> Initializing tm_a_pca
138+
if (interactive()) {
139+
shinyApp(app$ui, app$server)
140+
}
141+
```
142+
143+
144+
```{r}
145+
library(teal.modules.general)
146+
data <- teal_data()
147+
data <- within(data, {
148+
iris <- iris
149+
})
150+
151+
app <- init(
152+
data = data,
153+
modules = list(
154+
tm_g_distribution(
155+
dist_var = data_extract_spec(
156+
dataname = "iris",
157+
select = select_spec(variable_choices("iris"), "Petal.Length")
158+
),
159+
decorators = list(default_decorator)
160+
)
161+
)
162+
)
163+
if (interactive()) {
164+
shinyApp(app$ui, app$server)
165+
}
166+
```
167+
168+
### Dynamic Decoration
169+
170+
### Reusable Variable Decoration
52171

53172
### Server
54173

0 commit comments

Comments
 (0)