[R] adding a function after package.skeleton()
Gavin Simpson
gavin.simpson at ucl.ac.uk
Thu Jan 3 15:47:42 CET 2008
On Sun, 2007-12-30 at 20:28 +0000, baptiste Auguié wrote:
> Hi,
>
> Thanks for this tip, I'm always amazed at the number of clever
> functions built-in in R –– just wish i could think of their name
> rather than reinventing the wheel.
>
> However, I'm still stupidly stuck with this basic question: how
> should a function access data in its own package?
Perhaps rethink the way you are implementing this. Why do you need to
load Constants each time one calls L2eV? Wouldn't it be better to assume
Constants exist or (better) handle its absence with something like:
if(!exists(Constants))
stop("Data object 'Constants' not found. Try 'data("Constants")'")
in the body of the function.
Or have an extra argument in L2eV that requires the user to pass in the
correct constant.
Or on loading your package have it arrange for some options to be set
such that you can retrieve them within the function. Other packages set
options this way.
Is Constants supposed to be fixed and unchanging? If yes, why hold these
in a data object? One option might be to have a function Constants() in
your package, such that it returns a list e.g.
Constants <- function() {
list(h = h, cel = cel, ee = ee)
}
and then redefine L2eV etc along these lines:
L2eV <- function(lambda)
{
Constants <- Constants()
Constants$h * Constants$cel / Constants$ee / lambda
}
If these are just example Constants that a user might wish to change,
then you could adapt this approach to the following
## this definition holds your basic constants that the user
## can change
Constants <- function(h = 3.14, cel = 1, ee = 10) {
list(h = h, cel = cel, ee = ee)
}
L2eV <- function(lambda, Constants = Constants())
{
Constants$h * Constants$cel / Constants$ee / lambda
}
A user wishing to change the constants could do
res <- L2eV(lambda = 4, Constants = list(h = 10, cel = 4. ee = 2.5))
As you can see, I'm not clear on what you actually want to do to be able
to provide concrete answer. What is "Constants" and how/why are you
using it? would have been useful information to provide alongside what
you perceive to be your problem.
Others may have more useful advice?
HTH
G
>
>
> On 30 Dec 2007, at 18:27, Gabor Grothendieck wrote:
>
> > On Dec 30, 2007 1:19 PM, baptiste Auguié <ba208 at exeter.ac.uk> wrote:
> >
> >> These two functions use the dataframe "Constants", part of this
> >> package:
> >>
> >>> `L2eV` <- function(lambda)
> >>> {
> >>> data("Constants")
> >>> Constants$h*Constants$cel/Constants$ee/lambda ->eV
> >>> eV
> >>> }
> >>
> >
> > This does not answer your question but note that you can do this:
> >
> > with(Constants, h * cel / ee / lambda)
>
>
>
> Initial post:
>
> >> From: ba208 at exeter.ac.uk
> >> Subject: adding a function after package.skeleton()
> >> Date: 30 December 2007 18:19:40 GMT
> >> To: r-help at r-project.org
> >>
> >> Dear R helpers,
> >>
> >> I've successfully created a package 'constants' using
> >> package.skeleton() with one dataframe and a few functions.
> >> However, now that I want to add some functions and data to the
> >> package, I run into a problem.
> >>
> >> I ran prompt(...) and moved + edited the resulting .Rd files as
> >> appropriate (I believe). The log file from RCMD check constants
> >> does indicate a few problems (full log below).
> >>
> >> As far as I understand, the real problem would be on the lines:
> >>
> >>
> >>> * checking R code for possible problems ... NOTE
> >>> L2eV: no visible binding for global variable 'Constants'
> >>> eV2L: no visible binding for global variable 'Constants'
> >>>
> >>
> >>
> >> These two functions use the dataframe "Constants", part of this
> >> package:
> >>
> >>
> >>> `L2eV` <- function(lambda)
> >>> {
> >>> data("Constants")
> >>> Constants$h*Constants$cel/Constants$ee/lambda ->eV
> >>> eV
> >>> }
> >>>
> >>
> >>
> >> and
> >>
> >>
> >>> `eV2L` <- function(eV)
> >>> {
> >>> data("Constants")
> >>> Constants$h*Constants$cel/Constants$ee/eV ->Lambda
> >>> Lambda
> >>> }
> >>>
> >>
> >>
> >> After searching the R archives about "no visible binding for
> >> global variable ", I added the quotes around "Constants" but it
> >> doesn't seem to help. What would be the correct way to use this
> >> data inside the package?
> >>
> >> Best regards,
> >>
> >> baptiste
> >>
> >> ----------------
> >> Log of R CMD check constants :
> >>
> >>
> >>
> >>>
> >>> baptiste-auguies-ibook-g4:~ baptiste$ R CMD check constants
> >>> * checking for working latex ... OK
> >>> * using log directory '/Users/baptiste/constants.Rcheck'
> >>> * using R version 2.6.1 (2007-11-26)
> >>> * checking for file 'constants/DESCRIPTION' ... OK
> >>> * checking extension type ... Package
> >>> * this is package 'constants' version '1.0'
> >>> * checking package dependencies ... OK
> >>> * checking if this is a source package ... OK
> >>> * checking whether package 'constants' can be installed ... OK
> >>> * checking package directory ... OK
> >>> * checking for portable file names ... OK
> >>> * checking for sufficient/correct file permissions ... OK
> >>> * checking DESCRIPTION meta-information ... OK
> >>> * checking top-level files ... OK
> >>> * checking index information ... OK
> >>> * checking package subdirectories ... OK
> >>> * checking R files for non-ASCII characters ... OK
> >>> * checking R files for syntax errors ... OK
> >>> * checking whether the package can be loaded ... OK
> >>> * checking whether the package can be loaded with stated
> >>> dependencies ... OK
> >>> * checking for unstated dependencies in R code ... OK
> >>> * checking S3 generic/method consistency ... OK
> >>> * checking replacement functions ... OK
> >>> * checking foreign function calls ... OK
> >>> * checking R code for possible problems ... NOTE
> >>> L2eV: no visible binding for global variable 'Constants'
> >>> eV2L: no visible binding for global variable 'Constants'
> >>> * checking Rd files ... WARNING
> >>> Rd files with non-standard keywords:
> >>> L2eV.Rd: kwd1 kwd2
> >>> constants-package.Rd: physical constants optical
> >>> delete.all.Rd: kwd1 kwd2
> >>> eV2L.Rd: kwd1 kwd2
> >>> epsilon2nk.Rd: kwd1 kwd2
> >>> fano.Rd: kwd1 kwd2
> >>> lorentz.Rd: kwd1 kwd2
> >>> nk2epsilon.Rd: kwd1 kwd2
> >>> Each '\keyword' entry should specify one of the standard keywords
> >>> (as
> >>> listed in file 'KEYWORDS' in the R documentation directory).
> >>>
> >>> See the chapter 'Writing R documentation files' in manual 'Writing R
> >>> Extensions'.
> >>> * checking Rd cross-references ... OK
> >>> * checking for missing documentation entries ... OK
> >>> * checking for code/documentation mismatches ... OK
> >>> * checking Rd \usage sections ... OK
> >>> * checking data for non-ASCII characters ... OK
> >>> * creating constants-Ex.R ... OK
> >>> * checking examples ... OK
> >>> * creating constants-manual.tex ... OK
> >>> * checking constants-manual.tex ... OK
> >>>
> >>> WARNING: There was 1 warning, see
> >>> /Users/baptiste/constants.Rcheck/00check.log
> >>> for details
> >>>
>
>
>
>
> _____________________________
>
> Baptiste Auguié
>
> Physics Department
> University of Exeter
> Stocker Road,
> Exeter, Devon,
> EX4 4QL, UK
>
> Phone: +44 1392 264187
>
> http://newton.ex.ac.uk/research/emag
> http://projects.ex.ac.uk/atto
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Dr. Gavin Simpson [t] +44 (0)20 7679 0522
ECRC, UCL Geography, [f] +44 (0)20 7679 0565
Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/
UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
More information about the R-help
mailing list