<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Justin Talbot &#187; R</title>
	<atom:link href="http://www.justintalbot.com/tag/r/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.justintalbot.com</link>
	<description></description>
	<lastBuildDate>Wed, 07 Sep 2011 05:26:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Non-functional elements in R</title>
		<link>http://www.justintalbot.com/2009/01/non-functional-elements-in-r/</link>
		<comments>http://www.justintalbot.com/2009/01/non-functional-elements-in-r/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 23:34:07 +0000</pubDate>
		<dc:creator>Justin Talbot</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://justintalbot.org/?p=37</guid>
		<description><![CDATA[This list is from John&#8217;s lecture: Operators and functions with side effects: (&#60;&#60;-, assign(), options(foo=)) Nonstandard R objects: environments, connections. Random number generation Special mechanisms-&#8221;closures&#8221; According to the R language definition, environments are mutable objects, so changes are visible outside of the function. R closures can have side effects due to the fact that when [...]]]></description>
			<content:encoded><![CDATA[<p>This list is from John&#8217;s lecture:</p>
<ol>
<li>Operators and functions with side effects: (&lt;&lt;-, assign(), options(foo=))</li>
<li>Nonstandard R objects: environments, connections.</li>
<li>Random number generation</li>
<li>Special mechanisms-&#8221;closures&#8221;</li>
</ol>
<p>According to the R language definition, environments are mutable objects, so changes are visible outside of the function. R closures can have side effects due to the fact that when an R function returns a list of closures, these closures share the same environment. Since the environment is mutable, using &lt;&lt;- in the any of the closures will affect the other closures as well.</p>
<p>Example (inspired by a more involved example in John&#8217;s <a href="http://www.amazon.com/Software-Data-Analysis-Programming-Statistics/dp/0387759352">Software for Data Analysis</a>):<br />
<code><br />
&gt; Counter &lt;- function(start) {<br />
+ 	t &lt;- start<br />
+ 	list(<br />
+ 		inc = function() {t &lt;&lt;- t+1; t},<br />
+ 		dec = function() {t &lt;&lt;- t-1; t} )<br />
+ }<br />
&gt;<br />
&gt; counter &lt;- Counter(5)<br />
&gt; counter[["dec"]]()<br />
[1] 4<br />
&gt; counter[["dec"]]()<br />
[1] 3<br />
&gt; counter[["inc"]]()<br />
[1] 4<br />
</code></p>
<p>The Counter function returns a list of two functions &#8220;inc&#8221; and &#8220;dec&#8221;. Both functions are associated with the same environment. Thus successive calls to &#8220;inc&#8221; and &#8220;dec&#8221; operate on the same t variable. This use of closures has become largely out of date with the addition of S4 objects to the language.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.justintalbot.com/2009/01/non-functional-elements-in-r/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

