[R] Ellipsis arguments for plot.formula
Gabor Grothendieck
ggrothendieck at gmail.com
Fri Jul 11 15:31:33 CEST 2008
A workaround is to use the title command instead:
my.plot(y ~ x, tdf)
title(main.str)
On Fri, Jul 11, 2008 at 8:43 AM, Amit Ganatra <amit_d_ganatra at yahoo.com> wrote:
> Thank you for taking the time.
> I get your point about presumptions.
> If you issue the call my.plot( x, y, main = main.str ), it works fine. The problem turns up only when you use a formula as an argument, hence the suspicion that the behavior was unintended.
>
>
> --- On Thu, 7/10/08, Bert Gunter <gunter.berton at gene.com> wrote:
>
>> From: Bert Gunter <gunter.berton at gene.com>
>> Subject: RE: [R] Ellipsis arguments for plot.formula
>> To: amit_d_ganatra at yahoo.com
>> Cc: r-help at r-project.org
>> Date: Thursday, July 10, 2008, 5:10 PM
>> I think it's rather presumptuous of you to ask whether a
>> fundamental
>> behavior that you don't understand in a mature software
>> product that has
>> been used by literally thousands of people for around 10
>> years (>20 for S)
>> is "a bug", don't you?
>>
>> So the answer is, no, this is how R's evaluation
>> mechanism works. It is,
>> admittedly, a complex issue, but let's see if we can
>> trace it through (help
>> and corrections from wiser folks would be appreciated if I
>> get anything
>> wrong...).
>>
>> When you issue your call
>>
>> > my.plot( y ~ x, tdf, main = main.str )
>>
>> at the command line, the "main=main.str" part is
>> the "..." argument (and
>> can be recovered within the function e.g. by list(...); see
>> ?browser). As
>> you understand, R now passes this down to the generic
>> plot() call (see
>> ?UseMethod for info on S3 generics). This in turn calls
>> plot.default() via
>>
>> > plot.default(x,y, cex.axis=0.5, main = main.str) .
>>
>> Now plot.default has to evaluate the name, main.str, in
>> order to execute.
>> Where is it to do this evaluation? It is **not** the top
>> level (.GlobalEnv),
>> which is why you threw the error message. So where does the
>> symbol,
>> "main.str," get evaluated? As I read the
>> ?UseMethod
>> documentation(corrections please here if I'm wrong), it
>> is in the context of
>> the plot call, which is within the my.plot function *** not
>> in the parent
>> frame as you said*** -- which is the global environment
>> (where it *would*
>> find it). It does not find main.str there, and so you get
>> an error message.
>> As you noted, if you explicitly put main.str into the
>> my.plot environment,
>> it would find it and the function works as you expect.
>>
>> I think the best documentation of all these technicalities
>> is V&R's S
>> PROGRAMMING, but others may also have their own favorites.
>> As I said, it is
>> quite tricky.
>>
>> As for a "workaround," this too, gets tricky: you
>> basically need to
>> construct the plot call with the evaluated variables. The
>> V&R reference
>> shows you one way to do this (see the "Computing on
>> the Language" chapter,
>> if memory serves). There may well be other slicker tricks
>> by now (which
>> others may provide, I hope).
>>
>> Hope this helps... and please be more circumspect in future
>> when asking such
>> questions. R is a quite remarkable effort by some really
>> smart people who
>> have done an enormous amount of work essentially for
>> nothing. While bugs
>> certainly can and do arise, I think our first and almost
>> always correct
>> impulse should be to assume that there's something that
>> we don't understand
>> and phrase our queries accordingly. We owe the developers
>> this courtesy.
>>
>> Cheers,
>>
>> Bert Gunter
>> Genentech, Inc.
>>
>>
>>
>>
>>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org
>> [mailto:r-help-bounces at r-project.org] On
>> Behalf Of Amit Ganatra
>> Sent: Thursday, July 10, 2008 11:51 AM
>> To: r-help at r-project.org
>> Subject: [R] Ellipsis arguments for plot.formula
>>
>> Hi:
>> I have a function as follows:
>> my.plot <- function( x, y = NULL, ... )
>> {
>> plot( x, y, cex.axis=0.5, ...)
>> }
>>
>> Set up the variables:
>> x <- 1:10; y <- x; tdf <- data.frame( x, y );
>> main.str <- "test"
>>
>> I will exercise the function in two ways:
>>
>> > my.plot( y ~ x, tdf, main = "test" )
>> This works fine
>>
>> > my.plot( y ~ x, tdf, main = main.str )
>> Error in eval(expr, envir, enclos) :
>> ..1 used in an incorrect context, no ... to look in
>>
>> Turns out that plot.formula is looking for the name
>> "main.str" in its parent
>> frame. If I define the name main.str in my.plot the
>> function works fine.
>>
>> Is this a bug?
>> I have a clunky workaround where I get the parent.frame in
>> my.plot and
>> create the variables in my.plot, but I don't like the
>> solution.
>>
>> Thanks
>>
>> ______________________________________________
>> 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.
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list