Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dots_list() fails when called via lapply() #580

Closed
mikmart opened this issue Aug 21, 2018 · 3 comments
Closed

dots_list() fails when called via lapply() #580

mikmart opened this issue Aug 21, 2018 · 3 comments
Labels

Comments

@mikmart
Copy link
Contributor

@mikmart mikmart commented Aug 21, 2018

It seems dots_list() fails when called via lapply():

lapply(list(quote(a)), rlang::dots_list)
#> Error in FUN(X[[i]], ...): object 'a' not found

I would expect the above to be equivalent to:

list(rlang::dots_list(quote(a)))
#> [[1]]
#> [[1]][[1]]
#> a

This came up when I tried to construct several calls from expressions:

lapply(rlang::exprs(a, b), rlang::call2, .fn = quote(f))
#> Error in dots_list(...): object 'a' not found

I was hoping to get:

rlang::exprs(f(a), f(b))
#> [[1]]
#> f(a)
#> 
#> [[2]]
#> f(b)

Created on 2018-08-21 by the reprex package (v0.2.0.9000).

(Originally accidentally posted in tidyverse/purrr#534)

@egnha
Copy link
Contributor

@egnha egnha commented Aug 21, 2018

@mikmart, an aside: although not addressing the root problem, if you just want to construct lists of calls, you can do that succinctly with base R: lapply(alist(a, b), function(arg) call("f", arg)). (Though I suppose you already knew that.)

@mikmart
Copy link
Contributor Author

@mikmart mikmart commented Aug 21, 2018

@egnha thanks, I actually hadn't thought of that alternative. Turns out that doesn't quite work for what I wanted to do though. More generally, I was looking to replace this anonymous function pattern that I found myself using in map() functions: function(x) rlang::expr((!!f)(!!x, !!!dots)); where f and dots come as arguments to the enclosing function, and f can for example be an anonymous function, too. I thought call2 would be a good fit for this: but maybe there's a better way?

@egnha
Copy link
Contributor

@egnha egnha commented Aug 21, 2018

In this case, where you're mapping over x for given a f and dots, you might want to consider using as.call() instead or call() or rlang::expr(), e.g.,

make_calls <- function(f, args, dots) {
  lapply(args, function(arg) as.call(c(f, arg, dots)))
}

make_calls(quote(f), alist(a, b), list(x = 1, 2))
#> [[1]]
#> f(a, x = 1, 2)
#> 
#> [[2]]
#> f(b, x = 1, 2)

(f may also be a function literal.)

@lionel- lionel- closed this in 8d42e79 Sep 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants