Repository navigation
Expand file tree
/
Copy path01_HTML_rendering.qmd
More file actions
141 lines (111 loc) · 5.24 KB
/
Copy path01_HTML_rendering.qmd
File metadata and controls
141 lines (111 loc) · 5.24 KB
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: "HTML rendering - What happens behind the scenes"
author: "Dror Berel"
format:
html:
page-layout: full
toc: true
toc-location: left
toc-depth: 3
self-contained: true
execute:
echo: true
editor_options:
chunk_output_type: console
editor:
markdown:
wrap: 72
---
# Core idea:
R gives you a set of functions that write HTML for you. You never touch
HTML directly. You call R functions, and Shiny turns them into the
browser code.
There are really just three moving parts:
## 1. UI functions → HTML tags
When you write `textInput("name", "Your name")` in R, Shiny immediately
converts that into actual HTML text:\
`<label for="name">Your name</label>`\
`<input id="name" type="text" class="form-control">`
::: form-group
<label for="name">Your name</label>
<input id="name" type="text" class="form-control">
:::
The browser receives that comma-separated stream of HTML text and draws
a text box. `fluidPage()`, `sidebarLayout()`, `sliderInput()` - all of
these are just R functions that produce HTML strings.
## 2. Server functions → rendered outputs
On the server side, you write things like
`renderPlot({ hist(rnorm(100)) })`. Shiny runs that R expression,
captures what it produces (a plot image, a table, some text), converts
it to HTML, and sends it to the browser. `renderText`, `renderTable`,
`renderUI` - each one knows how to wrap its R output in the right HTML
tags.
## 3. Reactivity connects the two
When a user moves a slider, the browser sends the new value back to the
R server. Shiny's reactive system notices which render functions depend
on that input and re-runs only those, then pushes fresh HTML back to the
browser.
Here's a simple visual of the flow:

# Key insight
**HTML is just text**. A web page is nothing more than a very long
string of comma-separated tags that the browser reads top to bottom.
Shiny's only trick is that it writes that string for you using R
functions instead of making you type raw HTML.\
So when you call:
`rsliderInput("n", "Sample size", min=10, max=500, value=100)` Shiny
runs that function, which returns this character string:\
`<label class="control-label" for="n">Sample size</label>`\
`<input class="js-range-slider" id="n" min="10" max="500" value="100"/>`
That character string is rendered into this on the browser:
::: {.form-group .shiny-input-container}
<label class="control-label" for="n">Sample size</label>
<input class="js-range-slider" id="n" min="10" max="500" value="100"/>
:::
That string gets concatenated with all the other UI function outputs,
and the whole pile of text is sent to the browser, which parses it
left-to-right, comma-separated tag by tag, and draws the interface. The
server side works the same way. renderPlot() runs your ggplot code,
saves the image to a temp file, and injects `<img src="..."/>` into the
HTML. `renderText()` wraps your string in a `<span>`. The browser never
knows it's talking to R - it just sees HTML.
# Shiny module
The key new idea with modules is namespacing. Without a module, every
input ID is global - `input$n`
is just `input$n`.
A module wraps a chunk of UI and server logic in its own private
namespace, so the same module can be used multiple times without ID
collisions. The mechanism is straightforward: you write two functions -
`myModuleUI(id)` and `myModuleServer(id)` - and Shiny handles the rest
by prefixing every HTML id attribute with the namespace you provide. The
R code is the same; what changes is the HTML it emits.
Here's a concrete before/after showing exactly what HTML gets produced:
**Without** a module - `sliderInput("n", ...)` emits:
`html<input id="n" type="range" ...>`
**With** a module called as `myModuleUI("mod1")` - that same
`sliderInput("n", ...)` inside the module UI now emits:
`html<input id="mod1-n" type="range" ...>` The `-` separator is the
entire trick.
Shiny prepends id `+ "-"` to every element ID inside the module. On the
server side, `moduleServer` does the same thing in reverse - when your
server function reads `input$n`, Shiny silently translates that to
`input$mod1-n` so it reads from the right namespaced element.
This lets you call `myModuleUI("mod1")` and `myModuleUI("mod2")` and get
two completely independent widgets in the HTML, even though they're
generated by identical R code.
!!!
The `NS(id)` call is really the whole story. `NS` stands for "namespace"
and it returns a small helper function. When you write `ns <- NS(id)`
and then `ns("n")`, it just returns the string `"mod1-n"`. That string
gets passed to `sliderInput()` as the `inputId`, and `sliderInput()`
places it in the HTML id attribute as normal.
Nothing magical - just string concatenation happening one layer up. On
the server side,
`moduleServer(id, function(input, output, session) { ... })` sets up a
filtered view of input that automatically reads from and writes to the
`mod1-` prefixed keys. So when your server code says `input\$n`, Shiny's
internals transparently read `input[["mod1-n"]]` from the real input
list. The result: you can stamp out as many instances of a module as you
like - `myModuleUI("ae")`, `myModuleUI("labs")`,
`myModuleUI("vitals")` - and each gets its own isolated bubble of HTML
ids, inputs, and outputs, all from the same R function definition.