[R] Writing R package and do.call
Rolf Turner
r.turner at auckland.ac.nz
Tue Mar 3 03:39:01 CET 2009
On 3/03/2009, at 2:46 PM, Rob Foxall wrote:
> Dear R-Help,
>
> I have written a function, which in simplified format can be
> represented as:
>
> temp.fn <- function(my.mean,my.sd){
> Parameters <- list(mean = my.mean, sd = my.sd)
> curve(do.call(dnorm,c(list(x), Parameters)), from = my.mean-my.sd,
> to = my.mean+my.sd)
> }
>
> This works as I want it do. Wishing to immortalise this function into
> my very own package however, results in the following warning:
> (Running R CMD check myPackage)
>
> * checking R code for possible problems ... NOTE
> temp.fn: no visible binding for global variable 'x'
>
> Although it doesn't seem to matter, I would feel more comfortable if
> there was an alternative way which avoided this warning.
> ( I can avoid the warning, by pointlessly defining x, e.g. "x <- 1",
> but this seems pretty ugly!)
Rob: This is almost as ugly --- or even more ugly --- but it's
sexier in that
it's more sophisticated. (Nudge, nudge, wink, wink!) Anyhow, I
think you can
work around the warning as follows: Put the entire call to curve()
inside a text
string, and then call ``eval() on that text string:
temp.fn <- function(my.mean,my.sd){
Parameters <- list(mean = my.mean, sd = my.sd)
xxx <-"curve(do.call(dnorm,c(list(x), Parameters)), from =
my.mean-my.sd,to = my.mean+my.sd)"
eval(parse(text=xxx))
}
This seems to run, and when I put temp.fn.R inside an arbitrary
package and built that package,
it built without issuing a warning.
HTH.
cheers,
Rolf
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
More information about the R-help
mailing list