Let's create a package, use dplyr
and the magrittr
pipe, and craft a new function:
usethits::create_package("TidyEvalInsidePackage")
usethis::use_package_doc()
usethis::use_package("dplyr")
usethis::use_pipe(export = F)
devtools::document()
usethis::use_r("foo")
Inside foo.R
:
foo <- function() {
df <- data.frame(N = 1:10)
df %>%
dplyr::select(N) ## This will produce a note when I run devtools::check()
}
Produces: foo: no visible binding for global variable 'N'
This note could be easliy removed with this:
df %>%
dplyr::select( df$N )
But now if want to use mutate like this:
foo <- function() {
df <- data.frame(N = 1:10)
df %>%
dplyr::mutate(X = pi^N,
Y = X / N))
}
The trick no longer works:
df
%>% dplyr::mutate(X = pi^df$N,
Y = df$X / df$N) ## coz at this line df$X does not exist
Is there any way to preserve the pipe workflow and do this without producing notes when checking the package?
EDIT: @Hong oi proposed a workaround and decided to close the question but the workaround is not the proper way to do it.
The cleaner way is to use the .data$var
construct. This is where I have found the answer : https://community.rstudio.com/t/how-to-solve-no-visible-binding-for-global-variable-note/28887/3