| 1 |
test_that("Render functions correctly handle quosures", { |
| 2 |
# Normally, quosures are not unwrapped at creation time. |
| 3 |
# However, using inject() will make it unwrap at creation time. |
| 4 |
|
| 5 |
a <- 1 |
| 6 |
r1 <- inject(renderText({ !!a })) |
| 7 |
r2 <- renderText({ eval_tidy(quo(!!a)) }) |
| 8 |
a <- 2 |
| 9 |
expect_identical(r1(), "1") |
| 10 |
expect_identical(r2(), "2") |
| 11 |
|
| 12 |
a <- 1 |
| 13 |
r1 <- inject(renderPrint({ !!a })) |
| 14 |
r2 <- renderPrint({ eval_tidy(quo(!!a)) }) |
| 15 |
a <- 2 |
| 16 |
expect_identical(r1(), "[1] 1") |
| 17 |
expect_identical(r2(), "[1] 2") |
| 18 |
|
| 19 |
a <- 1 |
| 20 |
r1 <- inject(renderUI({ tags$p(!!a) })) |
| 21 |
r2 <- renderUI({ eval_tidy(quo(tags$p(!!a))) }) |
| 22 |
a <- 2 |
| 23 |
res1 <- r1(shinysession = MockShinySession$new(), name = "foo") |
| 24 |
expect_identical(as.character(res1$html), "<p>1</p>") |
| 25 |
res2 <- r2(shinysession = MockShinySession$new(), name = "foo") |
| 26 |
expect_identical(as.character(res2$html), "<p>2</p>") |
| 27 |
|
| 28 |
a <- 1 |
| 29 |
r1 <- inject(renderTable({ pressure[!!a, ] }, digits = 1)) |
| 30 |
r2 <- renderTable({ eval_tidy(quo(pressure[!!a, ])) }, digits = 1) |
| 31 |
a <- 2 |
| 32 |
expect_match(r1(), "0\\.0") |
| 33 |
expect_match(r2(), "20\\.0") |
| 34 |
}) |
| 35 |
|
| 36 |
test_that("functionLabel returns static value when the label can not be assigned to", { |
| 37 |
getFunc <- function(exprF, envF = parent.frame(), quotedF = FALSE) { |
| 38 |
quoToFunction(enquo0(exprF)) |
| 39 |
} |
| 40 |
|
| 41 |
expect_label <- function(func, labely) { |
| 42 |
expect_equal( |
| 43 |
as.character(body(func)[[2]][[1]]), |
| 44 |
labely |
| 45 |
) |
| 46 |
} |
| 47 |
|
| 48 |
a <- 1 |
| 49 |
|
| 50 |
expect_label( |
| 51 |
getFunc({a + 1}), |
| 52 |
"getFunc" |
| 53 |
) |
| 54 |
|
| 55 |
# multiline labels are not supported |
| 56 |
expect_label( |
| 57 |
(function(exprF) { |
| 58 |
quoToFunction(enquo0(exprF)) |
| 59 |
})(), |
| 60 |
"anonymous" |
| 61 |
) |
| 62 |
# parents are not supported |
| 63 |
expect_label( |
| 64 |
(function(exprF) {quoToFunction(enquo0(exprF))})(), |
| 65 |
"anonymous" |
| 66 |
) |
| 67 |
}) |
| 68 |
|
| 69 |
local({ |
| 70 |
|
| 71 |
# (must also copy logic into `lower - quoToFunction(enquo0(expr))` code) |
| 72 |
return_func <- function(func) { |
| 73 |
function() { |
| 74 |
value <- func() |
| 75 |
list(value, value) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
for (info in list( |
| 80 |
list( |
| 81 |
name = "exprToFunction(expr, env, quoted)", |
| 82 |
fn = function(exprF, envF = parent.frame(), quotedF = FALSE) { |
| 83 |
func <- exprToFunction(exprF, envF, quotedF) |
| 84 |
return_func(func) |
| 85 |
} |
| 86 |
), |
| 87 |
list( |
| 88 |
name = "exprToFunction(expr, env, quoted = TRUE)", |
| 89 |
fn = function(exprF, envF = parent.frame(), quotedF = FALSE) { |
| 90 |
# `exprF` coudl be a raw quosure if `inject()`ed |
| 91 |
if (!quotedF) exprF <- substitute(exprF) |
| 92 |
func <- exprToFunction(exprF, envF, quoted = TRUE) |
| 93 |
return_func(func) |
| 94 |
} |
| 95 |
), |
| 96 |
list( |
| 97 |
name = "exprToFunction(expr, env, quoted = TRUE) + force()", |
| 98 |
fn = function(exprF, envF = parent.frame(), quotedF = FALSE) { |
| 99 |
# Make `exprF` always language, even if `inject()`ed |
| 100 |
if (!quotedF) exprF <- substitute(force(exprF)) |
| 101 |
func <- exprToFunction(exprF, envF, quoted = TRUE) |
| 102 |
return_func(func) |
| 103 |
} |
| 104 |
), |
| 105 |
list( |
| 106 |
name = "installExprFunction(expr, \"func\", env, quoted)", |
| 107 |
fn = function(exprF, envF = parent.frame(), quotedF = FALSE) { |
| 108 |
a <- 1000 |
| 109 |
installExprFunction(exprF, "func", envF, quotedF) |
| 110 |
return_func(func) |
| 111 |
} |
| 112 |
), |
| 113 |
list( |
| 114 |
name = "installExprFunction(expr, \"func\", env, quoted = TRUE)", |
| 115 |
fn = function(exprF, envF = parent.frame(), quotedF = FALSE) { |
| 116 |
a <- 1000 |
| 117 |
# `exprF` coudl be a raw quosure if `inject()`ed |
| 118 |
if (!quotedF) exprF <- substitute(exprF) |
| 119 |
installExprFunction(exprF, "func", envF, quoted = TRUE) |
| 120 |
return_func(func) |
| 121 |
} |
| 122 |
), |
| 123 |
list( |
| 124 |
name = "installExprFunction(expr, \"func\", env, quoted = TRUE)", |
| 125 |
fn = function(exprF, envF = parent.frame(), quotedF = FALSE) { |
| 126 |
a <- 1000 |
| 127 |
# Make `exprF` always language, even if `inject()`ed |
| 128 |
if (!quotedF) exprF <- substitute(force(exprF)) |
| 129 |
installExprFunction(exprF, "func", envF, quoted = TRUE) |
| 130 |
return_func(func) |
| 131 |
} |
| 132 |
), |
| 133 |
list( |
| 134 |
name = "quoToFunction(enquo0(expr))", |
| 135 |
fn = function(expr) { |
| 136 |
func <- quoToFunction(enquo0(expr)) |
| 137 |
return_func(func) |
| 138 |
} |
| 139 |
), |
| 140 |
list( |
| 141 |
name = "lower - quoToFunction(enquo0(expr))", |
| 142 |
fn = function(expr) { |
| 143 |
function() { |
| 144 |
func <- quoToFunction(enquo0(expr)) |
| 145 |
value <- func() |
| 146 |
list(value, value) |
| 147 |
} |
| 148 |
} |
| 149 |
) |
| 150 |
)) { |
| 151 |
|
| 152 |
# Scope the local variables |
| 153 |
local({ |
| 154 |
renderH <- info$fn %||% stop("`info$fn` not found") |
| 155 |
|
| 156 |
# Different usages of env and quoted param |
| 157 |
a <- 1 |
| 158 |
e <- new.env() |
| 159 |
e$a <- 10 |
| 160 |
|
| 161 |
test_that(paste0("vanilla: ", info$name), { |
| 162 |
val <- renderH({a + 1})() |
| 163 |
expect_identical(val, list(2, 2)) |
| 164 |
}) |
| 165 |
|
| 166 |
# Test that no error is thrown when the function is created |
| 167 |
# This proves that the expression is not immediately evaluated |
| 168 |
test_that(paste0("stop('boom'): ", info$name), { |
| 169 |
expect_error( |
| 170 |
renderH(stop("boom")), |
| 171 |
NA |
| 172 |
) |
| 173 |
}) |
| 174 |
|
| 175 |
if (length(formals(renderH)) > 1) { |
| 176 |
test_that(paste0("quoted = FALSE: ", info$name), { |
| 177 |
r <- renderH(a + 1, quotedF = FALSE) |
| 178 |
expect_identical(r(), list(2, 2)) |
| 179 |
}) |
| 180 |
|
| 181 |
test_that(paste0("quoted = TRUE: ", info$name), { |
| 182 |
r <- renderH(quote(a + 1), quotedF = TRUE) |
| 183 |
expect_identical(r(), list(2, 2)) |
| 184 |
}) |
| 185 |
|
| 186 |
test_that(paste0("env = e: ", info$name), { |
| 187 |
r <- renderH(a + 1, envF = e) |
| 188 |
expect_identical(r(), list(11, 11)) |
| 189 |
}) |
| 190 |
|
| 191 |
test_that(paste0("env = e, quoted = FALSE: ", info$name), { |
| 192 |
r <- renderH(a + 1, envF = e, quotedF = FALSE) |
| 193 |
expect_identical(r(), list(11, 11)) |
| 194 |
}) |
| 195 |
|
| 196 |
test_that(paste0("env = e, quoted = TRUE: ", info$name), { |
| 197 |
r <- renderH(quote(a + 1), envF = e, quotedF = TRUE) |
| 198 |
expect_identical(r(), list(11, 11)) |
| 199 |
}) |
| 200 |
|
| 201 |
test_that(paste0("Works with raw quosures, quoted = FALSE: ", info$name), { |
| 202 |
e <- list2env(list(a=10)) |
| 203 |
x <- new_quosure(quote({ a + 1 }) , env = e) |
| 204 |
r <- renderH(x, quotedF = FALSE) |
| 205 |
expect_identical(r(), list(x, x)) |
| 206 |
}) |
| 207 |
test_that(paste0( |
| 208 |
"Passing in a raw quosures, quoted = FALSE, env = otherenv", |
| 209 |
" is treated like an expression: ", info$name), |
| 210 |
{ |
| 211 |
e <- list2env(list(a=10)) |
| 212 |
x <- new_quosure(quote({ a + 1 }) , env = e) |
| 213 |
other_env <- list2env(list(x=20)) |
| 214 |
r <- renderH(x, quotedF = FALSE, envF = e) |
| 215 |
expect_identical(r(), list(x, x)) |
| 216 |
}) |
| 217 |
test_that( |
| 218 |
paste0("Works with injected quosures, quoted = FALSE, env = otherenv: ", info$name), { |
| 219 |
e <- list2env(list(a=10)) |
| 220 |
x <- new_quosure(quote({ a + 1 }) , env = e) |
| 221 |
other_env <- new.env(parent = emptyenv()) |
| 222 |
r <- inject(renderH(!!x, quotedF = FALSE, envF = e)) |
| 223 |
expect_identical(r(), list(11, 11)) |
| 224 |
}) |
| 225 |
test_that(paste0("Works with raw quosures, quoted = TRUE: ", info$name), { |
| 226 |
e <- list2env(list(a=10)) |
| 227 |
x <- new_quosure(quote({ a + 1 }) , env = e) |
| 228 |
ans <- renderH(x, quotedF = TRUE)() |
| 229 |
expect_identical(ans, list(11, 11)) |
| 230 |
}) |
| 231 |
test_that(paste0("Works with injecting raw quosures: ", info$name), { |
| 232 |
e <- list2env(list(a=10)) |
| 233 |
x <- new_quosure(quote({ a + 1 }) , env = e) |
| 234 |
ans <- inject(renderH(!!x))() |
| 235 |
expect_identical(ans, list(11, 11)) |
| 236 |
}) |
| 237 |
|
| 238 |
test_that(paste0("Missing env with quosure, quoted = TRUE: ", info$name), { |
| 239 |
e <- list2env(list(a=10)) |
| 240 |
x <- new_quosure(quote({ a + 1 }) , env = e) |
| 241 |
|
| 242 |
ans <- renderH(x, envF = rlang::missing_arg(), quotedF = TRUE)() |
| 243 |
expect_identical(ans, list(11, 11)) |
| 244 |
}) |
| 245 |
|
| 246 |
} |
| 247 |
|
| 248 |
test_that(paste0("Works with inject / !!: ", info$name), { |
| 249 |
# Quosures |
| 250 |
a <- 1 |
| 251 |
r1 <- inject(renderH({ !!a })) |
| 252 |
r2 <- renderH({ eval_tidy(quo(!!a)) }) |
| 253 |
a <- 100 |
| 254 |
expect_identical(r1(), list(1,1)) |
| 255 |
expect_identical(r2(), list(100, 100)) |
| 256 |
}) |
| 257 |
}) |
| 258 |
} |
| 259 |
}) |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
test_that("nested observe events work with exprToFunction", { |
| 264 |
|
| 265 |
val <- 0 |
| 266 |
|
| 267 |
local({ |
| 268 |
t1 <- reactiveVal(0) |
| 269 |
t2 <- reactiveVal(10) |
| 270 |
observeEvent( |
| 271 |
{ |
| 272 |
# message("outer observeEvent trigger") |
| 273 |
val <<- val + 1 |
| 274 |
t1() |
| 275 |
}, |
| 276 |
{ |
| 277 |
# message("outer observeEvent handler") |
| 278 |
val <<- val + 2 |
| 279 |
observeEvent( |
| 280 |
{ |
| 281 |
# message("inner observeEvent trigger") |
| 282 |
val <<- val + 3 |
| 283 |
t2() |
| 284 |
}, |
| 285 |
{ |
| 286 |
val <<- val + 4 |
| 287 |
# message("inner observeEvent handler") |
| 288 |
} |
| 289 |
) |
| 290 |
} |
| 291 |
) |
| 292 |
}) |
| 293 |
|
| 294 |
expect_equal(val, 0) |
| 295 |
flushReact() |
| 296 |
expect_equal(val, 1 + 2 + 3 + 4) |
| 297 |
}) |