Note that there are some explanatory texts on larger screens.

plurals
  1. PODisable assignment via = in R
    text
    copied!<p>R allows for assignment via <code>&lt;-</code> and <code>=</code>.</p> <p>Whereas there a <a href="https://stackoverflow.com/q/1741820/289572">subtle differences</a> between both assignment operators, there seems to be a broad consensus that <code>&lt;-</code> is the better choice than <code>=</code>, as <code>=</code> is also used as operator mapping values to arguments and thus its use may lead to ambiguous statements. The following exemplifies this:</p> <pre><code>&gt; system.time(x &lt;- rnorm(10)) user system elapsed 0 0 0 &gt; system.time(x = rnorm(10)) Error in system.time(x = rnorm(10)) : unused argument(s) (x = rnorm(10)) </code></pre> <p>In fact, the <a href="http://google-styleguide.googlecode.com/svn/trunk/google-r-style.html#assignment" rel="nofollow noreferrer">Google style code</a> disallows using <code>=</code> for assignment (see comments to this <a href="https://stackoverflow.com/a/1742591/289572">answer</a> for a converse view).</p> <p>I also almost exclusively use <code>&lt;-</code> as assignment operator. However, the almost in the previous sentence is the reason for this question. When <code>=</code> acts as assignment operator in my code it is always accidental and if it leads to problems these are usually hard to spot.</p> <p><strong>I would like to know if there is a way to turn off assignment via <code>=</code> and let R throw an error any time <code>=</code> is used for assignment.</strong></p> <p>Optimally this behavior would only occur for code in the Global Environment, as there may well be code in attached namespaces that uses <code>=</code> for assignment and should not break.</p> <p>(This question was inspired by a discussion with <a href="http://www.jonathandnelson.com/" rel="nofollow noreferrer">Jonathan Nelson</a>)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload