Repository navigation
Expand file tree
/
Copy pathapp.R
More file actions
454 lines (423 loc) · 13 KB
/
Copy pathapp.R
File metadata and controls
454 lines (423 loc) · 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
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
source("packages.R")
source("utils.R")
source("plot-examples.R")
# UI layout
ui <- page_navbar(
title = "ggplot2 Layer Explorer",
theme = bs_theme(),
# Main Explorer tab
nav_panel(
title = "Explorer",
page_sidebar(
sidebar = sidebar(
width = 350,
radioButtons(
"selected_function",
"Method selection:",
choiceNames = fns_info,
choiceValues = names(fns)
),
if (!in_webr()) { actionButton("debug_btn", "Debug") }
),
layout_column_wrap(
width = 1/2,
height = "calc(100vh - 100px)",
# Code editor card
card(
card_header("Define plot"),
div(
div(
style = "margin-bottom: -1rem;",
radioInlinedButtons(
inputId = "plot_selector",
label = "Use a pre-defined plot:",
choices = seq_along(plots)
)
),
hr(style = "margin: 1rem 0;"),
aceEditor(
"code_editor",
value = plots$plot1,
mode = "r", theme = "chrome", fontSize = 14,
minLines = 5, maxLines = 20, autoScrollEditorIntoView = TRUE
),
div(
style = "display: flex; justify-content: space-between;",
actionButton("run_code_btn", "Run Plotting Code", class = "btn-primary"),
textOutput("code_error_output")
),
div(
plotOutput("plot_preview", height = "300px")
)
)
),
# Right panel with Layer Selector and Inspect
card(
card_header("Explore method"),
div(
div(
style = "display: flex; align-items: center; margin-bottom: -1rem;",
span("Layer number (i):", style = "margin-right: 10px;"),
uiOutput("layer_id", inline = TRUE, style = "margin-bottom: -1rem;"),
code(textOutput("selected_layer_fn"), style = "margin-left: 1rem;"),
actionButton(
"show_layer_methods", "Show layer info",
style = "margin-left: 1rem;",
class = "btn-sm btn-primary mt-1"
)
),
div(
style = "margin-top: 1.5rem; margin-bottom: -1rem;",
radioInlinedButtons(
inputId = "inspect_type",
label = "Inspect:",
choices = c("input", "output"),
selected = "output",
extras = actionButton(
"show_input_output_diff", "Show data diff",
style = "margin: 1rem;",
class = "btn-sm btn-primary mt-1"
)
)
),
hr(style = "margin: 1rem 0;"),
aceEditor(
"function_expr",
value = "",
mode = "r", theme = "chrome", fontSize = 14,
minLines = 5, maxLines = 20, autoScrollEditorIntoView = TRUE
),
actionButton("run_inspect_expr_btn", "Run expression", class = "btn-sm btn-primary mt-1"),
actionButton("run_highjack_expr_btn", "Highjack plot 😈", class = "btn-sm btn-secondary mt-1"),
uiOutput("function_output_ui")
)
)
)
)
),
# About tab
nav_panel(
title = "About",
page_fillable(
padding = 20,
div(
style = "max-width: 900px; margin: 0 auto;",
card(
card_body(
tags$style(HTML("
pre code {
padding: 0px;
}
li {
margin-bottom: .2rem;
}
")),
includeHTML("about.html")
)
)
)
)
)
)
server <- function(input, output, session) {
# Setup environment
user_env <- new.env()
lapply(
c("ggtrace", "grid", "ggplot2", "dplyr"),
function(pkg) eval(
call2("library", pkg, character.only = TRUE),
envir = user_env
)
)
# Run initial code
tryCatch({
eval(parse(text = plots$plot1), envir = user_env)
# Set initial value for i
user_env$i <- 1
lockBinding("i", user_env)
}, error = function(e) {
# Silent error handling for initialization
})
# Initialize reactive for number of layers
layer_count <- reactiveVal(length(user_env$p$layers))
# Initialize reactive for current expression
current_expr <- reactiveVal("")
# Dynamic UI for layer input based on number of layers
output$layer_id <- renderUI({
nlayers <- layer_count()
numericInput(
"layer_selector",
label = NULL,
value = 1,
min = 1,
max = max(1, nlayers),
step = 1,
width = "50px"
)
})
# Execute user plotting code
run_code_editor <- function(code_text) {
output$code_error_output <- renderText("")
tryCatch({
eval(parse(text = gsub(x = code_text, "\r", "")), envir = user_env)
if (!exists("p", envir = user_env) || !inherits(user_env$p, "ggplot")) {
stop("A ggplot object `p` must exist in the environment")
}
# Update plot and layer count after running code
num_layers <- length(user_env$p$layers)
layer_count(num_layers)
output$plot_preview <- renderPlot({ user_env$p })
# Re-run the inspect expression with updated environment
run_inspect_expr(current_expr())
}, error = function(e) {
output$code_error_output <- renderText(paste("Error:", e$message))
})
}
# Refresh and execute inspect code
update_and_run_inspect_expr <- function(fn = input$selected_function,
inspect_type = input$inspect_type) {
fn_call <- fn_to_expr(fn, inspect_type)
updateAceEditor(session, "function_expr", poorman_styler(fn_call))
current_expr(fn_call)
run_inspect_expr(fn_call)
}
observeEvent(input$run_code_btn, {
run_code_editor(input$code_editor)
})
observeEvent(input$plot_selector, {
# Update and run plot expression
selected_plot_code <- plots[[as.integer(input$plot_selector)]]
updateAceEditor(session, "code_editor", value = selected_plot_code)
run_code_editor(selected_plot_code)
# Reset layer ID
updateNumericInput(session, "layer_selector", value = 1)
update_i(1L)
# Update and run inspect expression
update_and_run_inspect_expr()
})
# Initialize plot
output$plot_preview <- renderPlot({
user_env$p
})
cur_layer <- function() {
evalq(p$layers[[i]], user_env)
}
update_i <- function(value) {
# Update value in env
unlockBinding("i", user_env)
user_env$i <- value
lockBinding("i", user_env)
# Update display
layer_fn <- deparse1(cur_layer()$constructor[1])
output$selected_layer_fn <- renderText({ layer_fn })
}
# Update i when layer_selector changes
observeEvent(input$layer_selector, {
update_i(input$layer_selector)
# Update and run inspect expression
update_and_run_inspect_expr()
})
# Update expression when inspect_type changes
observeEvent(input$inspect_type, {
# Update and run inspect expression
update_and_run_inspect_expr()
})
# Run data diff button
observeEvent(input$show_input_output_diff, {
input_call <- fn_to_expr(input$selected_function, "input")
output_call <- fn_to_expr(input$selected_function, "output")
input_data <- tryCatch(
eval(local_call(input_call), envir = user_env),
error = function(e) e
)
output_data <- tryCatch(
eval(local_call(output_call), envir = user_env),
error = function(e) e
)
comparison <- compare_input_output(input_data, output_data)
output$diff_result <- renderPrint({ comparison })
showModal(
modalDialog(
title = "Data diff",
verbatimTextOutput("diff_result"),
easyClose = TRUE,
footer = NULL,
size = "xl",
open = TRUE
)
)
})
# Layer methods info button
observeEvent(input$show_layer_methods, {
thelayer <- cur_layer()
output$layer_constructor_text <- renderText({
deparse1(thelayer$constructor)
})
layer_methods <- show_sublayer_methods(thelayer)
showModal(
modalDialog(
title = "Layer information",
div(
verbatimTextOutput("layer_constructor_text"),
renderTable(
layer_methods,
striped = TRUE,
hover = TRUE,
bordered = TRUE,
spacing = 'xs',
width = '100%'
)
),
easyClose = TRUE,
footer = NULL,
size = "xl",
open = TRUE
)
)
})
run_inspect_expr <- function(expr) {
if (!nzchar(expr)) return(NULL)
res <- tryCatch(
eval(local_call(expr), envir = user_env),
error = function(e) e
)
if (inherits(res, "grob")) {
output$function_output_ui <- renderUI({
tagList(
plotOutput("grob_plot_output", height = "300px"),
reactjsonOutput("str_output")
)
})
output$grob_plot_output <- renderPlot({
grid.newpage()
pushViewport(viewport())
grid.draw(res)
})
output$str_output <- renderReactjson({
reactjson(
jsonlite::toJSON(unclass(res), force = TRUE),
collapsed = 1,
enableClipboard = FALSE,
displayObjectSize = FALSE, displayDataTypes = FALSE,
onEdit = FALSE, onAdd = FALSE, onDelete = FALSE, onSelect = FALSE
)
})
} else if (inherits(res, "data.frame")) {
output$function_output_ui <- renderUI({
DTOutput("table_output")
})
output$table_output <- renderDT({
res |>
datatable(
extensions = "Scroller",
options = list(
dom = "t",
deferRender = TRUE,
scrollY = 400,
scroller = TRUE
),
fillContainer = TRUE
) |>
formatRound(which(sapply(res, is.roundable), 2))
})
} else {
output$function_output_ui <- renderUI({
verbatimTextOutput("text_output")
})
output$text_output <- renderPrint({
res
})
}
}
run_highjack_expr <- function(expr) {
if (!nzchar(expr)) return(NULL)
inspect_res <- tryCatch(
eval(local_call(expr), envir = user_env),
error = function(e) e
)
method_expr <- parse_expr(resolve_fn(input$selected_function, user_env))
layer_id_expr <- call2("layer_is", input$layer_selector)
highjack_expr <- call2(
paste0("highjack_", resolve_inspect_type(input$inspect_type)),
x = sym("p"),
method = method_expr,
cond = layer_id_expr,
value = sym("new_value")
)
highjack_res <- tryCatch(
eval_tidy(
local_call(highjack_expr),
list(new_value = inspect_res),
user_env
),
error = function(e) e
)
untrace_expr <- call2("gguntrace", method_expr, .ns = "ggtrace")
eval(call2("suppressMessages", untrace_expr), user_env)
if (is_error(highjack_res)) {
fn_call <- fn_to_expr(input$selected_function, input$inspect_type)
og_res <- eval(parse_expr(fn_call), user_env)
output$function_output_ui <- renderUI({
verbatimTextOutput("text_output")
})
output$text_output <- renderPrint({
cat("! Error: ", highjack_res$message, "\n")
cat("i Check the data type carefully\n")
cat(format(
waldo::compare(
og_res, inspect_res, list_as_map = TRUE,
x_arg = "Original value", y_arg = "Highjacked value"
)
))
})
return(NULL)
}
if (!inherits(highjack_res, "ggtrace_highjacked")) {
output$text_output <- renderPrint({
stop("Expression failed to generate a highjacked ggplot")
})
} else {
output$function_output_ui <- renderUI({
div(
style = "font-size: 0.8rem",
verbatimTextOutput("highjack_expr_putput"),
plotOutput("highjack_plot_output", height = "300px")
)
})
output$highjack_expr_putput <- renderText({
names(highjack_expr)[names(highjack_expr) != "value"] <- ""
deparse1(highjack_expr)
})
output$highjack_plot_output <- renderPlot({
grid.newpage()
pushViewport(viewport())
grid.draw(highjack_res)
})
}
}
# Run function expressions
observeEvent(input$run_inspect_expr_btn, {
current_expr(input$function_expr)
run_inspect_expr(input$function_expr)
})
observeEvent(input$run_highjack_expr_btn, {
current_expr(input$function_expr)
run_highjack_expr(input$function_expr)
})
# Handle radio button selection
observeEvent(input$selected_function, {
if (!is.null(input$selected_function)) {
# Update and run inspect expression
update_and_run_inspect_expr()
}
})
# Initial function output
output$function_output_ui <- renderUI({ verbatimTextOutput("text_output") })
output$text_output <- renderPrint({
cat(input$function_expr)
})
if (!in_webr()) {
observeEvent(input$debug_btn, { browser() })
}
}
shinyApp(ui, server, options = list(launch.browser = TRUE))