46

Since the latest R update I get the Note

summary.xmlImport: no visible global function definition for ‘median’

in the CRAN check. Further notes refer to read.table, write.table and other standard functions in R.

When I have a look on my file summary.xmlImport, the file looks like this:

summary.xmlImport <- function(object, ...){

   rowCount <- sapply(object,nrow)
   cat("Summary of xmlImport object\n")
   cat("---------------------------\n")
   cat("Sequences    :",length(object),"\n")
   cat("Min hits     :",min(rowCount),"\n")
   cat("Average hits :",mean(rowCount),"\n")
   cat("Median hits  :",median(rowCount),"\n")
   cat("Max hits     :",max(rowCount),"\n")
   invisible(object)

} 

I cannot understand, why I should add now the median function to the NAMESPACE, but why not the min, mean, etc. The note is only about the medianfunction.

Anybody an idea what the reason for the Note is and how to fix it? I noted that there are tons of R packages that have currently the same Note.

I can understand this warning in the context of a non-declared variable, but I would assume that median(), read.table() and such functions are globally visible in R, especially as mean()seems to be!?

EDIT: I only receive the Note on CRAN, but not on my local computer what makes the search for the solution a bit nasty... The session info of my computer:

> sessionInfo()
R version 3.2.1 (2015-06-18)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.2 LTS
Daniel Fischer
  • 3,280
  • 1
  • 18
  • 29
  • 6
    Not sure it is related, but functions `length`, `mean`, `min` and `max` are from `base` and function `median` from `stats`. –  Jun 30 '15 at 07:54
  • 1
    Thanks for the hint, I assumed `median` would be also in `base`, I'll try to import the `stats` package to the NAMESPACE, maybe since in R.3.2.1 only the `base` functions are then globally visible... – Daniel Fischer Jun 30 '15 at 07:58
  • According to Thomas's answer, it is the case. –  Jun 30 '15 at 08:27
  • I started a discussion on the package-devel-mailinglist for the same reason. Another suggestion was to add namespaces selectively. – Daniel Jun 30 '15 at 08:39

1 Answers1

68

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"
Thomas
  • 43,637
  • 12
  • 109
  • 140
  • 2
    Thanks, it seems it would be worthwhile to read R-Devel in the future. – Daniel Fischer Jun 30 '15 at 09:27
  • @DanielFischer Yes, CRAN runs checks on R-devel (i.e., the daily release of R), so before you try to submit, you need to install that R version and run your package through its checks. They can change daily and without notice. – Thomas Jun 30 '15 at 09:28
  • So what would the current recommended syntax be for these R core packages (eg stats)? How should they be included in the Description and Namespace - could you perhaps give the correct syntax? Under Depends or Imports? – Tom Wenseleers Jul 17 '15 at 13:32
  • @TomWenseleers Treat them like any other package. See edit. – Thomas Jul 17 '15 at 16:21
  • 3
    So if I am using roxygen, should I include stats and so in in Imports then, and in any functions that use functions from stats put an @import stats then? – Tom Wenseleers Jul 17 '15 at 16:46
  • Related question is how I can set up my RStudio to only attach the base package by default, so that I can simulate the tests they are now done at CRAN? As I am having trouble with my package passing R CMD check --as-cran over here OK, but not at CRAN... – Tom Wenseleers Jul 17 '15 at 16:48
  • 1
    @TomWenseleers I have no idea about roxygen as I don't use that. Regarding the checks, just make sure you're running everything on the latest R-devel (i.e., the daily build), not the latest R release. That's probably the reason you're getting different results. – Thomas Jul 17 '15 at 17:34
  • Thanks! I used the latest R devel windows build, https://cran.r-project.org/bin/windows/base/rdevel.html, maybe I should use the latest Linux build, don' know? The @import in Roxygen then causes import(stats) to be added to NAMESPACE, is that OK? – Tom Wenseleers Jul 17 '15 at 20:45
  • @TomWenseleers Shouldn't matter whether you're doing Windows or Linux (unless you have compiled C/C++/Fortran/etc. code in your package, or a handful of other relatively rare cases). Seems like the roxygen approach should be fine, then. – Thomas Jul 17 '15 at 21:17
  • Yes I was a little stumped by the fact that all checks were OK on my side, but not at CRAN. It must be some difference in their setup. The dependencies with the core R packages I managed to solve, but still there was some example code that wouldn't run on their side... I think it had to do something with a difference in their JAVA version perhaps... – Tom Wenseleers Jul 17 '15 at 22:34
  • Some general information related to this can be found at https://kbroman.org/pkg_primer/pages/depends.html including using roxygen,@import and @importFrom – N Brouwer Jun 02 '19 at 01:35