Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

r - no visible global function definition for ‘median’

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
")
   cat("---------------------------
")
   cat("Sequences    :",length(object),"
")
   cat("Min hits     :",min(rowCount),"
")
   cat("Average hits :",mean(rowCount),"
")
   cat("Median hits  :",median(rowCount),"
")
   cat("Max hits     :",max(rowCount),"
")
   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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...