<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/pets-or-livestock-naming-your-rmarkdown-chunks/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.masalmon.eu/2017/08/08/chunkpets/"> Maëlle</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Today I made a <a rel="nofollow" href="https://twitter.com/ma_salmon/status/894877595417948160" target="_blank">confession on Twitter</a>: I told the world I had spent my whole career not naming chunks in RMarkdown documents. Even if I had said one should name them when teaching RMarkdown. But it was also a tweet for showing off since I was working on the first manuscript with named chunks and loving it. I got some interesting reactions to my tweet, including <a rel="nofollow" href="https://twitter.com/thomasp85/status/894882639303368705" target="_blank">one that made me feel better about myself</a> (sorry Thomas), and other ones that made me feel like phrasing why one should name RMarkdown chunks.</p>
<p><a rel="nofollow" href="https://twitter.com/hadleywickham/status/894889893922459648" target="_blank">Hadley Wickham asked</a> whether chunks were pets or livestock, as in <a rel="nofollow" href="https://twitter.com/mikeksmith/status/857583637465878528" target="_blank">his analogy for models</a>. Livestock chunks are identified by numbers, not names, in the case of chunks defined by position. I now think we have good reason to consider them as pets and here’s why…</p>
<p><span></span></p>
<p><img alt="" src="https://i2.wp.com/www.masalmon.eu/figure/source/2017-08-08-chunkpets/running.png?w=456" data-recalc-dims="1"></p>
<p><em>My fur sibling does come if you call him</em></p>
<ul>
<li>
<p>Navigating the RMarkdown document. You can yell a dog’s name and it’ll come to you. Well hopefully… In any case in RStudio you can actually navigate named chunks fairly easily.</p>
</li>
<li>
<p>Making your RMarkdown easier to understand. Even if you comment code, having an informative code chunk name will help your collaborators when they read your file. For that, the chunk names should reflect what’s being done in them, not your creativity, keep that for your real pets or kids. Naming the chunks has moreover made me divide my code into chunks doing one thing, e.g. preparing the data, then fitting a model. David Robinson <a rel="nofollow" href="https://twitter.com/drob/status/738786604731490304" target="_blank">suggested</a> naming chunks such as to make dependencies more explicit, and the thread contains some nice tips about automatic dependencies between chunks, something I need to explore more myself (thanks <a rel="nofollow" href="https://twitter.com/nj_tierney" target="_blank">Nick</a> for providing me with this tweet link!). <a rel="nofollow" href="https://yihui.name/knitr/options/#chunk-options" target="_blank">Don’t use spaces or dots in chunk labels</a>, and Nick even says not to use “_” but rather “-“ because it makes names navigation easier at least on Mac.</p>
</li>
</ul>
<p><img alt="" src="https://i1.wp.com/www.masalmon.eu/figure/source/2017-08-08-chunkpets/chunkdown.png?w=456" data-recalc-dims="1"></p>
<p><em>Chunk down! Don’t worry, Mowgli was just taking a nap.</em></p>
<ul>
<li>
<p>Reading error reports or progress of knitting. It’s much easier to find which chunk exactly is down, or to see how well knitting is going, with chunk names.</p>
</li>
<li>
<p>Moving chunks around, <a rel="nofollow" href="https://twitter.com/robjhyndman/status/894886426885578752" target="_blank">adding some</a>. In particular, cache results are saved by chunk name or position, so caching is more efficient with named chunks. With unnamed chunks you might end up unwillingly re-running that long slow chunk!</p>
</li>
</ul>
<p><img alt="" src="https://i2.wp.com/www.masalmon.eu/figure/source/2017-08-08-chunkpets/cool.png?w=456" data-recalc-dims="1"></p>
<p><em>When you’re cool and you know it</em></p>
<ul>
<li><a rel="nofollow" href="https://twitter.com/pallavipnt/status/894894067179335681" target="_blank">Feeling organized</a> (no shame, Pallavi!) and empowered. And boasting on Twitter.</li>
</ul>
<p>Truth be told maybe I won’t always name my chunks, e.g. in blog posts, but for manuscripts and other bigger projects I think it’s the way to go. And now I need to go read more about <code>autodep</code> in <code>knitr</code>, at least to distract myself for missing my fur sibling too much!</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="http://www.masalmon.eu/2017/08/08/chunkpets/"> Maëlle</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/YMFLSnM4wmc" height="1"></a></div>