[R] edit/environment bug??

Duncan Murdoch murdoch at stats.uwo.ca
Fri May 19 03:29:59 CEST 2006


On 5/18/2006 8:06 PM, Scholz, Fritz wrote:
> I sent the query below out to 'cran at r-project.org'
> which was probably not appropriate.
> 
> I seem to have run into some weird behavior concerning the latest
> Windows version 2.3.0 of R.
> It does not happen in 2.2.1. 
> I have reduced it to a very simple exersise and I attach the workspace
> after things are messed up.
> That workspace is no longer readable by R 2.2.1.
>  <<.RData>> 
> ==============================================
> If this workspace gets filtered out of you e-mail, it contains two
> objects
> dat=rnorm(100)
> and the function
> test.fun = function (x = dat) 
> {
>     par(mfrow = c(2, 1))
>     for (i in 1:2) {
>         hist(x)
>     }
> }
> 
> To get the messed up version of fun, follow the steps below.
> ===============================================
> 
> When you spool test.fun without () in R 2.3.0 with the messed up version
> you get
> 
>> ls()
> [1] "dat"      "test.fun"
>> test.fun
> function (x = dat) 
> {
>     par(mfrow = c(2, 1))
>     for (i in 1:2) {
>         hist(x)
>     }
> }
> <environment: base>
> 
> and test.fun() no longer runs as it should.
> 
> The way I got to this version of test.fun was to edit it via
> fix(test.fun)
> I introduced an error (leaving off the ) in hist)
> 
> function (x = dat) 
> {
>     par(mfrow = c(2, 1))
>     for (i in 1:2) {
>         hist(x
>     }
> }
> 
> and after saving this messed up function
> I got Error in edit(name, file, title, editor) : 
>         an error occurred on line 6
>  use a command like
>  x <- edit()
>  to recover
> 
> I then did
> x=edit()
> fixed the error, saved again and did
> test.fun=x.
> 
> After that this weird <environment: base>
> showed up when before it was not there.
> 
> I have no experience with environments and don't know
> whether this is normal. It certainly does not happen this
> way when I do it in R 2.2.1.
> The same problem was verified in Unix R 2.3.
> 
> I hope someone can enlighten me. I tried to look at the various 
> FAQ but could not make a link.

This sounds like a bug that was reported in the R-devel list and fixed 
last week.  You can try out the fix in R-patched.

The issue was that when you use edit(), make an error, and then open and 
fix the file, the original environment of the function gets lost. 
edit() was defaulting to the base environment instead of the global 
environment as it usually should.

Duncan Murdoch




More information about the R-help mailing list