After you updated your answer, to me, this is sort of on the line of whether it's a full duplicate or not. The only difference here is that you've added rlang
to Imports
in DESCRIPTION
and haven't seen the difference between that and a NAMESPACE
directive.
I mocked up an example package to show this isn't sufficient. First, I set up the package:
library(devtools)
create("anExample", rstudio = FALSE, open = FALSE)
Then, I add the example function from https://dplyr.tidyverse.org/articles/programming.html to a file R/my_mutate.R
:
#' A function
#'
#' @param df A dataframe
#' @param expr A variable in the dataframe
#'
#' @return The dataframe with new mean and sum columns
#' @export
my_mutate <- function(df, expr) {
expr <- enquo(expr)
mean_name <- paste0("mean_", quo_name(expr))
sum_name <- paste0("sum_", quo_name(expr))
mutate(df,
!! mean_name := mean(!! expr),
!! sum_name := sum(!! expr)
)
}
Notice there are no roxygen2
namespace tags. I make sure to add rlang
and dplyr
to Imports
in DESCRIPTION
and run devtools::document()
. Then when I run devtools::check()
I get the following:
my_mutate: no visible global function definition for ‘enquo’
my_mutate: no visible global function definition for ‘quo_name’
my_mutate: no visible global function definition for ‘mutate’
my_mutate: no visible global function definition for ‘:=’
Undefined global functions or variables:
:= enquo mutate quo_name
0 errors ✔ | 1 warning ✖ | 1 note ✖
However, if I change R/my_mutate.R
to the following:
#' A function
#'
#' @param df A dataframe
#' @param expr A variable in the dataframe
#'
#' @return The dataframe with new mean and sum columns
#' @importFrom dplyr mutate
#' @importFrom rlang enquo
#' @importFrom rlang quo_name
#' @importFrom rlang :=
#' @export
my_mutate <- function(df, expr) {
expr <- enquo(expr)
mean_name <- paste0("mean_", quo_name(expr))
sum_name <- paste0("sum_", quo_name(expr))
mutate(df,
!! mean_name := mean(!! expr),
!! sum_name := sum(!! expr)
)
}
When I run devtools::check()
(after re-document()
ing), I do not get that note.
Long story short, Import
in DESCRIPTION
is not sufficient. You also need NAMESPACE
directives.