No visible global function definition/no visible binding for global variable in a project?

1.4k Views Asked by At

I'm working with R in VS Code, using the R Extension. I do almost all of my project work using targets, which means that my package imports and functions are usually in separate files.

A simple example would be working in a folder which looks like

project
│   packages.R
│   README.md
│   _targets.R
│
├───R
│       functions.R

The problem I have is that I get warnings from {lintr} for global function definitions for functions (esp specials) loaded from packages.R (or from tar_option_set(packages=c(...))). I have a similar problem with lintr not recognising column names, instead thinking of them as missing variables. For example, a function in functions.R might look like:

Example of lintr warnings

If I was developing a package, the solution (as posted here and elsewhere) would be to include the function in the NAMESPACE. But I don't feel like every quick analysis requires a package. How else do I avoid these warnings? Is the only other option to exclude these linters in /.lintr?

2

There are 2 best solutions below

1
On BEST ANSWER
0
On

This post has 1k views, so I guess others are dealing with the same issue.

While @landau's suggestion to disable the linter does work, there is an alternative option if you want to retain the linter.

Just require the package containing the definition inside the function.

For the specific pipes %>% error message described by the OP, it seems to be not sufficient to require(tidyverse) collection. Instead you would need the specific package require(magrittr)

So the function will become

reduce_mtcars <- function(mtcars) {
  require(magrittr)
  mtcars %>% 
    dplyr::select(mpg, cyl, disp)  
}