Count Lines of Code, Comments and Whitespace in Source Files and Archives

Description

Counts blank lines, comment lines, and physical lines of source code in source files/trees/archives. An R wrapper to the Perl cloc utility https://github.com/AlDanial/cloc by @AlDanial.

What’s Inside The Tin

The following functions are implemented:

  • cloc: Count lines of code, comments and whitespace in source files/archives
  • cloc_by_file: Count lines of code, comments and whitespace in source files/archives by file
  • cloc_cran: Count lines of code (etc) from source packages on CRAN
  • cloc_git: Count lines of code, comments and whitespace in a git tree
  • cloc_remove_comments: Strip comments and white space from a single source file
  • cloc_recognized_languages: Return a data frame of ‘cloc’ recognized languages and associated extensions
  • cloc_call: Call ‘cloc.pl’ directly with granular control over options
  • clock_pkg_md : Run cloc_pkg() on active package and format it as a markdown table for knitting into reports
  • cloc_help: See the command-line help
  • cloc_version: Retrieve the version of the embedded perl script
  • cloc_os: Ask the embedded perl script to detect the OS type

Usage

devtools::install_github("hrbrmstr/cloc")

Basic usage

# by dir
cloc(system.file("extdata", package="cloc"))
#> # A tibble: 2 x 10
#>   source  language file_count file_count_pct   loc loc_pct blank_lines blank_line_pct comment_lines comment_line_pct
#>   <chr>   <chr>         <int>          <dbl> <int>   <dbl>       <int>          <dbl>         <int>            <dbl>
#> 1 extdata C++               1            0.5   142   0.507          41          0.631            63            0.470
#> 2 extdata R                 1            0.5   138   0.493          24          0.369            71            0.530

# by file
cloc(system.file("extdata", "qrencoder.cpp", package="cloc"))
#> # 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
#>   <chr>       <chr>         <int>          <dbl> <int>   <dbl>       <int>          <dbl>         <int>            <dbl>
#> 1 qrencoder.… C++               1              1   142       1          41              1            63                1

# from a url
cloc("https://rud.is/dl/cloc-1.74.tar.gz")
#> # A tibble: 93 x 10
#>    source    language  file_count file_count_pct   loc loc_pct blank_lines blank_line_pct comment_lines comment_line_pct
#>    <chr>     <chr>          <int>          <dbl> <int>   <dbl>       <int>          <dbl>         <int>            <dbl>
#>  1 cloc-1.7… Perl               5        0.0180  19712  0.598         1353       0.420             2430          0.443  
#>  2 cloc-1.7… YAML             141        0.507    2887  0.0876           1       0.000311           141          0.0257 
#>  3 cloc-1.7… Markdown           1        0.00360  2195  0.0666         226       0.0702              26          0.00474
#>  4 cloc-1.7… ANTLR Gr…          2        0.00719  1012  0.0307         200       0.0621              59          0.0108 
#>  5 cloc-1.7… R                  3        0.0108    698  0.0212          95       0.0295             312          0.0569 
#>  6 cloc-1.7… C/C++ He…          1        0.00360   617  0.0187         191       0.0593             780          0.142  
#>  7 cloc-1.7… C++                4        0.0144    570  0.0173         132       0.0410             173          0.0315 
#>  8 cloc-1.7… Forth              2        0.00719   529  0.0160          17       0.00528             84          0.0153 
#>  9 cloc-1.7… TypeScri…          3        0.0108    410  0.0124          52       0.0162              39          0.00711
#> 10 cloc-1.7… Logtalk            1        0.00360   368  0.0112          59       0.0183              57          0.0104 
#> # … with 83 more rows

Custom CRAN package counter:

git tree

git tree (with specific commit)

remote git tree

Detailed results by file

Recognized languages

Strip comments and whitespace from individual source files

cloc Metrics

Lang # Files (%) LoC (%) Blank lines (%) # Lines (%)
R 16 0.94 484 0.95 183 0.83 289 0.8
Rmd 1 0.06 24 0.05 38 0.17 71 0.2

Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.