-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmake-screenshots.R
More file actions
188 lines (173 loc) · 5.13 KB
/
Copy pathmake-screenshots.R
File metadata and controls
188 lines (173 loc) · 5.13 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
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
# Regenerate the question screenshots in images/screenshots/ that the docs
# embed for widgets that only render well in a live Shiny environment
# (see chunks/question-screenshot-note.qmd).
#
# Run from the website repo root, with the current surveydown version
# installed: Rscript make-screenshots.R
# Requires: surveydown, shiny, chromote, and Google Chrome.
library(chromote)
port <- 8121
shots <- list(
# selector = output file (relative to website root)
"#container-experience" = "images/screenshots/slider.png",
"#container-slider_single_val" = "images/screenshots/slider-numeric-single.png",
"#container-slider_range" = "images/screenshots/slider-numeric-range.png",
"#container-pet" = "images/screenshots/mc-image.png",
"#container-pet_no_caption" = "images/screenshots/mc-image-no-caption.png",
"#container-pets_owned" = "images/screenshots/mc-multiple-image.png"
)
# Image-choice cards whose inputs to click (to show the selected state)
# before screenshotting, keyed by container selector
clicks <- list(
"#container-pet" = "#pet input[value=\"cat\"]",
"#container-pet_no_caption" = "#pet_no_caption input[value=\"cat\"]",
"#container-pets_owned" = c(
"#pets_owned input[value=\"cat\"]",
"#pets_owned input[value=\"dog\"]"
)
)
# -- Build a temporary survey app with the questions to screenshot -------
# Question code must match the examples shown on docs/question-types.qmd.
app_dir <- file.path(tempdir(), "screenshot_app")
dir.create(app_dir, showWarnings = FALSE)
# Image-choice examples need their images on Shiny's resource path
dir.create(file.path(app_dir, "images"), showWarnings = FALSE)
file.copy(
c("images/cat.png", "images/dog.png"),
file.path(app_dir, "images"),
overwrite = TRUE
)
writeLines(
c(
"---",
"survey-settings:",
" mode: preview",
" use-cookies: false",
"---",
"",
"```{r}",
"library(surveydown)",
"```",
"",
"--- page1",
"",
"```{r}",
"sd_question(",
" type = 'slider',",
" id = 'experience',",
" label = \"How would you rate your overall experience?\",",
" option = c(",
" \"Very poor\" = \"very_poor\",",
" \"Poor\" = \"poor\",",
" \"Neutral\" = \"neutral\",",
" \"Good\" = \"good\",",
" \"Very good\" = \"very_good\"",
" ),",
" selected = 'neutral'",
")",
"",
"sd_question(",
" type = 'slider_numeric',",
" id = 'slider_single_val',",
" label = 'Single value example',",
" option = seq(0, 10, 1)",
")",
"",
"sd_question(",
" type = 'slider_numeric',",
" id = 'slider_range',",
" label = 'Range example',",
" option = seq(0, 10, 1),",
" default = c(3, 5)",
")",
"",
"sd_question(",
" type = 'mc_image',",
" id = 'pet',",
" label = \"Which pet do you prefer?\",",
" option = c('Cat' = 'cat', 'Dog' = 'dog'),",
" image = c('images/cat.png', 'images/dog.png')",
")",
"",
"sd_question(",
" type = 'mc_image',",
" id = 'pet_no_caption',",
" label = \"Which pet do you prefer?\",",
" option = c('cat', 'dog'),",
" image = c('images/cat.png', 'images/dog.png')",
")",
"",
"sd_question(",
" type = 'mc_multiple_image',",
" id = 'pets_owned',",
" label = \"Which pets have you owned? (select all that apply)\",",
" option = c('Cat' = 'cat', 'Dog' = 'dog'),",
" image = c('images/cat.png', 'images/dog.png')",
")",
"```",
"",
"--- end",
"",
"End.",
"",
"```{r}",
"sd_close()",
"```"
),
file.path(app_dir, "survey.qmd")
)
writeLines(
c(
"library(surveydown)",
"ui <- sd_ui()",
"server <- function(input, output, session) {",
" sd_server()",
"}",
"shiny::shinyApp(ui = ui, server = server)"
),
file.path(app_dir, "app.R")
)
# -- Launch the app -------------------------------------------------------
system(sprintf("pkill -f 'shiny::runApp.*%d'", port), ignore.stderr = TRUE)
Sys.sleep(1)
cat("Launching screenshot app (first render takes ~1 min)...\n")
system(sprintf(
"Rscript -e 'shiny::runApp(\"%s\", port = %d)' > /tmp/sd_screenshot_app.log 2>&1 &",
normalizePath(app_dir), port
))
url <- sprintf("http://127.0.0.1:%d/", port)
up <- FALSE
for (i in 1:60) {
up <- tryCatch(
{
suppressWarnings(readLines(url, n = 1))
TRUE
},
error = function(e) FALSE
)
if (up) break
Sys.sleep(3)
}
if (!up) stop("App did not start. See /tmp/sd_screenshot_app.log")
# -- Take the screenshots -------------------------------------------------
b <- ChromoteSession$new(width = 1000, height = 1600)
b$Page$navigate(url)
Sys.sleep(6)
# Click image-choice cards to show the selected state in the screenshots
for (container in names(clicks)) {
for (target in clicks[[container]]) {
b$Runtime$evaluate(sprintf(
"document.querySelector('%s').click();", target
))
}
}
Sys.sleep(1)
for (sel in names(shots)) {
out <- shots[[sel]]
b$screenshot(out, selector = sel, scale = 2)
cat("Saved", out, "\n")
}
b$close()
system(sprintf("pkill -f 'shiny::runApp.*%d'", port))
unlink(app_dir, recursive = TRUE)
cat("Done.\n")