问题
I am writing a package that uses tidyeval. Becuase 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.
回答1:
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.
来源:https://stackoverflow.com/questions/58026637/no-visible-global-function-definition-for