I was cursing Matlab again today (what else is new) because the default action for every Matlab command is to spew the result to the console, and one must remember to put that darn ";" at the end of every line. So I just wondered: was there ever a discussion as to providing some modified version of the "<-" and "->" operators in R to do the reverse? That is, since R does not print the values of a command to the console, what if there were an operator such that newobject <p- somefunction() would do the same as print(newobject <- somefunction()) Any thoughts? Carl
Hi Carl, Well, we already have (newobject <- somefunction()), so <p- would only save a single keystroke at best, and none if you use an editor that auto-closes parentheses... Best, Ista On Wed, Mar 30, 2011 at 7:00 PM, Carl Witthoft <carl at witthoft.com> wrote:> I was cursing Matlab again today (what else is new) because the default > action for every Matlab command is to spew the result to the console, and > one must remember to put that darn ";" ?at the end of every line. > > So I just wondered: ?was there ever a discussion as to providing some > modified version of the "<-" and "->" operators in R to do the reverse? > ?That is, since R does not print the values of a command to the console, > ?what if there were ?an operator such that > > > ?newobject <p- somefunction() > > would do the same as > > print(newobject <- somefunction()) > > > Any thoughts? > Carl > > ______________________________________________ > 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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
Dear Carl, I think that the following does what you want:> `%<-%` <- function(e1, e2){+ e1 <- deparse(substitute(e1)) + env <- parent.frame() + assign(e1, e2, envir=env) + e2 + }> x %<-% 10[1] 10> x[1] 10 But, as has been pointed out, it's probably easier just to parenthesize the usual assignment command. Regards, John -------------------------------- John Fox Senator William McMaster Professor of Social Statistics Department of Sociology McMaster University Hamilton, Ontario, Canada http://socserv.mcmaster.ca/jfox> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > On Behalf Of Carl Witthoft > Sent: March-30-11 7:00 PM > To: r-help at r-project.org > Subject: [R] how about a "<p-" operator? > > I was cursing Matlab again today (what else is new) because the default > action for every Matlab command is to spew the result to the console, and > one must remember to put that darn ";" at the end of every line. > > So I just wondered: was there ever a discussion as to providing some > modified version of the "<-" and "->" operators in R to do the reverse? > That is, since R does not print the values of a command to the console, > what if there were an operator such that > > > newobject <p- somefunction() > > would do the same as > > print(newobject <- somefunction()) > > > Any thoughts? > Carl > > ______________________________________________ > 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.
On 11-03-30 7:00 PM, Carl Witthoft wrote:> I was cursing Matlab again today (what else is new) because the default > action for every Matlab command is to spew the result to the console, > and one must remember to put that darn ";" at the end of every line. > > So I just wondered: was there ever a discussion as to providing some > modified version of the "<-" and "->" operators in R to do the reverse? > That is, since R does not print the values of a command to the > console, what if there were an operator such that > > > newobject<p- somefunction() > > would do the same as > > print(newobject<- somefunction()) > > > Any thoughts?Others have given alternatives. Just some comments on this particular proposal: We already have a problem that x<-3 is slightly ambiguous: does it mean x <- 3 # yes or x < -3 # no I think the compound operator <p- would just repeat this problem. What does x<p-3 mean? If the amount of typing in print(x <- 3) is a problem, that's a user interface issue, so it should be addressed at the user interface level, not by changing the language. A front-end could make it easy to transform x <- 3 into the longer expression (or an equivalent), or just make it easy to examine the .Last.value variable. Duncan Murdoch