Default arguments are evaluated in the function frame, not in the calling environment (nor in the same place as explicit arguments).> Which to me reads that a with statement as above is equivalent to > > > attach(data) ; aov.SS1(y=Obs) ; detach(data) > > Or is that just wishful thinking??The latter. On Thu, 7 Oct 2004, RenE J.V. Bertin wrote:> Hello, > > I'm having a little argument with the 'with' function. I have > > > aov.SS1 <- function( y, indep=speed, fSnr=Subject, ... ) > { > indep <- factor(speed) > fSnr <- factor(fSnr) > ## .... > } > > and a dataframe containing speed, Subject, and a bunch of other columns. If I now do > > > with( data, aov.SS1( y=Obs ) ) > > I get a message > > Error in factor(indep) : Object "speed" not found > > It seems that automatic argument initialisation doesn't work, as the only effectively valid call seems to be > > > with( data, aov.SS1( y=Obs, indep=speed, fSnr=Subject ) ) > > whereas > > > with( data, speed ) > > prints what one would expect (i.e. data$speed). > > How come? From ?with, one learns that > "... assignments within 'expr' take place in the constructed > environment and not in the user's workspace." > > Which to me reads that a with statement as above is equivalent to > > > attach(data) ; aov.SS1(y=Obs) ; detach(data) > > Or is that just wishful thinking?? > > RenE > > ______________________________________________ > R-help at 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 > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
I think what's happening is that the function aov.SS1 will look up variables in the environment in which it was *defined*, which in your case (I'm guessing) is the global environment/workspace. Therefore, if `speed' and `Subject' are not defined in the global environment, they will not be found. -roger RenE J.V. Bertin wrote:> Hello, > > I'm having a little argument with the 'with' function. I have > > > aov.SS1 <- function( y, indep=speed, fSnr=Subject, ... ) > { > indep <- factor(speed) > fSnr <- factor(fSnr) > ## .... > } > > and a dataframe containing speed, Subject, and a bunch of other columns. If I now do > > >>with( data, aov.SS1( y=Obs ) ) > > > I get a message > > Error in factor(indep) : Object "speed" not found > > It seems that automatic argument initialisation doesn't work, as the only effectively valid call seems to be > > >>with( data, aov.SS1( y=Obs, indep=speed, fSnr=Subject ) ) > > > whereas > > >>with( data, speed ) > > > prints what one would expect (i.e. data$speed). > > How come? From ?with, one learns that > "... assignments within 'expr' take place in the constructed > environment and not in the user's workspace." > > Which to me reads that a with statement as above is equivalent to > > >>attach(data) ; aov.SS1(y=Obs) ; detach(data) > > > Or is that just wishful thinking?? > > RenE > > ______________________________________________ > R-help at 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 >
r.ghezzo@staff.mcgill.ca
2004-Oct-07 18:19 UTC
[R] Re: problems installing package in R 2.0.0
Hello, I just installed R 2.0.0 in a Win XP machine. As old programs do not wor I tried to re-install them by:>C:\R\RW2000\bin>Rcmd INSTALL c:\r\r_src\src\autologi > >----------Making package autologi ------------------ > adding build stamp to DESCRIPTION > installing R files > installing data files > installing man source files > installing indices > not zipping data > installing help > >>>Building/Updating help pages for package 'autologi' > Formats: text html latex example chm > autologi text html latex example >wc: C:/R/rw2000/library/autologi/R/autologi: No such file or directory > adding MD5 sums > >* DONE <autologit>then in R>library() >Packages in library 'C:/R/rw2000/library': > >autologi ** No title available (pre-2.0.0 install?) ** >base The R Base Package > ...my directory for autologi has the following structure: c:\r\r_src\src\autologi\DESCRIPTION TITLE \R\autologi.r \man\autologi.RD \data\ex.dat I could not find anything relevant in the last version of "Writing R Extensions" that came with R 2.0.0. Another question. I did a full "install packages from CRAN" but then comparing the list of packages downloaded and installed with those in CRAN/windows/contrib/2.0/ i found packages like moc, multidim, multiv, netCDF, serialize, yags, xgobi. Can these packages be downloaded and installed or there is something broken in them? Thanks for any help and thanks to the R-Team. Heberto Ghezzo - McGill University
T/F are not defined in the global environment, rather they are in the `base' package. That's why you can override them by redefining T/F in the global environment (or somewhere farther up the search list than `base'). attach() is different because it places variables on the search list *after* the global environment. -roger RenE J.V. Bertin wrote:> On Thu, 07 Oct 2004 13:17:02 -0400, "Roger D. Peng" > <rpeng at jhsph.edu> wrote regarding "Re: [R] 'with' usage question" > > 8-) I think what's happening is that the function aov.SS1 will look > up 8-) variables in the environment in which it was *defined*, > which in your 8-) case (I'm guessing) is the global > environment/workspace. Therefore, 8-) if `speed' and `Subject' are > not defined in the global environment, 8-) they will not be found. > > > If you take that to the letter, doesn't it mean that attach() and > detach() couldn't have the effect they have? Which is, in fact and > if memory serves me well, much like the Pascal 'with' operator. A > (possible) counter argument: aov.SS1() used T and F as the usual > abbreviations for TRUE and FALSE: these are defined in the global > environment. Now my dataframes also contain a variable T: it took > me quite a while to realise that this variable (which I in fact > never use directly) would override the T constant/operator and > cause hard-to-trace error messages. > > I'm trying to find some analogy. with() extends the current > environment with the variables in the given dataframe, but does not > create a C-like scope. Rather, it functions more or less like a > Bourne shell script: the new variables are not 'exported' to be > available to code evoked from within that new environment, unless > they were specifically passed as arguments. Which still doesn't > explain why ls() sees everything when called within a with() > statement... > > Anyway, I'd better get back to work before my gray matter > shortcircuits :) > > ______________________________________________ > R-help at 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 >
On Thu, 7 Oct 2004, RenE J.V. Bertin wrote:> On Thu, 07 Oct 2004 13:17:02 -0400, "Roger D. Peng" <rpeng at jhsph.edu> wrote regarding "Re: [R] > 'with' usage question" > > 8-) I think what's happening is that the function aov.SS1 will look up > 8-) variables in the environment in which it was *defined*, which in your > 8-) case (I'm guessing) is the global environment/workspace. Therefore, > 8-) if `speed' and `Subject' are not defined in the global environment, > 8-) they will not be found. > > > If you take that to the letter, doesn't it mean that attach() and > detach() couldn't have the effect they have? Which is, in fact and if > memory serves me well, much like the Pascal 'with' operator.No. attach() attaches the data frame to the search path after the global environment. So variables in an attached data frame are always available (but may be overridden by variables in the global environment). attach() is superficially like with(), but not exactly the same. If you define with_attach <-function(data, expr){ attach(data) on.exit(detach(data)) eval(substitute(expr), parent.frame()) } it would behave more as you expected in this case. Inside a function this is not as good as with() because variables in the data frame can be overriden by local variables.> A > (possible) counter argument: aov.SS1() used T and F as the usual > abbreviations for TRUE and FALSE: these are defined in the global > environment. Now my dataframes also contain a variable T: it took me > quite a while to realise that this variable (which I in fact never use > directly) would override the T constant/operator and cause hard-to-trace > error messages.and that's why R CMD check tells you not to use T/F for TRUE/FALSE> I'm trying to find some analogy. with() extends the current environment > with the variables in the given dataframe, but does not create a C-like > scope. Rather, it functions more or less like a Bourne shell script: the > new variables are not 'exported' to be available to code evoked from > within that new environment, unless they were specifically passed as > arguments.It does create a new scope, as in C. The variables are not available inside functions called from that scope, because variables are never exported to functions called from a given scope. The same is true in C: variables will not be exported to functions *called from* a given block unless they are explicitly passed as arguments. The reason many people expect variables to be available in functions called from a given scope ("dynamic scoping") is that variables in the global workspace are available in functions called from the global workspace. But this is just a coincidence: the variables are available because the function is *defined* in the global workspace (or in an environment that includes the global workspace). Where it is called from is irrelevant. It is also initially confusing that having a default argument value is different from giving exactly the same expression as the actual argument value. This is also for a good reason: it is important for default arguments to be able to depend on other argument values. This means they have to be evaluated in an environment where the other formal arguments are available -- inside the function So in your example with( data, aov.SS1( y=Obs, indep=speed, fSnr=Subject ) ) works, because actual arguments are evaluated in the calling environment, which is inside with(), where speed and Subject are available. with( data, aov.SS1( y=Obs)) doesn't work because indep=speed and fSnr=Subject are evaluated inside aov.SS1, where speed and Subject aren't available.> Which still doesn't explain why ls() sees everything when > called within a with() statement... >Yes, it does. If ls() is used inside with() it sees the scope defined by with(), but if it is called inside a function called from with() it sees the environment inside that function: eg> with(trees, ls())[1] "Girth" "Height" "Volume"> f<-function() ls() > with(trees, f())character(0) -thomas
On Thu, 7 Oct 2004 r.ghezzo at staff.mcgill.ca wrote:> Hello, I just installed R 2.0.0 in a Win XP machine. As old programs do > not wor I tried to re-install them by:> Another question. I did a full "install packages from CRAN" but then > comparing the list of packages downloaded and installed with those in > CRAN/windows/contrib/2.0/ i found packages like moc, multidim, multiv, > netCDF, serialize, yags, xgobi. Can these packages be downloaded and > installed or thereserialize is incorporated into base R. netCDF, yags, xgobi are available from my site, as the ReadMe on CRAN says. moc is broken under 2.0.0, and multidim and multiv I believe are too. The ReadMe tells you such things -- please consult it. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595