Skip to content

Commit 449bc03

Browse files
committed
Remove dependency on plyr::as.quoted()
1 parent dc3c985 commit 449bc03

File tree

5 files changed

+38
-18
lines changed

5 files changed

+38
-18
lines changed

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,6 @@ import(grid)
560560
import(gtable)
561561
import(scales)
562562
importFrom(lazyeval,f_eval)
563-
importFrom(plyr,as.quoted)
564563
importFrom(plyr,defaults)
565564
importFrom(rlang,.data)
566565
importFrom(rlang,enexpr)

R/facet-.r

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,7 @@ as_facets_list <- function(x) {
290290
# facet_wrap() called as.quoted(). Hence this is a little more
291291
# complicated for backward compatibility.
292292
if (rlang::is_string(x)) {
293-
x <- plyr::as.quoted(x)
294-
if (rlang::is_formula(x[[1]])) {
295-
x <- x[[1]]
296-
}
293+
x <- rlang::parse_expr(x)
297294
}
298295

299296
# At this level formulas are coerced to lists of lists for backward
@@ -306,13 +303,12 @@ as_facets_list <- function(x) {
306303

307304
# For backward-compatibility with facet_wrap()
308305
if (!rlang::is_bare_list(x)) {
309-
x <- plyr::as.quoted(x)
310-
attributes(x) <- NULL
306+
x <- as_quoted(x)
311307
}
312308

313309
# If we have a list there are two possibilities. We may already have
314310
# a proper facet spec structure. Otherwise we coerce each element
315-
# with plyr::as.quoted() for backward compatibility with facet_grid().
311+
# with as_quoted() for backward compatibility with facet_grid().
316312
if (is.list(x)) {
317313
x <- lapply(x, as_facets)
318314
}
@@ -324,6 +320,38 @@ as_facets_list <- function(x) {
324320
x
325321
}
326322

323+
# Compatibility with plyr::as.quoted()
324+
as_quoted <- function(x) {
325+
if (is.character(x)) {
326+
return(rlang::parse_exprs(x))
327+
}
328+
if (is.null(x)) {
329+
return(list())
330+
}
331+
if (rlang::is_formula(x)) {
332+
return(simplify(x))
333+
}
334+
list(x)
335+
}
336+
# From plyr:::as.quoted.formula
337+
simplify <- function(x) {
338+
if (length(x) == 2 && rlang::is_symbol(x[[1]], "~")) {
339+
return(simplify(x[[2]]))
340+
}
341+
if (length(x) < 3) {
342+
return(list(x))
343+
}
344+
op <- x[[1]]; a <- x[[2]]; b <- x[[3]]
345+
346+
if (rlang::is_symbol(op, c("+", "*", "~"))) {
347+
c(simplify(a), simplify(b))
348+
} else if (rlang::is_symbol(op, "-")) {
349+
c(simplify(a), expr(-!!simplify(b)))
350+
} else {
351+
list(x)
352+
}
353+
}
354+
327355
f_as_facets_list <- function(f) {
328356
lhs <- function(x) if (length(x) == 2) NULL else x[-3]
329357
rhs <- function(x) if (length(x) == 2) x else x[-2]
@@ -352,7 +380,7 @@ as_facets <- function(x) {
352380
# environment correctly.
353381
f_as_facets(x)
354382
} else {
355-
vars <- plyr::as.quoted(x)
383+
vars <- as_quoted(x)
356384
rlang::as_quosures(vars, globalenv(), named = TRUE)
357385
}
358386
}

R/facet-grid-.r

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ NULL
144144
#' mg + facet_grid(vs + am ~ gear, margins = "gear")
145145
#' mg + facet_grid(vs + am ~ gear, margins = c("gear", "am"))
146146
#' }
147-
#' @importFrom plyr as.quoted
148147
facet_grid <- function(rows = NULL, cols = NULL, scales = "fixed",
149148
space = "fixed", shrink = TRUE,
150149
labeller = "label_value", as.table = TRUE,

tests/testthat/helper-facet.r

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,3 @@ quos_list <- function(...) {
44
names(x) <- rlang::names2(x)
55
structure(x, class = "quosures")
66
}
7-
8-
quoted_obj <- structure(list(), class = "quoted_obj")
9-
as.quoted.quoted_obj <- function(...) plyr::as.quoted(quote(dispatched), globalenv())
10-
assign("as.quoted.quoted_obj", as.quoted.quoted_obj, envir = globalenv())

tests/testthat/test-facet-.r

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@ test_that("as_facets_list() coerces lists", {
2626
out <- as_facets_list(list(
2727
quote(foo),
2828
c("foo", "bar"),
29-
NULL,
30-
quoted_obj
29+
NULL
3130
))
3231
exp <- c(
3332
as_facets_list(quote(foo)),
3433
list(do.call(base::`c`, as_facets_list(c("foo", "bar")))),
35-
list(quos_list()),
36-
as_facets_list(quoted_obj)
34+
list(quos_list())
3735
)
3836
expect_identical(out, exp)
3937
})

0 commit comments

Comments
 (0)