0

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

pietrodito
  • 1,783
  • 15
  • 24
  • `utils::globalVariables` – Hong Ooi Mar 21 '21 at 15:30
  • 1
    Please consider my edit, this is not a duplicate and there is cleaner way than the workaround you use. – pietrodito Mar 21 '21 at 16:01
  • @pietrodito I know it's a bit late (OK, a lot late), but I found your question in the reopen queue. I've added some other duplicate targets, but if you still disagree that it's a dup, I will reopen it for you. Just @ mention me. – Ian Campbell May 12 '21 at 05:01

0 Answers0