Re: [R] bug: Editing function formals deletes the environment

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Fri 13 Oct 2006 - 12:32:28 GMT

If you are just modifying an S3 method in a package you may not need to reinsert the method into the package since UseMethod first looks into the caller environment
for methods anyways and only second does it look for methods in the package. Thus:

   HTML.data.frame <- R2HTML:::HTML.data.frame    HTML.data.frame$Border <- 2
   HTML(BOD, file = file("clipboard", "w"), append = FALSE)

would be sufficient if you intend to call HTML.

There is one significant caveat. If you intend to call a function in R2HTML which in
turn calls HTML then the above would not be enough. You would also have to modify
the environment of the caller too.

Thus after running the above:

   HTML2clip(BOD)

would still get the old Border since we are calling HTML2clip which in turn calls
HTML (as opposed to calling HTML directly). In this case, we would need to create
a new HTML2clip with a reset environment too:

   HTML2clip <- R2HTML:::HTML2clip
   environment(HTML2clip) <- environment()    HTML2clip(BOD)

would get the Border=2 value.

On 10/13/06, Alex Brown <alex@transitive.com> wrote:
> First, here's the specific bug I have. Later I'll say why I care.
>
> > ls(zappo)
> Error in try(name) : object "zappo" not found
> # good.
> > f = function(zappo) { function(y) zappo + y }
> > g = f(1)
> > g(1)
> [1] 2
>
> > formals(g)
> $y
>
> > formals(g)$y
> > formals(g)$y = 2
> > g
> function (y = 2)
> zappo + y
> > g(1)
> Error in g(1) : object "zappo" not found
>
> # looks like formals strips the environment off stuff.
>
> anything I can do about this?
>
> -Alex
>
>
> Original question:
>
> I'm trying to change the behaviour of a package, to simplify the
> interface.
>
> I'd rather not change the package, although I could.
>
> There's a hidden function whose defaults I wish to change.
>
> I'm using R 2.3.1 for macosX. Upgrading is not an option.
>
> This is what I do:
>
> library(R2HTML)
>
> # get the function to modify
> x = getFromNamespace("HTML.data.frame", "R2HTML")
> # change the default for an argument
> formals(x)["Border"]=list(NULL)
> # put the function back
> assignInNamespace("HTML.data.frame", x, "R2HTML")
>
> #test the function:
>
> HTML(data.frame(1:2), file=stdout())
>
> Error: could not find function "HTMLReplaceNA"
>
> # what seems to be happening is that the formals function is
> stripping the namespace off the variable x. I can't tell why.
>
> ______________________________________________
> R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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. Received on Sat Oct 14 02:39:34 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 13 Oct 2006 - 17:30:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.