Skip to content

accessing data by packagename::dataname from within package code fails.

3 messages · Witold E Wolski, William Dunlap, Gabriel Becker

#
I have narrowed down the problem.
The error
Error : 'AminoAcids' is not an exported object from 'namespace:bibliospec'
Error : unable to load R code in package 'bibliospec'

occurs only if I try to access the data using bibliospec::AminoAcids

within the initialize method of an R reference class.
It does work, as far as I tested everywhere else. In other methods of
a reference class as well as in free functions.
It also does not work in the initialization list to the initialize function.
So I also can't do something like
initialize = function(aminoAcids=bibliospec::AminoAcids){


I guess this is an R FEATURE.

But then where and how is the best practice to initialize class
members with default values?

Thank you.
On 12 December 2016 at 15:45, Witold E Wolski <wewolski at gmail.com> wrote:

  
    
#
You can define the data in the R directory.  You can keep it all in a *.R
file
by wrapping the text of the *.csv file in quotes and using
read.table(text="quoted stuff"), as in:

theData <- read.csv(header=TRUE, text="
English,Digit
One,1
Two,2
Three,3")
N <- nrow(theData)

You need to make sure 'theData' is defined before using it so put
everything requiring
it in one file or use the Collate: directive in the DESCRIPTION file.



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Mon, Dec 12, 2016 at 12:34 PM, Witold E Wolski <wewolski at gmail.com>
wrote:

  
  
#
Witold,

Are you using the sys data approach to actually put your data into your
package's namespace? From ?data (Good practices section)

Use of ?data? within a function without an ?envir? argument has

     the almost always undesirable side-effect of putting an object in

     the user's workspace (and indeed, of replacing any object of that

     name already there).  It would almost always be better to put the

     object in the current evaluation environment by ?data(..., envir =

     environment())?.  However, two alternatives are usually

     preferable, both described in the ?Writing R Extensions? manual.


        ? For sets of data, set up a package to use lazy-loading of

          data.


        ? For objects which are system data, for example lookup tables

          used in calculations within the function, use a file

          ?R/sysdata.rda? in the package sources or create the objects

          by R code at package installation time.


     A sometimes important distinction is that the second approach

     places objects in the namespace but the first does not.  So if it

     is important that the function sees ?mytable? as an object from

     the package, it is system data and the second approach should be

     used.  In the unusual case that a package uses a lazy-loaded

     dataset as a default argument to a function, that needs to be

     specified by ?::?, e.g., ?survival::survexp.us?.


It does seem a bit strange that the :: works elsewhere but not in
initialize, but I don't have time to track that down atm.

Best,
~G

On Mon, Dec 12, 2016 at 12:34 PM, Witold E Wolski <wewolski at gmail.com>
wrote: