-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexercise_3.qmd
141 lines (121 loc) · 3.84 KB
/
exercise_3.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
---
title: "Exercise 3"
---
Create your custom module!
Create a custom module that does a simple demographic summary table on user specified columns.
- read [`teal::module()`](https://insightsengineering.github.io/teal/latest-tag/reference/teal_modules.html) documentation
- read ["Creating Custom Modules"](https://insightsengineering.github.io/teal/latest-tag/articles/creating-custom-modules.html) vignette
- read [`"qenv"`](https://insightsengineering.github.io/teal.code/latest-tag/articles/qenv.html) article on how to interact with internal `qenv` object - in particular: [`teal.code::within()`](https://insightsengineering.github.io/teal.code/latest-tag/reference/qenv.html) function
### Code
```{r ex, message = FALSE}
library(random.cdisc.data)
library(teal)
library(teal.transform)
library(teal.widgets)
library(tern)
library(dplyr)
modules <- list(
module(
label = "Adhoc module",
server = function(id, data){
moduleServer(id, function(input, output, session){
s_summary <- function(x) {
if (is.numeric(x)) {
in_rows(
"n" = rcell(sum(!is.na(x)), format = "xx"),
"Mean (sd)" = rcell(c(mean(x, na.rm = TRUE), sd(x, na.rm = TRUE)), format = "xx.xx (xx.xx)"),
"IQR" = rcell(IQR(x, na.rm = TRUE), format = "xx.xx"),
"min - max" = rcell(range(x, na.rm = TRUE), format = "xx.xx - xx.xx")
)
} else if (is.factor(x)) {
vs <- as.list(table(x))
do.call(in_rows, lapply(vs, rcell, format = "xx"))
} else {
stop("type not supported")
}
}
observe({
ADSL <- get_var(data(), "ADSL")
req(ADSL)
updateSelectInput(
inputId = "param",
choices = variable_choices(ADSL),
selected = c("AGE"),
)
})
table_q <- reactive({
data() |>
within(
{
s_summary <- my_summary
summary_lyt <- basic_table() %>%
split_cols_by(var = "ARM") %>%
analyze(param, afun = s_summary)
summary_tbl <- build_table(summary_lyt, ADSL)
summary_tbl
},
my_summary = s_summary,
param = input$param
)
})
output$table = renderUI({
renderPrint(table_q()[["summary_tbl"]])
})
})
},
ui = function(id) {
ns <- NS(id)
standard_layout(
output = div(
fluidRow(
column(
width = 12,
br(), hr(),
uiOutput(ns("table"))
)
)
),
encoding = div(
br(),
tags$label('Encodings', class = 'text-primary'),
helpText('Analysis Data:', tags$code('ADSL')),
selectInput(
inputId = ns('param'),
label = 'Demographic Parameter',
choices = NULL,
selected = NULL,
multiple = TRUE
)
)
)
},
datanames = c("ADSL")
)
)
data <- teal_data()
data <- within(data, {
ADSL <- radsl(cached = TRUE)
})
datanames(data) <- c("ADSL")
app <- init(
data = data,
modules = modules
)
if (Sys.getenv("QUARTO_ROOT") == "") {
shinyApp(app$ui, app$server)
}
```
```{r save_script, include = FALSE}
code <- paste0(knitr::knit_code$get("ex"), collapse = "\n")
writeLines(code, "scripts/exercise_3.R")
```
### URL
```{r shinylive_url, echo = FALSE, results = 'asis', purl = FALSE}
code <- paste0(knitr::knit_code$get("ex"), collapse = "\n")
url <- roxy.shinylive::create_shinylive_url(code)
cat(sprintf("[Open in Shinylive](%s)\n\n", url))
```
### App
```{r shinylive_iframe, echo = FALSE, out.width = '150%', out.extra = 'style = "position: relative; z-index:1"', purl = FALSE}
knitr::include_url(url, height = "800px")
```