[R] Defining/Checking constants in packages

Henrik Bengtsson hb at maths.lth.se
Thu Nov 21 01:55:57 CET 2002


If you add

# Allows conflicts. For more information, see library() and
# conflicts() in [R] base.
.conflicts.OK <- TRUE 

to your package you will not get a warning with masking objects when
your package is loaded. I normally include this line in a file named
zzz.R in which one normally also add functions as .First.lib() etc.

Otherwise there are no way to specify (final) constants in R. The new
support for name spacing, as Deepayan Sarkar mention in the other email,
will solve some of these problems. However, a common coding convention
between programmers could also prevent a lot of these problems. For
instance, using the widely used naming convention that constants have
only captial letters, e.g. PI, and (changing) variables not, would
minimize the risk for redefinition of its value. Of course, it could
still be that different programmers defines PI differently. For *my own
and my collegues* purposes, I am trying to define such a coding
convention for minimizing mishaps like the one you describe. It can be
found on http://www.maths.lth.se/help/R/RCC/, but note that is under
construction(!).

Best wishes

Henrik Bengtsson
Lund University


> -----Original Message-----
> From: owner-r-help at stat.math.ethz.ch 
> [mailto:owner-r-help at stat.math.ethz.ch] On Behalf Of Giovanni Petris
> Sent: den 21 november 2002 08:40
> To: r-help at stat.math.ethz.ch
> Subject: [R] Defining/Checking constants in packages
> 
> 
> 
> In a package I am writing I need to use the constant pi (3.14..., that
> is) which is already defined in R. However, how can I make 
> sure the user has not changed its value before loading the package? 
> If this happened, is a reference to `pi' in the package going 
> to use the user-defined value or the value defined in base? I 
> thought I could solve the issue by including the following 
> line in my package
>    pi <- get("pi", pos=grep("package:base",search()))
> but then when I load the package R warns me about masking issues. 
> What is the recommended way of dealing with this issue?
> 
> Thank you in advance,
> Giovanni
> 
> -- 
> 
>  __________________________________________________
> [                                                  ]
> [ Giovanni Petris                 GPetris at uark.edu ]
> [ Department of Mathematical Sciences              ]
> [ University of Arkansas - Fayetteville, AR 72701  ]
> [ Ph: (479) 575-6324, 575-8630 (fax)               ]
> [ http://definetti.uark.edu/~gpetris/              ]
> [__________________________________________________]
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-.-.-.-.-.-.-.-
> r-help mailing list -- Read 
> http://www.ci.tuwien.ac.at/~hornik/R/R-> FAQ.html
> Send "info", 
> "help", or "[un]subscribe"
> (in the 
> "body", not the subject !)  To: 
> r-help-request at stat.math.ethz.ch 
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _._._._._._._._._
> 
> 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list