Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tmPlot is in namespace, but its dependencies are not found

I have a package which uses the tmPlot function from treemap, but when I try to use the function, it throws an error that one of its dependencies isn't loaded:

Error in tmPlot(data, index = index, vSize = vSize) : 
  could not find function "brewer.pal"

The dependency is installed and in the namespace.

This question has a little bit of setup, being a package problem, but I've tried to make it as minimal as possible:

Ensure you have treemap (and all its dependencies) installed.

I've made a directory called 'anRpackage'. Inside it is a folder ('R') and a DESCRIPTION file with the following text:

Package: anRpackage
Title: What the package does (short line)
Version: 1.0
Author: Who wrote it
Maintainer: Who to complain to <[email protected]>
Description: More about what it does (maybe more than one line)
License: What license is it under?
Imports:
    treemap
Collate:
    'maketree.R'

Inside the R/ folder is a single R file called 'maketree.R'. Its contents are:

#' maketree
#' 
#' @importFrom treemap tmPlot
#' @export maketree

maketree <-
function(data, index, vSize){
  tmPlot(data, index=index, vSize=vSize)
}

Assuming you're in the directory above 'anRpackage', run the following script:

library(roxygen2)
roxygenise("anRpackage/")

library(devtools)

build("anRpackage")
install("anRpackage")

Restart R (preferably with --vanilla) and run the following:

library(anRpackage)

data(mtcars)
maketree(mtcars, "cyl", "mpg")

You should get the error I described right at the beginning. Why does this happen? RColorBrewer is listed as Depends for treemap, so it should be be automatically imported, should it not?

like image 290
sebastian-c Avatar asked Feb 11 '13 08:02

sebastian-c


2 Answers

The problem is really with treemap. treemap uses brewer.pal, and so should Imports: RColorBrewer and importFrom(RColorBrewer, brewer.pal).

As it stands now, everything is ok if the user says library(treemap), treemap and RColorBrewer are attached to the search() path, and when tmPlot is evaluated brewer.pal is found on the search path. Of course the package would break if the user were to say brewer.pal="yeast" or something, because the wrong symbol would be found; this is one of the reasons for a name space, to protect treemap's functions from what the user might do.

But what happens when you (correctly) Imports: treemap? treemap is loaded (into memory) but neither treemap nor its dependencies are attached (to the search path). So brewer.pal is not found.

If treemap were to Imports: RColorBrewer, then brewer.pal would be found both when treemap were attached to the search path by a call to library(treemap), and when only imported into your package.

Contact the maintainer of treemap and ask them to do a more careful job of constructing their name space.

like image 144
Martin Morgan Avatar answered Oct 03 '22 23:10

Martin Morgan


before calling the tmPlot(data, index = index, vSize = vSize), you need to load RColorBrewer:

require(RColorBrewer)
like image 26
Néstor Waldyd Avatar answered Oct 04 '22 00:10

Néstor Waldyd