Search code examples
rpackagedevtoolstidyeval

rlang::expr(`=`(!!rlang::ensym(x), !!rlang::as_name(y))) causes weird note in devtools::check()


I use the following code in a function that is part of an R package I am writing:

x = "a"
y = "b"

rlang::expr(`=`(!!rlang::ensym(x), !!rlang::as_name(y)))

It automatically creates arguments for a function further down the line that look like this:

a = "b"

Which can then be plugged into a function like this:

foo(a = "b")

The problem is that when I run the devtools::check() function I get a note due to this part of the code.

my_function : <anonymous>: no visible global function definition for '!<-'

I assume the problem is the bang-bang (!!) together with the = function but I could not really figure out how to solve this.

Would be great if someone has an idea of what to do to prevent this note from occurring! Thanks a lot!

Edit: Based on MrFlick answer I am now using the following:

x = c("a", "b")
y = c("y", "z")

args <- purrr::map2(.x = x,
            .y = y,
            .f = function(x, y){
            rlang::exprs(!!rlang::as_name(x) := !!y)
            })

rlang::expr(foo(!!!unlist(args)))

Solution

  • The CRAN checks don't really like non-standard evaluation so when it sees you calling the = function which it interprets as the <- function, it doesn't like it.

    The rlang package gets around this by defining the := operator when you are trying to dynamically build named parameters. So instead you can build your arguments with

    args <- rlang::exprs(!!rlang::as_name(x) := !!y)
    

    And then inject them into the call with

    rlang::expr(foo(!!!args))
    # foo(a = "b")
    

    That should prevent CRAN from trying to find the special assignment operator and is generally how one should use rlang for such a purpose.