<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/create-code-metrics-with-cloc/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2018/05/19/create-code-metrics-with-cloc/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The <code>cloc</code> Perl script (yes, <em>Perl</em>!) by Al Danial (<a rel="nofollow" href="https://github.com/AlDanial/cloc" target="_blank">https://github.com/AlDanial/cloc</a>) has been one of the go-to tools for generating code metrics. Given a single file, directory tree, archive, or git repo, <code>cloc</code> can speedily give you metrics on the count of blank lines, comment lines, and physical lines of source code in a <em>vast array</em> of programming languages.</p>
<p>I don’t remember the full context but someone in the R community asked about about this type of functionality and I had tossed together a small script-turned-package to thinly wrap the Perl <code>cloc</code> utility. Said package was and is unimaginatively named <a rel="nofollow" href="https://github.com/hrbrmstr/cloc" target="_blank"><code>cloc</code><img alt="🔗" src="https://i2.wp.com/s.w.org/images/core/emoji/2.4/72x72/1f517.png?w=456&ssl=1" class="wp-smiley" data-recalc-dims="1"></a>. Thanks to some collaborative input from <a rel="nofollow" href="http://twitter.com/ma_salmon" target="_blank">@ma_salmon</a>, the package gained more features. Recently I added the ability to process R markdown (<code>Rmd</code>) files (i.e. only count lines in code chunks) to the main <code>cloc</code> Perl script and was performing some general cleanup when the idea to create some RStudio addins hit me.</p>
<h3><code>cloc</code> Basics</h3>
<p>As noted, you can <code>cloc</code> just about anything. Here’s some metrics for <code>dplyr::group_by</code>:</p>
<pre><code>
cloc(&quot;https://raw.githubusercontent.com/tidyverse/dplyr/master/R/group-by.r&quot;)
## # A tibble: 1 x 10
##   source language file_count file_count_pct   loc loc_pct blank_lines blank_line_pct comment_lines comment_line_pct
## 1 group… R                 1             1.    44      1.          13             1.           110               1.
</code></pre>
<p>and, here’s a similar set of metrics for the whole <code>dplyr</code> package:</p>
<pre><code>
cloc_cran(&quot;dplyr&quot;)
## # A tibble: 7 x 11
##   source language file_count file_count_pct   loc loc_pct blank_lines blank_line_pct comment_lines comment_line_pct
## 1 dplyr… R               148        0.454   13216 0.442          2671       0.380             3876          0.673  
## 2 dplyr… C/C++ H…        125        0.383    6687 0.223          1836       0.261              267          0.0464 
## 3 dplyr… C++              33        0.101    4724 0.158           915       0.130              336          0.0583 
## 4 dplyr… HTML             11        0.0337   3602 0.120           367       0.0522              11          0.00191
## 5 dplyr… Markdown          2        0.00613  1251 0.0418          619       0.0880               0          0.     
## 6 dplyr… Rmd               6        0.0184    421 0.0141          622       0.0884            1270          0.220  
## 7 dplyr… C                 1        0.00307    30 0.00100           7       0.000995             0          0.     
## # ... with 1 more variable: pkg 
</code></pre>
<p>We can also measure (in bulk) from afar, such as the measuring the <code>dplyr</code> git repo:</p>
<pre><code>
cloc_git(&quot;git://github.com/tidyverse/dplyr.git&quot;)
## # A tibble: 12 x 10
##    source    language     file_count file_count_pct   loc  loc_pct blank_lines blank_line_pct comment_lines
##  1 dplyr.git HTML                108        0.236   21467 0.335           3829       0.270             1114
##  2 dplyr.git R                   156        0.341   13648 0.213           2682       0.189             3736
##  3 dplyr.git Markdown             12        0.0263  10100 0.158           3012       0.212                0
##  4 dplyr.git C/C++ Header        126        0.276    6891 0.107           1883       0.133              271
##  5 dplyr.git CSS                   2        0.00438  5684 0.0887          1009       0.0711              39
##  6 dplyr.git C++                  33        0.0722   5267 0.0821          1056       0.0744             393
##  7 dplyr.git Rmd                   7        0.0153    447 0.00697          647       0.0456            1309
##  8 dplyr.git XML                   1        0.00219   291 0.00454            0       0.                   0
##  9 dplyr.git YAML                  6        0.0131    212 0.00331           35       0.00247             12
## 10 dplyr.git JavaScript            2        0.00438    44 0.000686          10       0.000705             4
## 11 dplyr.git Bourne Shell          3        0.00656    34 0.000530          15       0.00106             10
## 12 dplyr.git C                     1        0.00219    30 0.000468           7       0.000493             0
## # ... with 1 more variable: comment_line_pct
</code></pre>
<h3>All in on Addins</h3>
<p>The Rmd functionality made me realize that some interactive capabilities might be handy, so I threw together three of them.</p>
<p>Two of them extraction of code chunks from Rmd documents. One uses <code>cloc</code> other uses <code>knitr::purl()</code> (h/t <a rel="nofollow" href="http://twitter.com/yoniceedee" target="_blank">@yoniceedee</a>). The <code>knitr</code> one adds in some very nice functionality if you want to preserve chunk options and have “<code>eval=FALSE</code>” chunks commented out.</p>
<p><img data-orig-file="https://i2.wp.com/rud.is/b/wp-content/uploads/2018/05/purl.gif?fit=1440%2C900&amp;ssl=1" data-orig-size="1440,900" data-medium-file="https://i2.wp.com/rud.is/b/wp-content/uploads/2018/05/purl.gif?fit=300%2C188&amp;ssl=1" data-attachment-id="10299" data-image-meta="{" data-image-description="" src="https://i2.wp.com/rud.is/b/wp-content/uploads/2018/05/purl.gif?resize=780%2C488&ssl=1" data-permalink="https://rud.is/b/2018/05/19/create-code-metrics-with-cloc/purl/" data-large-file="https://i2.wp.com/rud.is/b/wp-content/uploads/2018/05/purl.gif?fit=530%2C331&amp;ssl=1" alt="" data-image-title="purl" width="450" data-comments-opened="1" class="wp-image-10299" data-recalc-dims="1"></p>
<p><img data-orig-file="https://i0.wp.com/rud.is/b/wp-content/uploads/2018/05/cloc-rmd.gif?fit=1440%2C900&amp;ssl=1" data-orig-size="1440,900" data-medium-file="https://i0.wp.com/rud.is/b/wp-content/uploads/2018/05/cloc-rmd.gif?fit=300%2C188&amp;ssl=1" data-attachment-id="10300" data-image-meta="{" data-image-description="" src="https://i0.wp.com/rud.is/b/wp-content/uploads/2018/05/cloc-rmd.gif?resize=780%2C488&ssl=1" data-permalink="https://rud.is/b/2018/05/19/create-code-metrics-with-cloc/cloc-rmd/" data-large-file="https://i0.wp.com/rud.is/b/wp-content/uploads/2018/05/cloc-rmd.gif?fit=530%2C331&amp;ssl=1" alt="" data-image-title="cloc-rmd" width="450" data-comments-opened="1" class="wp-image-10300" data-recalc-dims="1"></p>
<p>The final one will gather up code metrics for all the sources in an active project.</p>
<p><img alt="" width="450" src="https://i0.wp.com/rud.is/dl/pkg.gif?resize=780%2C488&ssl=1" data-recalc-dims="1"></p>
<h3>FIN</h3>
<p>If you’d like additional features or want to contribute, give (<a rel="nofollow" href="https://github.com/hrbrmstr/cloc" target="_blank">https://github.com/hrbrmstr/cloc</a>) a visit and drop an issue or PR.</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://rud.is/b/2018/05/19/create-code-metrics-with-cloc/"> R – rud.is</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 alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/PXhL5aCtzbE" height="1"></a></div>