Skip to content

Commit 5312b1a

Browse files
author
Jordan S Read
committed
for #237
1 parent 36f805f commit 5312b1a

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

R/embedded-functions.R

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,17 @@ is_in_package <- function(x){
99
separate_args <- function(...){
1010

1111
dots <- lazy_dots(...)
12-
args = list(args=dots,e.fun=c(),e.args=c())
12+
args = list(args=dots)
1313

1414
embeds <- unname(sapply(dots, function(x) is_in_package(x$expr)))
15-
if (sum(embeds) > 1)
16-
stop('only one embedded function is currently supported')
17-
else if (sum(embeds) == 0)
15+
16+
if (sum(embeds) == 0)
1817
return(args)
1918

20-
embedded.funs <- dots[[which(embeds)]]
21-
dots[[which(embeds)]] <- NULL
22-
args$args = dots
23-
args$e.fun = as.character(embedded.funs$expr[[1]])
24-
embedded.funs$expr[[1]] <- NULL
25-
args$e.args = embedded.funs$expr
19+
embedded.funs <- dots[which(embeds)]
20+
args$args = dots[which(!embeds)]
21+
fun.names = sapply(embedded.funs, function(x) as.character(x$expr[[1]]))
22+
args$e.funs = lapply(embedded.funs, function(x) {x$expr[[1]] <- NULL; return(x$expr)}) %>%
23+
setNames(fun.names)
2624
return(args)
2725
}

R/lines.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ lines.gsplot <- function(object, ..., legend.name=NULL, side=c(1,2)){
5353

5454
object <- gsplot(append(object, to.gsplot)) # append initial call
5555
if (!is.null(e.fun)){
56-
embed.args = set_inherited_args(e.fun, arguments, dots$e.args)
57-
object <- do.call(e.fun, append(list(object=object), embed.args))
56+
for (i in seq_len(length(e.fun))){
57+
fun.name = names(e.fun)[i]
58+
embed.args = set_inherited_args(fun.name, arguments, e.fun[[i]])
59+
object <- do.call(fun.name, append(list(object=object), embed.args))
60+
}
5861
}
5962
return(object)
6063
}

R/points.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ points.gsplot <- function(object, ..., legend.name=NULL, side=c(1,2)){
5656

5757
object <- gsplot(append(object, to.gsplot)) # append initial call
5858
if (!is.null(e.fun)){
59-
embed.args = set_inherited_args(e.fun, arguments, dots$e.args)
60-
object <- do.call(e.fun, append(list(object=object), embed.args))
59+
for (i in seq_len(length(e.fun))){
60+
fun.name = names(e.fun)[i]
61+
embed.args = set_inherited_args(fun.name, arguments, e.fun[[i]])
62+
object <- do.call(fun.name, append(list(object=object), embed.args))
63+
}
6164
}
6265
return(object)
6366
}

tests/testthat/tests-embed_function.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,15 @@ test_that("usr args aren't overidden", {
2121
gs <- points(gsplot(), c(0,3), c(2,4), callouts(labels=c('dogs','cats'), col='yellow'))
2222
expect_equal(gs$view$callouts$col, 'yellow')
2323

24+
})
25+
26+
test_that("multiple functions can be embedded", {
27+
28+
gs <- lines(gsplot(), c(0,3), c(2,4), callouts(labels=c('dogs','cats'), col='yellow'), error_bar(x.low=c(NA,1), col='red'), error_bar(x.low=c(0.5,NA), col='green'))
29+
expect_equal(gs$view$callouts$col, 'yellow')
30+
expect_equal(gs$view[[3]]$col, 'red')
31+
expect_equal(gs$view[[4]]$col, 'green')
32+
gs <- points(gsplot(), c(0,3), c(2,4), callouts(labels=c('dogs','cats'), col='yellow'))
33+
expect_equal(gs$view$callouts$col, 'yellow')
34+
2435
})

0 commit comments

Comments
 (0)