No Visible Global Function Definition for 'Median'

no visible global function definition for ‘median’

As of Monday June 29, 2015, all non-base functions must be explicitly exported in NAMESPACE in order to pass R CMD check --as-cran. The change comes because code is now checked with only the base package attached, so functions from default packages (such as stats) must be listed explicitly.

To import these packages, consider doing the following:

  • In DESCRIPTION, you probably want to list them in Imports. There is very little reason to ever list a package in Depends.
  • In NAMESPACE, you can choose between import(stats) or importFrom(stats, ...), where ... is one or more comma-separated function names. (If you use roxygen2::roxygenize() or devtools::document() to generate documentation and NAMESPACE, the analogous markup would be #' @import stats and #' @importFrom stats ....)

If you want to work interactively with R in a mode that mimics this, you'll want to start R with only the base package attached. There are several ways to do this, but probably the easiest is to set an environment variable at your shell: R_DEFAULT_PACKAGES=NULL or in the .Renviron file and then start R using R --vanilla. In Terminal or bash this would be:

$ export R_DEFAULT_PACKAGES=NULL
$ R --quiet --vanilla
> search()
[1] ".GlobalEnv" "Autoloads" "package:base"

In Windows command prompt it would be:

C:\>SET R_DEFAULT_PACKAGES=NULL
C:\>R --quiet --vanilla
> search()
[1] ".GlobalEnv" "Autoloads" "package:base"

no visible global function definition for ':='

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.

checking R code for possible problems ... NOTE

You can specify the packages to import in each function definition using the @import or @importFrom keywords in the Roxygen description:

#' Title
#'
#' @return
#' @importFrom stats acf dist loess tsp tsp<-
#' @export
#'
#' @examples
foo <- function() {}

NAMESPACE will be automatically modified after running devtools::document(), see R Packages : Imports.

You have to modify the DESCRIPTION file manually.



Related Topics



Leave a reply



Submit