-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSeanceShiny-en.Rmd
158 lines (123 loc) · 3.83 KB
/
SeanceShiny-en.Rmd
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
---
title: "Visualisation, </br>Shiny"
author: "Etienne Côme"
date: "9 Novembre 2023"
output:
revealjs::revealjs_presentation:
theme: white
transition: none
self_contained: true
css: slides.css
beamer_presentation:
toc: false
incremental: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Shiny
<link href="https://fonts.googleapis.com/css?family=Armata&display=swap" rel="stylesheet">
- Interactive interface / dashboard
- Quite easy to implement
- All in R
- Debugging sometimes delicate because // javascript-html
- Requires a shiny server to be deployed
## Shiny
### 2 components
- <span class="green">UI: describes the interface</span>
- creation of the layout
- buttons, sliders, plots, ...
- <span class="red">Server: describes the application logic </span>
- carries out the treatments
- defines how the interface updates when inputs change
- makes the link between the inputs and the outputs
## <span class="green">UI</span>
```{r, eval=FALSE,echo=TRUE}
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "bins", label = "Number of bins:",
min = 1, max = 50, value = 30)
),
# Show a plot of the generated distribution
mainPanel(plotOutput(outputId = "distPlot"))
)
)
```
## <span class="green">UI</span>
### Different components :
- layout : page, layout, panel
- inputs : actionButton, dateInput, actionLink, sliderInput, radioButtons,...
- outputs : plotOutput, dataTableOutput, ... + leaflet
see la cheat sheet for details
### ! inputId, outputId
- id to reference input and output (defined in the ui)
- allow the server to access them
## <span class="red">Server</span>
```{r, eval=FALSE,echo=TRUE}
# fait le liens entre input et output
server <- function(input, output) {
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}
```
## <span class="red">Server</span>
### Reactive part :
- renderPlot, renderImage,renderTable, ...
Th block will be re run each time one of the input it use change.
see cheat-sheet for details
## <span class="red">Server, advanced reactivity</span>
Structure the code to avoid duplicated treatments :
```{r, eval=FALSE,echo=TRUE}
data = reactive({})
data()
```
## <span class="red">Server, réactivité avancée</span>
Structure the code to avoid duplicated treatments :
- reactive : reactive block that return a reactive expression
- observe : reactive block without any output (side effect i/o)
```{r, eval=FALSE,echo=TRUE}
data = reactive({})
data()
renderPlot(data())
observe({
input$saveButton
write.csv(data())
})
```
## <span class="red">Server, advanced reactivity</span>
Isolate a code block :
```{r, eval=FALSE,echo=TRUE}
observe({
input$saveButton # Do take a dependency on input$saveButton
# isolate a whole block
data <- isolate({
a <- input$valueA # No dependency on input$valueA or input$valueB
b <- input$valueB
c(a=a, b=b)
})
writeToDatabase(data)
})
```
## <span class="red">Server, advanced reactivity</span>
Execute a block conditional to a specified list of inputs change
```{r, eval=FALSE,echo=TRUE}
data = eventReactive(input,{})
data()
observeEvent(input,{}})
```
## <span class="red">Server, advanced reactivity</span>
Deploy :
- shinyapp.io -> account creation
- button publish document
- get key and secret
- package the data needed by the app in an Rdata file in the app folder
- use the load functio to load Rdata files