-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathisolation.Rmd
More file actions
84 lines (70 loc) · 2.44 KB
/
isolation.Rmd
File metadata and controls
84 lines (70 loc) · 2.44 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
# Pour aller plus loin : isolation
## Isolation | Définition
Par défaut, les outputs et les expressions réactives se mettent à jour automatiquement quand un des inputs présents dans le code change de valeur. Dans certains cas, on aimerait pouvoir contrôler un peu cela.
Par exemple, en utilisant un bouton de validation (__actionButton__) des inputs pour déclencher le calcul des sorties.
- un input peut être isolé comme cela `isolate(input$id)`
- une expression avec la notation suivante `isolate({expr})` et l'utilisation de ``{}``
## Isolation | Exemple | ui.R
Trois inputs : __color__ et __bins__ pour l'histogramme, et un __actionButton__ :
```{r, echo = TRUE, eval = FALSE}
shinyUI(fluidPage(
titlePanel("Isolation"),
sidebarLayout(
sidebarPanel(
radioButtons(inputId = "col", label = "Choose a color", inline = TRUE,
choices = c("red", "blue", "darkgrey")),
sliderInput("bins", "Number of bins:", min = 1, max = 50, value = 30),
actionButton("go_graph", "Update !")
),
mainPanel(plotOutput("distPlot"))
)
))
```
## Isolation | Exemple | server.R
On isole tout le code sauf l'__actionButton__ :
```{r, echo = TRUE, eval = FALSE}
shinyServer(function(input, output) {
output$distPlot <- renderPlot({
input$go_graph
isolate({
inputColor <- input$color
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = inputColor, border = 'white')
})
})
})
```
L'histogramme sera donc mis-à-jour quand l'utilisateur cliquera sur le bouton.
## Isolation | Exemple | App
```{r, echo = FALSE}
rmarkdown::render_delayed({
shinyApp(
ui = fluidPage(
titlePanel("Isolation"),
sidebarLayout(
sidebarPanel(
radioButtons(inputId = "col", label = "Choose a color", inline = TRUE,
choices = c("red", "blue", "darkgrey")),
sliderInput("bins", "Number of breaks:", min = 1, max = 50, value = 30),
actionButton("go_graph", "Update !")
),
mainPanel(
plotOutput("distPlot")
)
)
),
server = function(input, output) {
output$distPlot <- renderPlot({
input$go_graph
isolate({
col <- input$col
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = col, border = 'white')
})
})
}
)
})
```