<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/reference-semantics-in-r/"></div></div><div><a data-url="http://www.r-bloggers.com/reference-semantics-in-r/" data-counturl="http://www.r-bloggers.com/reference-semantics-in-r/" data-count="horizontal" data-text="Reference semantics in R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ipub.com/reference-semantics/"> R – ipub</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Question</h2>
<p>I recently got a mail from Václav on reference semantics in <a target="_blank" href="http://ipub.com/data-tree/">data.tree</a>, reading as follows:</p>
<blockquote>
<div>Dear Christoph,</div>
<div></div>
<div>I am rather inexperienced when it comes to environments in R and henceforth I apologize if my question is basic; however, my colleagues are no better than me to answer my question.</div>
<div>I would have a question iro the following behavior of your data.tree package. Is it correct that if I create a function which uses some data.tree structure as a parameter, the input value would get changed too?</div>
<div>In the following case I would assume that acme’s values should not get changed.</div>
<div></div>
<div>Thank you, Vaclav</div>
</blockquote>
<div>The code he provided was similar to this:</div>
<div></div>
<div>
<pre>library(data.tree)
data(acme)

acme$val &lt;- 5
acme$val

DoAssign &lt;- function(tr) {
  a &lt;- tr
  a$val &lt;- 33
  return (a)
}

acme2 &lt;- DoAssign(acme)

acme$val
[1] 33</pre>

<h2>Answer</h2>
<p>My answer was as follows:</p>
</div>
<div>Well observed, that is indeed the behavior of data.tree. From the manual:</div>
<div></div>
<blockquote>
<h4>Node and Reference Semantics</h4>
<div>The entry point to the package is Node. Each tree is composed of a number of Nodes, referencing each other. One of most important things to note about data.tree is that it exhibits reference semantics. In a nutshell, this means that you can modify your tree along the way, without having to reassign it to a variable after each modification. By and large, this is a rather exceptional behavior in R, where value-semantics is king most of the time.</div>
</blockquote>
<div></div>
<h3>Reference Semantics Explained</h3>
<div>In a nutshell, reference semantics can be understood by the following analogy: If I give you a URL, I provide you with a <strong>reference</strong> to a web page. You, I and the owner of the web page can access that web page with that URL. And if the owner changes the content, then you will see these changes next time you connect to the URL.</div>
<div>Contrarily, if I print out the web page and give you that print out, then I provide you with a disconnected copy of the web page. You may modify that copy (e.g. by highlighting passages with a marker), but I will not see these changes, nor will you see changes made in the original page by the owner. This is <strong>value semantics</strong>.</div>
<div></div>
<h3>Why data.tree uses reference semantics</h3>
<div>The main reason why we chose to do it that way in data.tree is that we treat each Node as a unit. When modifying a Node, or when adding a field to a Node, we do not want to create a deep copy of the entire tree for performance reasons.</div>
<div></div>
<div>Another reason is that it greatly simplifies the API of the package. For example, we can do:</div>
<div></div>
<div>
<div>
<pre>library(data.tree)
data(acme)
#get a list of Nodes
traversal &lt;- Traverse(acme, filterFun = function(node) !is.null(node$cost))
#modify a field
Do(traversal, function(node) node$cost2 &lt;- node$cost * 1.2)
#the value is now modified in the original tree:
print(acme, &quot;cost&quot;, &quot;cost2&quot;)</pre>
</div>
</div>
<h3>Reference Semantics in R</h3>
<div>While very common with object oriented languages (e.g. C++, Java, C#), this paradigm is not very wide-spread in R. Though it’s gaining more and more acceptance. Check out, for instance, the <a target="_blank" href="https://cran.r-project.org/web/packages/data.table/vignettes/datatable-reference-semantics.html">:= operator in data.table</a>, or google <a target="_blank" href="http://adv-r.had.co.nz/R5.html">R reference classes</a>, or <a target="_blank" href="https://github.com/wch/R6">R6</a>.</div>
<div></div>
<div>The downside is, of course, that it might seem confusing at first.</div>
<div></div>
<div>Hope that helps!</div>
<div></div>
<p>The post <a target="_blank" rel="nofollow" href="http://ipub.com/reference-semantics/">Reference semantics in R</a> appeared first on <a target="_blank" rel="nofollow" href="http://ipub.com/">ipub</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/reference-semantics-in-r/"></div></div><div><a data-url="http://www.r-bloggers.com/reference-semantics-in-r/" data-counturl="http://www.r-bloggers.com/reference-semantics-in-r/" data-count="vertical" data-text="Reference semantics in R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://ipub.com/reference-semantics/"> R – ipub</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/McQH6meKpl4"></a></div>