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

Gabriel Becker gmbecker at ucdavis.edu
Mon Dec 12 21:57:01 CET 2016


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:

> 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:
> > I am wrting a package called bibliospec.
> > I have a dataset in data/AminoAcids.tsv and would like to be able to
> > access it with
> >
> > bibliospec::AminoAcids
> >
> > from within my package code.
> >
> > R CMD build gives me the error:
> > Error : 'AminoAcids' is not an exported object from
> 'namespace:bibliospec'
> >
> > I am able to access the data in package code with
> > data(AminoAcids)
> > AminoAcids
> >
> > but this will give me a NOTE with R CMD check
> >
> > Also, after loading the packagepackage I am able to access the data with
> > bibliospec::AminoAcids
> >
> > But I can't access it from the package bibliospec code.
> > So why can't I access bibliospec::AminoAcids from within package code?
> >
> > Help appreciated
> >
> > Witold
> > --
> > Witold Eryk Wolski
>
>
>
> --
> Witold Eryk Wolski
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



-- 
Gabriel Becker, PhD
Associate Scientist (Bioinformatics)
Genentech Research

	[[alternative HTML version deleted]]



More information about the R-devel mailing list