<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/the-r6-class-system/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.revolutionanalytics.com/2017/07/the-r6-class-system.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p>R is an object-oriented language with several object-orientation systems. There's the original (and still widely-used) <a rel="nofollow" href="https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Object_002doriented-programming" target="_blank">S3 class system</a> based on the &quot;class&quot; attribute. There's the somewhat stricter, signature-based <a rel="nofollow" href="http://adv-r.had.co.nz/S4.html" target="_blank">S4 class system</a>. There are <a rel="nofollow" href="https://www.rdocumentation.org/packages/methods/versions/3.4.1/topics/ReferenceClasses?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">reference classes</a> (also called R5), which provide R objects with multiple references without duplicating data in memory. And now there's the <a rel="nofollow" href="https://github.com/r-lib/R6#r6-classes" target="_blank">R6 class system</a>, implemented as an R package by Winston Chang,</p>
<p>So why yet another class system for R? As Winston explains in his <a rel="nofollow" href="https://channel9.msdn.com/Events/useR-international-R-User-conferences/useR-International-R-User-2017-Conference/The-R6-Class-System" target="_blank">useR!2017 talk</a> embedded below, the S3 and S4 class systems are <strong>functional</strong> object-oriented systems, where class methods are separate from objects, and objects are not mutable (that is, they can't be modified directly by methods; you have to assign modified values to the object directly). The R6 system, by contrast, is an <strong>encapsulated</strong> object oriented system akin to those in Java or C++, where objects contain methods in addition to data, and those methods can modify objects directly. </p>
<p><iframe width="450" allowfullscreen="" src="https://channel9.msdn.com/Events/useR-international-R-User-conferences/useR-International-R-User-2017-Conference/The-R6-Class-System/player" height="288"></iframe></p>
<p>The big advantage of R6 is that it makes it much easier to implement some common data structures in a user-friendly manner. For example, to implement a stack &quot;pop&quot; operation in S3 or S4 you have to do something like this:</p>
<pre>x &lt;- topval(mystack)
mystack &lt;- remove_top(mystack)
</pre>
<p>In R6, the implementation is much simpler to use:</p>
<pre>x &lt;- mystack$pop()
</pre>
<p>That single statement both returns the top value (as <code>x</code>) from the stack <code>mystack</code>, but also modifies the stack in the process (minus the popped element).</p>
<p>Despite being first released over 3 years ago, the R6 isn't widely known. It is widely <em>used</em>, however: it's used to manage session state in <a rel="nofollow" href="https://shiny.rstudio.com/" target="_blank">Shiny</a> and <a rel="nofollow" href="https://docs.microsoft.com/en-us/r-server/r-reference/mrsdeploy/mrsdeploy-package" target="_blank">mrsdeploy</a>, to manage database connections in <a rel="nofollow" href="http://dplyr.tidyverse.org/" target="_blank">dplyr</a>, and to represent system processes in the <a rel="nofollow" href="https://mran.microsoft.com/package/processx/" target="_blank">processx package</a>. In fact, the R6 package is the <em>most</em> downloaded CRAN package, according according to <a rel="nofollow" href="https://www.rdocumentation.org/trends?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">Rdocumentation Trends</a>.</p>
<p>The R6 package is <a rel="nofollow" href="http://mran.microsoft.com/package/R6/" target="_blank">available on CRAN</a>, and it also comes <a rel="nofollow" href="http://mran.microsoft.com/rro/installed/#enhance" target="_blank">pre-installed in Microsoft R</a>. If you'd like to start using R6, the vignette <a rel="nofollow" href="http://mran.microsoft.com/web/packages/R6/vignettes/Introduction.html" target="_blank">Introduction to R6 Classes</a> is a great place to start. You can also check out the development of R6 at the <a rel="nofollow" href="gh:%C2%A0https://github.com/r-lib/R6" target="_blank">R6 Github repository</a>.</p>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://blog.revolutionanalytics.com/2017/07/the-r6-class-system.html"> Revolutions</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/sVZffHW2eTE" height="1"></a></div>