-
Notifications
You must be signed in to change notification settings - Fork 6
/
action-bookmark.Rmd
129 lines (99 loc) · 3.86 KB
/
action-bookmark.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
# Bookmarking
```{r, echo=FALSE}
htmltools::includeHTML("./partials/todo.html")
```
### Exercise 11.3.1 {-}
Generate app for visualising the results of [noise::ambient_simplex()](https://ambient.data-imaginist.com/reference/noise_simplex.html). Your app should allow the user to control the frequency, fractal, lacunarity, and gain, and be bookmarkable. How can you ensure the image looks exactly the same when reloaded from the bookmark? (Think about what the seed argument implies).
:::solution
#### Solution {-}
For this example, we'll use the bookmarking by setting `enableBookmarking = "url"` within the `shinyApp` function. In order to ensure the simulation is the same each time we re-render the bookmark we'll create a grid to use for points then set the seed to `42` to ensure the same image is rendered when the bookmark is loaded.
```{r, eval=FALSE}
library(shiny)
library(ambient)
ui <- function(request) {
fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("frequency", "Frequency", min = 0, max = 1, value = 0.01),
selectInput("fractal", "Fractal", choices = c("none", "fbm", "billow", "rigid-multi")),
sliderInput("gain", "Gain", min = 0, max = 1, value = 0.5),
),
mainPanel(
plotOutput("result")
)
)
)
}
server <- function(input, output, session) {
grid <- long_grid(seq(1, 10, length.out = 1000), seq(1, 10, length.out = 1000))
noise <- reactive({
ambient::gen_simplex(
x = grid$x,
y = grid$y,
seed = 42,
frequency = input$frequency,
fractal = input$fractal,
gain = input$gain
)
})
output$result <- renderPlot({
plot(grid, noise())
})
observe({
reactiveValuesToList(input)
session$doBookmark()
})
onBookmarked(updateQueryString)
}
shinyApp(ui = ui, server = server, enableBookmarking = "url")
```
:::
<!---------------------------------------------------------------------------->
<!---------------------------------------------------------------------------->
<!---------------------------------------------------------------------------->
### Exercise 11.3.2 {-}
Make a simple app that lets you upload a csv file and then bookmark it. Upload a few files and then look in shiny_bookmarks. How do the files correspond to the bookmarks? (Hint: you can use readRDS() to look inside the cache files that Shiny is generating).
:::solution
#### Solution {-}
By setting the `state$values$data` equal to the `data` reactive, we can store the contents of the uploaded `csv`. Looking in the `shiny_bookmarks` folder we see an `input.rds` which has the same 4 arguments as `input$file`:
1. `name`
2. `size`
3. `type`
4. `datapath`
All of these except the `datapath` are the same as when we upload the file; rather than the temporary location the file is saved to within the shiny session, the datapath becomes `0.csv`, a csv file created within the same folder are our `input.RDS`.
```{r, eval=FALSE}
library(shiny)
ui <- function(request){
fluidPage(
sidebarLayout(
sidebarPanel(
bookmarkButton(),
fileInput("file", "Choose CSV File", multiple = TRUE,accept = ".csv")
),
mainPanel(
tableOutput("contents")
)
)
)
}
server <- function(input, output) {
# create reactive of input file
data <- reactive({
req(input$file)
read.csv(input$file$datapath)
})
# display head
output$contents <- renderTable( head(data()) )
# set the state to the df reactive
onBookmark(function(state){
state$values$data <- data()
})
# on restore set df to the state
onRestore(function(state){
data <- reactive(state$values$data)
})
enableBookmarking(store="server")
}
shinyApp(ui, server)
```
:::