<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/what-no-parentheses/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://matloff.wordpress.com/2018/08/24/what-no-parentheses/"> Mad (Data) Scientist</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I’m about to show you an R trick. Various readers may find it cool, useful and interesting, or stupid, useless and an evil deed undermining the sanctity of R’s functional programming nature (“All bow”). But I hope many of you will find the material here rather intriguing if not useful.</p>
<p>All this involves a trick one can employ while working in R’s top level from interactive mode, the familiar > prompt.</p>
<p>I’ll use as an toy example here. Let’s say I have some counting variable <strong>x</strong> that I need to increment occasionally as I work. Of course, the straightforward way to do this is</p>
<pre>x &lt;- x + 1</pre>
<p>But to save typing and reduce distraction from my main thought processes, it would be nice if I were able to simply type</p>
<pre>ix</pre>
<p>Of course, the straightforward way to do this would be to define a function <strong>ix()</strong> (“increment x”),</p>
<pre>ix &lt;- function() x &lt;- x + 1</pre>
<p>and then call it each time by typing</p>
<pre>ix()</pre>
<p>But again, I want to save typing, and don’t want to type the parentheses. How can I arrange this?</p>
<p>My approach here will be to exploit the fact that in R’s interactive mode, typing an expression will print the value of that expression. If I type, say</p>
<pre>y</pre>
<p>R will first determine the class of <strong>y</strong>, and invoke the print method for that class. If I write that method myself, I can put any R code in there that I wish, such as code to increment <strong>x</strong> above! So here goes:</p>
<pre>
> w &lt;- list(y=3)
> class(w) &lt;- 'ix'
> print.ix &lt;- function(ixObject) x &lt;- x + 1
> x &lt;- 88
> w
> x
[1] 89
> w
> x
[1] 90
</pre>
<p>I set up an S3 class <strong>‘ix’</strong>, including a print method <strong>print.ix()</strong>. I created <strong>w</strong>, an instance of that class, and as you can see, each time I typed ‘w’, <strong>x</strong> did get incremented by 1.</p>
<p>What just happened? When I type ‘w’, the R interpreter will know that I want to print that variable. The interpreter finds that <strong>w</strong> is of class ‘ix’, so it calls the print method for that class, <strong>print.ix()</strong>, which actually doesn’t do any printing; it merely increments <strong>x</strong>, as desired.</p>
<p>So I don’t even need to type out even the 4 characters ‘ix()’, or even the 2 characters ‘ix’; just typing the single character ‘w’ suffices. A small thing, maybe, but very useful to me when, in the frenzy of code development and especially debugging, I am able to keep distractions from my train of thought to a minimum.</p>
<p>By the way, we have been doing further development on our <a rel="nofollow" href="https://arxiv.org/pdf/1806.06850" target="_blank"><strong>polyreg</strong> package</a>, with some interesting new features. More news on that soon.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://matloff.wordpress.com/2018/08/24/what-no-parentheses/"> Mad (Data) Scientist</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/h9oJeFkJdw4" height="1"></a></div>