Skip to content

Writing R package and do.call

5 messages · Rob Foxall, Rolf Turner, Hadley Wickham +1 more

#
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!)

Cheers,
Rob.
_
platform       i386-pc-mingw32
arch           i386
os             mingw32
system         i386, mingw32
status
major          2
minor          8.1
year           2008
month          12
day            22
svn rev        47281
language       R
version.string R version 2.8.1 (2008-12-22)
#
On 3/03/2009, at 2:46 PM, Rob Foxall wrote:

            
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}}
#
On Mon, Mar 2, 2009 at 7:46 PM, Rob Foxall <rfoxall27 at gmail.com> wrote:
This isn't a warning - it's just a note!  You don't need to do
anything about it if you don't want to.

Hadley
#
Try this:

temp.fn.2 <- function(mean = 0, sd = 1, n = 101) {
   x <- seq(mean - sd, mean + sd, length = n)
   plot(x, dnorm(x), type = "l")
}

# test
temp.fn.2()
On Mon, Mar 2, 2009 at 8:46 PM, Rob Foxall <rfoxall27 at gmail.com> wrote:
#
Thanks Rolf,

I used your solution in my original, actual function and it worked
very fine and a much more sophisticated solution than what I
originally came up with! Thanks to the other people who replied as
well -- I know that the message said it was a "Note" rather than a
"Warning", but it sounds ominously like a warning, and so a little
part of me feels more secure now that I can avoid seeing it!

Cheers,
Rob.