<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/strcode-structure-your-code-better/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://lorenzwalthert.github.io/strcode1/"> Rsome - A blog about some R stuff</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I am pleased to announce my package <code>strcode</code>, a package that should make structuring code easier. You can install it from GitHub, a CRAN submission is planned at a later stage.</p>
<figure>
<pre><code data-lang="r"><span>devtools</span><span>::</span><span>install_github</span><span>(</span><span>&quot;lorenzwalthert/strcode&quot;</span><span>)</span></code></pre>
</figure>
<h1>A concept for code structuring</h1>
<p>The main feature of the package is its function to insert code breaks. These are helpful in breaking the code down into smaller sections. We suggest three levels of granularity for code structuring, wherein higher-level blocks can contain lower-level blocks:</p>
<p></p>
<p>You can notice from the above that:</p>
<ul>
<li>The number of <code>#</code>’s used in front of the break character (<code>___</code>, <code>...</code>, <code>. .</code>) correspond to the level of granularity for a code separator.</li>
<li>The breaks characters <code>___</code>, <code>...</code>, <code>. .</code> were chosen such that they reflect the level of granularity, namely <code>___</code> has a much higher visual density than <code>. .</code>.</li>
<li>Each block has an (optional) short title on what that block is about.</li>
</ul>
<p>Every title line ends with <code>####</code>. Therefore, the titles are recognized by RStudio as <a target="_blank" rel="nofollow" href="https://support.rstudio.com/hc/en-us/articles/200484568-Code-Folding-and-Sections">sections</a>. This has the advantage that you can get a quick summary of your code in RStudio’s code pane as depicted below.</p>
<p><img alt="" src="https://i0.wp.com/github.com/lorenzwalthert/some_raw_data/blob/master/show_contents.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>In addition, it means that you can fold sections as you can fold function declarations or if statements.</p>
<p><img alt="" src="https://i1.wp.com/github.com/lorenzwalthert/some_raw_data/blob/master/show_fold.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The package <code>strcode</code> provides an <a target="_blank" rel="nofollow" href="https://rstudio.github.io/rstudioaddins/">RStudio Add-in</a> to insert each of the three separators presented above – with a single click. To invoke the Add-in, simply click on the button <em>Addins</em> in your RStudio Window and select the separator you want to insert.</p>
<p><img alt="" src="https://i2.wp.com/github.com/lorenzwalthert/some_raw_data/blob/master/show_insert.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>By default, a Shiny Gadget will open in the viewer pane where you can also specify the title of the section (optional) and whether or not a unique identifier/anchor should be added to the section (see below).</p>
<p><img alt="" src="https://i0.wp.com/github.com/lorenzwalthert/some_raw_data/blob/master/show_shiny.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>If you prefer to insert the separator without the Shiny Gadget, you can change the option <code>strcode$insert_with_shiny</code> to <code>FALSE</code> which will only insert the break. The separators all have length 80. The value is looked up in the global option <code>strcode$char_length</code> and can therefore be changed by the user as well. The length of separators is thought to correspond to the character width limit you use.</p>
<h1>Anchoring sections</h1>
<p>Sometimes it is required to refer to a code section, which can be done by a title. A better way, however, is to use a unique hash sequence – let us call it a code anchor – to create an arguably unique reference to that section. A code anchor in <code>strcode</code> is enclosed by <code>#&lt;</code> and <code>>#</code> so all anchors can be found using regular expressions. You can add section breaks that include a hash. Simply tick the box when you insert the break via the Shiny Gadget. The outcome might look like this<br>
</p>
<h1>Summarizing code</h1>
<p>Once code has been structured with the separators introduced above, it can easily be summarized in a compact form. This is particularly handy when the code base is large, when a lot of people work on the code or when new people join a project. The function <code>sum_str</code> is designed for the purpose of extracting separators and respective comments, in order to provide high level code summaries. Thanks to RStudio’s API, you can even create summaries of the file you are working on, simply by typing <code>sum_str()</code> in the console.</p>
<p>The outcome might look like this:</p>
<p></p>
<p><code>sum_str</code> is highly customizable and flexible, with a host of options. For example, you can specify to omit level 3 or level 2 sections in the summary, summarizing multiple files at once, writing the summaries to a file and more.</p>
<h1>Insert a code anchor</h1>
<p>Code anchors might prove helpful in other situations where one wants to anchor a single line. That is also possible with <code>strcode</code>. An example of a code anchor is the following:</p>
<p></p>
<p>The hash sequences in strcode are produced with the R package <a target="_blank" rel="nofollow" href="https://github.com/eddelbuettel/digest">digest</a>.</p>
<p>To wrap it up, strcode tries to make structuring code easy and fun while keeping things simple. Give it a go. Feedback to the package is most welcome. If you find any bugs (in particular in <code>sum_str</code>) or if you have any suggestions for extensions of the package, feel free to open an issue on the GitHub <a target="_blank" rel="nofollow" href="https://github.com/lorenzwalthert/strcode">repo</a> of the package.</p>
<h1>Appendix</h1>
<p>As an appendix to this post, I would like to give a real-world example of how using strcode can improve the legibility of code.</p>
<p></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="https://lorenzwalthert.github.io/strcode1/"> Rsome - A blog about some R stuff</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/oBI6lzgC-0Q"></a></div>