Skip to contents

glockr is an R wrapper around scc (Succinct Code Counter), a fast command-line tool that counts lines of code, comments, and blank lines across hundreds of languages. scc also computes cyclomatic complexity estimates, making it useful beyond simple line counting.

scc must be installed and on your PATH. See the scc releases page for binaries.

Functions

Function Description
scc() Per-language summary for one or more paths
scc_by_file() Per-file breakdown for one or more paths
scc_languages() All languages recognized by scc
scc_version() Installed scc version string

Setup

scc_version()
#  [1] "scc version 3.7.0"

Counting by language

scc() returns one row per language. The examples below use the rlang source tree, downloaded and extracted into a session-local tempdir during this vignette’s setup chunk.1

scc(rlang_path) |> 
  gt::gt( ) |> 
  gt::tab_header(
    title = "Succinct Code Counter", 
    subtitle = "rlang R package",
    preheader = NULL)
Succinct Code Counter
rlang R package
language files lines code comments blanks complexity weighted_complexity bytes uloc
R 156 43171 27043 10926 5202 2239 8.279407 1152271 23250
Markdown 52 13437 11344 0 2093 0 0.000000 426556 4755
C 69 13173 10016 864 2293 1827 18.240815 348962 6679
C Header 70 8272 5322 1750 1200 677 12.720782 265714 4689
YAML 8 635 505 26 104 0 0.000000 15354 399
C++ 2 25 21 0 4 0 0.000000 492 17
C++ Header 1 26 21 0 5 0 0.000000 429 21
SVG 10 10 10 0 0 0 0.000000 9687 10
Makefile 1 11 7 0 4 2 28.571429 184 8
License 1 2 2 0 0 0 0.000000 43 2
TOML 1 0 0 0 0 0 0.000000 0 1

Results are sorted by code lines by default. Change the order with sort:

scc(rlang_path, sort = "complexity") |> 
  gt::gt() |> 
  gt::tab_header(
    title = "Succinct Code Counter", 
    subtitle = "sorted by complexity", 
    preheader = "rlang package")
Succinct Code Counter
sorted by complexity
language files lines code comments blanks complexity weighted_complexity bytes uloc
R 156 43171 27043 10926 5202 2239 8.279407 1152271 23250
C 69 13173 10016 864 2293 1827 18.240815 348962 6679
C Header 70 8272 5322 1750 1200 677 12.720782 265714 4689
Makefile 1 11 7 0 4 2 28.571429 184 8
C++ 2 25 21 0 4 0 0.000000 492 17
C++ Header 1 26 21 0 5 0 0.000000 429 21
License 1 2 2 0 0 0 0.000000 43 2
Markdown 52 13437 11344 0 2093 0 0.000000 426556 4755
SVG 10 10 10 0 0 0 0.000000 9687 10
TOML 1 0 0 0 0 0 0.000000 0 1
YAML 8 635 505 26 104 0 0.000000 15354 399

Valid sort values are: "name", "files", "lines", "code", "comments", "blanks", "complexity".

Filtering by extension

Use include_ext to keep only certain file types. Extensions are case-insensitive and match without the leading dot.

scc(rlang_path, include_ext = "r") |> 
  gt::gt() |> 
  gt::tab_header(
    title = "Succinct Code Counter", 
    subtitle = "by file extension", 
    preheader = "rlang package")
Succinct Code Counter
by file extension
language files lines code comments blanks complexity weighted_complexity bytes uloc
R 156 43171 27043 10926 5202 2239 8.279407 1152271 23250

exclude_ext works the opposite way — exclude one or more extensions:

scc(rlang_path, exclude_ext = "r") |> 
  gt::gt() |> 
  gt::tab_header(
    title = "Succinct Code Counter", 
    subtitle = "omit file extensions", 
    preheader = "rlang package")
Succinct Code Counter
omit file extensions
language files lines code comments blanks complexity weighted_complexity bytes uloc
Markdown 52 13437 11344 0 2093 0 0.00000 426556 4755
C 69 13173 10016 864 2293 1827 18.24081 348962 6679
C Header 70 8272 5322 1750 1200 677 12.72078 265714 4689
YAML 8 635 505 26 104 0 0.00000 15354 399
C++ 2 25 21 0 4 0 0.00000 492 17
C++ Header 1 26 21 0 5 0 0.00000 429 21
SVG 10 10 10 0 0 0 0.00000 9687 10
Makefile 1 11 7 0 4 2 28.57143 184 8
License 1 2 2 0 0 0 0.00000 43 2
TOML 1 0 0 0 0 0 0.00000 0 1

Pass a character vector to filter on multiple extensions at once:

scc(rlang_path, include_ext = c("r", "c")) |> 
  gt::gt() |> 
  gt::tab_header(
    title = "Succinct Code Counter", 
    subtitle = ".R and .c files", 
    preheader = "rlang package")
Succinct Code Counter
.R and .c files
language files lines code comments blanks complexity weighted_complexity bytes uloc
R 156 43171 27043 10926 5202 2239 8.279407 1152271 23250
C 69 13173 10016 864 2293 1827 18.240815 348962 6679

Skipping complexity

Complexity calculation adds a small cost on large trees. Pass no_complexity = TRUE to skip it:

scc(rlang_path, no_complexity = TRUE) |> 
  gt::gt() |> 
  gt::tab_header(
    title = "Succinct Code Counter", 
    subtitle = "no complexity calculations", 
    preheader = "rlang package")
Succinct Code Counter
no complexity calculations
language files lines code comments blanks complexity weighted_complexity bytes uloc
R 156 43171 27043 10926 5202 0 0 1152271 23250
Markdown 52 13437 11344 0 2093 0 0 426556 4755
C 69 13173 10016 864 2293 0 0 348962 6679
C Header 70 8272 5322 1750 1200 0 0 265714 4689
YAML 8 635 505 26 104 0 0 15354 399
C++ 2 25 21 0 4 0 0 492 17
C++ Header 1 26 21 0 5 0 0 429 21
SVG 10 10 10 0 0 0 0 9687 10
Makefile 1 11 7 0 4 0 0 184 8
License 1 2 2 0 0 0 0 43 2
TOML 1 0 0 0 0 0 0 0 1