Hi, There is an object, ".Last.value" to which the result of the most recent evaluation is assigned. This is similar to "ans" in Matlab. In Matlab "ans" can be very useful and time-saving, but typing the larger R version is somewhat clunky and takes away from the usefulness. Is it possible to reassign '.Last.value' to something simpler, like 'ans' ? I'm aware of this thread: http://tolstoy.newcastle.edu.au/R/help/00b/1598.html, where a function such as: ans <- function() { return(.Last.value) } is discussed, however it would be nice not to have to type the () every time. Is it possible to change something in R to essentially replace the location where ".Last.value" has the recent evaluation assigned to it with 'ans' instead? Or might it be possible to make an object 'ans' that updates its value from ".Last.value" each time it is called? Thanks, Jon -- Jon Zadra Department of Psychology University of Virginia P.O. Box 400400 Charlottesville VA 22904 (434) 982-4744 email: zadra at virginia.edu <http://www.google.com/calendar/embed?src=jzadra%40gmail.com>
Try this:> makeActiveBinding("ans", function() .Last.value, .GlobalEnv)NULL> yy <- 3 > ans[1] 3 On Mon, Jul 7, 2008 at 2:03 PM, Jon Zadra <zadra at virginia.edu> wrote:> Hi, > > There is an object, ".Last.value" to which the result of the most recent > evaluation is assigned. This is similar to "ans" in Matlab. In Matlab > "ans" can be very useful and time-saving, but typing the larger R version is > somewhat clunky and takes away from the usefulness. > > Is it possible to reassign '.Last.value' to something simpler, like 'ans' ? > > I'm aware of this thread: > http://tolstoy.newcastle.edu.au/R/help/00b/1598.html, where a function such > as: > > ans <- function() { > return(.Last.value) > } > > is discussed, however it would be nice not to have to type the () every > time. Is it possible to change something in R to essentially replace the > location where ".Last.value" has the recent evaluation assigned to it with > 'ans' instead? Or might it be possible to make an object 'ans' that updates > its value from ".Last.value" each time it is called? > > Thanks, > > Jon > -- > Jon Zadra > Department of Psychology > University of Virginia > P.O. Box 400400 > Charlottesville VA 22904 > (434) 982-4744 > email: zadra at virginia.edu > <http://www.google.com/calendar/embed?src=jzadra%40gmail.com> > > ______________________________________________ > 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. >
Jon Zadra wrote:> Hi, > > There is an object, ".Last.value" to which the result of the most > recent evaluation is assigned. This is similar to "ans" in Matlab. > In Matlab "ans" can be very useful and time-saving, but typing the > larger R version is somewhat clunky and takes away from the usefulness. > > Is it possible to reassign '.Last.value' to something simpler, like > 'ans' ? > > I'm aware of this thread: > http://tolstoy.newcastle.edu.au/R/help/00b/1598.html, where a function > such as: > > ans <- function() { > return(.Last.value) > } > > is discussed, however it would be nice not to have to type the () > every time. Is it possible to change something in R to essentially > replace the location where ".Last.value" has the recent evaluation > assigned to it with 'ans' instead? Or might it be possible to make an > object 'ans' that updates its value from ".Last.value" each time it is > called?Luke will probably want to murder anyone trying these silly tricks with nonstandard evaluation, but here's one way: > .<-structure(0,class="ans") > "!.ans"<-function(e1,e2).Last.value > rnorm(10) [1] -1.16906125 0.06507857 0.08435672 -0.95936948 -1.46486146 1.01636505 [7] -0.54111611 -0.21457016 0.35078126 0.67347097 > !. [1] -1.16906125 0.06507857 0.08435672 -0.95936948 -1.46486146 1.01636505 [7] -0.54111611 -0.21457016 0.35078126 0.67347097 However, I'd settle for .La<TAB>v<TAB> .... -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907