Search code examples
rtidyeval

no visible global function definition for ':='


I am writing a package that uses tidyeval. Because I use tidyeval I have rlang listed under imports in the description file.

One of the functions contains a few lines that use :=

Like this:

data %>%
    dplyr::mutate(
      !!New_R := AP_R_X*!!X + AP_R_Y*!!Y + AP_R_Z*!!Z,
      !!New_U := AP_U_X*!!X + AP_U_Y*!!Y + AP_U_Z*!!Z,
      !!New_F := AP_F_X*!!X + AP_F_Y*!!Y + AP_F_Z*!!Z) 

The code works as intended but I get the following note when running devtools::check()

 no visible global function definition for ':='

How can I get rid of this note? Is this not a part of rlang evaluation?


EDIT: I have read the question "no visible global function definition for ‘median’, although the answers there explain why such a problem can arise. It does not explain why := is not defined when I have imported rlang. I have edited the question to make this more clear.


Solution

  • Update from June 2023

    You should run usethis::use_import_from("rlang", ":=").

    usethis::use_import_from("rlang", ":=") will import the := function from rlang using the @importFrom tag in the package level documentation. For more information, check out the documentation here.

    This will change you package level documentation to look something like:

    @keywords internal
    "_PACKAGE"
    
    ## usethis namespace: start
    #' @importFrom rlang :=
    ## usethis namespace: end
    NULL
    

    Read more about this documentation in the R packages book here and here.

    If you would like to use more tidy-eval functions from rlang, you should import them using usethis::use_import_from().

    usethis::use_tidy_eval has been marked as defunct as of usethis 2.2.0 since it imports and re-exports an unnecessary amount of functions (see the issue here and the release notes here.

    Older Update (March 2023)

    I wanted to add an update as of 2023 - you can now run usethis::use_tidy_eval() to handle this issue. The function will automatically import rlang::dyn-dots, or rlang::`:=` , along with rlang::enquo, rlang::enquos, rlang::.data, rlang::as_name, and rlang::as_label to take care of potential notes that will appear while using tidyeval. These will appear in a new file titled utils-tidy-eval.R. For a little more info, the documentation is here.

    This function will essentially do the same thing as the accepted answer, but it creates a new file to hold these @importFrom functions, and it creates documentation for these imports within your package.

    TLDR: Just run usethis::use_tidy_eval()