-
Notifications
You must be signed in to change notification settings - Fork 14
/
comp-r-shiny.qmd
552 lines (413 loc) · 14.9 KB
/
comp-r-shiny.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
---
title: Shiny for R
format:
html:
code-line-numbers: true
code-annotations: hover
filters:
- line-highlight
execute:
eval: false
---
The R and Python Shiny packages are quite similar, and as a result if you know how to build a Shiny app in R you are well on your way to building one in Python.
All of the main components of Shiny like reactivity, rendering functions, and modules are the same, and both packages use the same JavaScript code.
There are, however, a few differences that you need to keep in mind in order to build effective Shiny applications in Python.
If you're reading this, we expect that you are an existing R Shiny user with some Python knowledge.
::: callout-tip
### Shiny Express
Shiny [express](express-vs-core.qmd) is a new, more expressive, way to build PyShiny apps. It is not available in R, so the comparisons drawn below are only relevant to core (i.e., non-express) apps.
:::
# Getting started
R users tend to use the R console to install and run Shiny while Python requires you to use the terminal.
To get started you can do the following (or see the [installation instructions](install-create-run.qmd) for a more in-depth explanation):
1. In your terminal, create a new directory with `mkdir <my_directory>` and navigate into it with `cd <my_directory>`
2. Install Shiny. We strongly recommend using a virtual environment for this as it will eliminate dependency resolution headaches and make deployment easier.
::: {.panel-tabset .panel-pills}
### Install with pip
``` bash
# Create a virtual environment in the .venv subdirectory
python3 -m venv venv
source venv/bin/activate
# Install shiny
pip install shiny
```
### Install with conda
``` bash
# Create a conda environment named 'myenv'
conda create --name myenv
conda activate myenv
conda install -c conda-forge shiny
```
:::
3. Call `shiny create .` to create an example shiny app in your directory
4. Call `shiny run --reload` to run the app and reload when the source code changes
::: callout-tip
### Takeaways
- Always use a virtual environment with Python projects
- Install and run shiny from the command line, not a Python process
:::
# Syntax differences
There are five main syntax difference between the R and Python versions of Shiny:
1) Decorators instead of render functions
2) Function names are used to connect outputs to the UI
3) More precise namespaces
4) All inputs are invoked with `input.<input_name>()`
5) Some functions have different names
## Decorators
Shiny for Python uses decorators instead of top level rendering functions.
Decorators are just python functions which take other functions and are invoked by putting `@<decorator>` above the function definition.
While R doesn't have an exact analog to decorators they are similar to [function operators](https://adv-r.hadley.nz/function-operators.html) you like `purrr::safely`.
::: callout-tip
### Takeaways
- Use rendering decorators like `@render.plot`, `@render.text`, or `@render.ui` instead of `renderPlot()`, `renderText`, or `renderUI`
- Reactive calculations (equivalent to reactive expressions in R) are decorated `@reactive.calc`, and reactive effects (equivalent to observers in R) are decorated with `@reactive.effect`.
:::
::: {.grid .column-screen-inset}
::: {.g-col-12 .g-col-md-6}
#### R
```{R}
#| source-line-numbers: "9-11"
library(shiny)
ui <- fluidPage(
sliderInput("n", "N", 0, 100, 40),
verbatimTextOutput("txt")
)
server <- function(input, output, session) {
output$txt <- renderText({
paste0("n*2 is ", input$n, " * 2")
})
}
shinyApp(ui, server)
```
:::
::: {.g-col-12 .g-col-md-6}
#### Python
```{python}
#| source-line-numbers: "9-13"
from shiny import ui, render, App
app_ui = ui.page_fluid(
ui.input_slider("n", "N", 0, 100, 40),
ui.output_text_verbatim("txt"),
)
def server(input, output, session):
@render.text
def txt():
return f"n*2 is {input.n() * 2}"
app = App(app_ui, server)
```
:::
:::
## Connecting outputs
Both R an Python use a special object type to connect server computations to UI components, but the interface is quite different.
In R, we connect outputs to UI elements by assigning into the `output` object but since Python renderings are produced with decorators, we instead use the function name to connect the rendered object to its UI component.
::: callout-tip
### Takeaways
- In Python, we don't define outputs by assigning to `output$x`
- Use the function name to connect a server output to a UI element
:::
::: {.grid .column-screen-inset}
::: {.g-col-12 .g-col-md-6}
#### R
```{R}
#| source-line-numbers: "5,9"
library(shiny)
ui <- fluidPage(
sliderInput("n", "N", 0, 100, 40),
verbatimTextOutput("txt")
)
server <- function(input, output) {
output$txt <- renderText({
paste0("n*2 is ", input$n," * 2")
})
}
shinyApp(ui, server)
```
:::
::: {.g-col-12 .g-col-md-6}
#### Python
```{python}
#| source-line-numbers: "5,11"
from shiny import ui, render, App
app_ui = ui.page_fluid(
ui.input_slider("n", "N", 0, 100, 40),
ui.output_text_verbatim("txt"),
)
def server(input, output, session):
@render.text
def txt():
return f"n*2 is {input.n() * 2}"
app = App(app_ui, server)
```
:::
:::
### Submodules
All of the Shiny R functions are in a single package namespace.
On the Python side we make use of [submodules](https://docs.python.org/3/tutorial/modules.html#packages) to keep related functions together.
Note that "submodules" in this case refers to the generic module which is not the same as [shiny modules](modules.qmd).
For example, instead of `sliderInput()`, you would call `ui.input_slider()`, where the `ui.` refers to a submodule of the main `shiny` module.
::: callout-tip
#### Takeways
- Python submodules make autocomplete easier
- Some important namespaces include:
- **ui** for the UI elements
- **render** includes the rendering decorators
- **reactive** has reactive expressions and observers
:::
::: {.grid .column-screen-inset}
::: {.g-col-12 .g-col-md-6}
#### R
```{R}
#| source-line-numbers: "3-6"
library(shiny)
ui <- fluidPage(
sliderInput("n", "N", 0, 100, 40),
verbatimTextOutput("txt")
)
server <- function(input, output) {
output$txt <- renderText({
paste0("n*2 is ", input$n," * 2")
})
}
shinyApp(ui, server)
```
:::
::: {.g-col-12 .g-col-md-6}
#### Python
```{python}
#| source-line-numbers: "3-6"
from shiny import ui, render, App
app_ui = ui.page_fluid(
ui.input_slider("n", "N", 0, 100, 40),
ui.output_text_verbatim("txt"),
)
def server(input, output, session):
@render.text
def txt():
return f"n*2 is {input.n() * 2}"
app = App(app_ui, server)
```
:::
:::
### Call inputs with `()`
In R reactive values and reactive expressions are retrieved with different syntax.
Reactive values like `input$value` are retrieved like variables while reactive expressions are called like functions `my_reactive()`.
This interface makes it seem like inputs and reactive expressions are different types of thing when in fact they're the same type of thing.
In Python we chose to require that all reactive objects be retrieved with a function call.
So instead of calling `input.value` you use `input.value()`.
::: {.grid .column-screen-inset}
::: {.g-col-12 .g-col-md-6}
### R
```{R}
#| source-line-numbers: "10,13"
library(shiny)
ui <- fluidPage(
sliderInput("n", "N", 0, 100, 40),
verbatimTextOutput("txt")
)
server <- function(input, output) {
n <- reactive({input$n})
output$txt <- renderText({
paste0("n*2 is ", n()," * 2")
})
}
shinyApp(ui, server)
```
:::
::: {.g-col-12 .g-col-md-6}
### Python
```{python}
#| source-line-numbers: "11,16"
from shiny import ui, render, reactive, App
app_ui = ui.page_fluid(
ui.input_slider("n", "N", 0, 100, 40),
ui.output_text_verbatim("txt"),
)
def server(input, output, session):
@reactive.calc
def n():
return input.n()
@render.text
def txt():
return f"n*2 is {n() * 2}"
app = App(app_ui, server)
```
:::
:::
::: callout-tip
### Takeaways
- Access input values by calling the object like a function
- `input.x()`, not `input$x`
:::
### Function name changes
The Python function names have been modified to make them easier to discover with tab completion.
For example all python output functions start with `output_` while the input functions start with `input_`.
This means that you can type `ui.ou` and hit tab to see all of the available output functions.
The Shiny R functions on the other hand all start with the element type (`plotOutput`, `textInput`), which makes it hard to see all of the input or output options.
For the most part you can follow this naming pattern to find the function you're looking for, but there are a number of functions that have different names in R and Python, the most important of which are listed below:
| R Function | Python Equivalent |
|-----------------------------------|-------------------------------------|
| `observe` | `@reactive.effect` |
| `reactive` | `@reactive.calc` |
| `bindEvent` | `@reactive.event` |
| `reactiveEvent` | `@reactive.calc` with `@reactive.event` |
| `observeEvent` | `@reactive.effect` with `@reactive.event` |
| `htmlTemplate` | `page_template` |
| `tabPanelBody` | `navs_content` |
| `*Tab` (`insertTab`, `appendTab`, etc) | `nav_*` (`nav_insert`, `nav_append` etc) |
| `fluidRow` | `row` |
# Reactive programming
Reactivity works mostly the same in R and Python, but there are a few small differences in naming and syntax.
## New names for `reactive()` and `observe()`
In Shiny for R, reactive expressions (created by `reactive()`, which are used when you want to compute a value (which is then used in an output or an observer), and observers (created by `observe()`) are used for their side effects, like writing data to disk.
This is a common source of confusion because the names `reactive()` and `observe()` do not clearly express when they should be used.
To help clarify this confusion we've renamed `reactive()` to `@reactive.calc`, and `observe()` to `@reactive.effect` in Python.
::: {.grid .column-screen-inset}
::: {.g-col-12 .g-col-md-6}
#### R
```{R}
#| source-line-numbers: "11,14-17"
library(shiny)
ui <- fluidPage(
sliderInput("n", "N", 0, 100, 40),
verbatimTextOutput("txt"),
actionButton("reset", "Reset")
)
server <- function(input, output, session) {
val <- reactive({input$n})
observe({
input$reset
updateSliderInput(session, "n", value = 40)
})
output$txt <- renderText({
paste0("n*2 is ", val()," * 2")
})
}
shinyApp(ui, server)
```
:::
::: {.g-col-12 .g-col-md-6}
#### Python
```{python}
#| source-line-numbers: "10-12,14-17"
from shiny import App, reactive, render, ui
app_ui = ui.page_fluid(
ui.input_slider("n", "N", 0, 100, 40),
ui.output_text_verbatim("txt"),
ui.input_action_button("reset", "Reset"),
)
def server(input, output, session):
@reactive.calc
def val():
return input.n()
@reactive.effect
def _():
input.reset()
ui.update_slider("n", value=40)
@render.text
def txt():
return f"n*2 is {val() * 2}"
app = App(app_ui, server)
```
:::
:::
## Reactive Values
------------------------------------------------------------------------
In R, there are two types of reactive objects which store values:
- Items in a `reactiveValues` object.
This is a list-like object that contains multiple reactive values.
(Note that the `input` object is a `reactiveValues`.)
- Standalone `reactiveVal` objects.
In R, the way that you get values from a `reactiveValues` object differs from how you get it from a `reactiveVal`.
To get the value of an item's in a `reactiveValues` object, you would simply access it with `input$x`.
However, for a standalone `reactiveVal`, you would invoke it like a function, with `x()`.
```{r}
vals <- reactiveValues(x = 1, y = 2)
z <- reactiveVal(3)
# Retrieve values
print(vals$x)
print(z())
```
In Shiny for Python, we've simplified things in the following ways:
- There is no direct analog to R's `reactiveValues`.
- The analog of R's standalone `reactiveVal` is `reactive.value`.
(The `input` object in Python is a dictionary-like object containing individual `reactive.value` objects.)
- Reactive values have can be retrieved with `my_val()` or `my_val.get()` and can be set with `my_val.set()`.
There is no analog of `reactiveValues` in Python, but you can create something similar by using a dictionary of `reactive.value` objects.
```{python}
vals = {
"x": reactive.value(1),
"y": reactive.value(2),
}
z = reactive.value(3)
# Retrieve values
print(vals.x())
print(z())
```
::: {.grid .column-screen-inset}
::: {.g-col-12 .g-col-md-6}
#### R
```{R}
#| source-line-numbers: "11,16-17,22-23,28"
library(shiny)
ui <- fluidPage(
actionButton("minus", "-1"),
actionButton("plus", "+1"),
br(),
textOutput("value")
)
server <- function(input, output, session) {
value <- reactiveVal(0)
observeEvent(input$minus, {
newValue <- value() - 1
value(newValue)
})
observeEvent(input$plus, {
newValue <- value() + 1
value(newValue)
})
output$value <- renderText({
value()
})
}
shinyApp(ui, server)
```
:::
::: {.g-col-12 .g-col-md-6}
#### Python
```{python}
#| source-line-numbers: "11,16-17,22-23,28"
from shiny import *
app_ui = ui.page_fluid(
ui.input_action_button("minus", "-1"),
ui.input_action_button("plus", "+1"),
ui.br(),
ui.output_text("value"),
)
def server(input: Inputs, output: Outputs, session: Session):
val = reactive.value(0)
@reactive.effect
@reactive.event(input.minus)
def _():
newVal = val() - 1
val.set(newVal)
@reactive.effect
@reactive.event(input.plus)
def _():
newVal = val() + 1
val.set(newVal)
@render.text
def value():
return str(val())
app = App(app_ui, server)
```
:::
:::
# Mutability
One of the biggest differences between R and Python is the mutability of objects.\
In R, most objects are *immutable*.
This means, for example, that if you pass a data frame to a function, that function cannot alter your copy of the data frame.\
In Python, many objects are *mutable*.
This means that they can be modified in place---modifying an object in one part of a program can cause it to be (unexpectedly) modified in another part of the program.
This difference in mutability has consequences for Shiny applications and for programming in general.
In many cases, using `.copy()` in Python will help avoid bugs due to inadvertently altering mutable objects.
See our [page on mutability](https://shiny.posit.co/py/docs/reactive-mutable.html) to understand the possible bugs and how to avoid them.