Skip to contents

Counting by file

scc_by_file() expands results to one row per source file, adding filename, location, and logical flags for generated and minified files.

rlang_by_file <- scc_by_file(rlang_path) 
str(rlang_by_file)
#> tibble [371 × 13] (S3: tbl_df/tbl/data.frame)
#>  $ language           : chr [1:371] "R" "R" "R" "R" ...
#>  $ filename           : chr [1:371] "sink.R" "standalone-lazyeval.R" "arg.R" "test-operators.R" ...
#>  $ location           : chr [1:371] "/tmp/RtmpRu3UGt/rlang-1.1.6/tests/sink.R" "/tmp/RtmpRu3UGt/rlang-1.1.6/R/standalone-lazyeval.R" "/tmp/RtmpRu3UGt/rlang-1.1.6/R/arg.R" "/tmp/RtmpRu3UGt/rlang-1.1.6/tests/testthat/test-operators.R" ...
#>  $ lines              : int [1:371] 8 112 481 79 7 298 40 83 277 256 ...
#>  $ code               : int [1:371] 6 89 166 63 4 114 4 58 222 139 ...
#>  $ comments           : int [1:371] 1 12 281 0 1 161 30 15 2 93 ...
#>  $ blanks             : int [1:371] 1 11 34 16 2 23 6 10 53 24 ...
#>  $ complexity         : int [1:371] 0 15 30 0 0 10 0 12 1 10 ...
#>  $ weighted_complexity: num [1:371] 0 16.9 18.1 0 0 ...
#>  $ bytes              : int [1:371] 171 2285 13409 2233 135 6924 832 2167 6836 6411 ...
#>  $ uloc               : int [1:371] 8 83 314 55 6 169 19 57 140 158 ...
#>  $ generated          : logi [1:371] FALSE FALSE FALSE FALSE FALSE FALSE ...
#>  $ minified           : logi [1:371] FALSE FALSE FALSE FALSE FALSE FALSE ...

Let’s take a look at this by complexity, and drop location, bytes, generated, and minified.

rlang_by_file_cm <- rlang_by_file[ , c("language", "filename", "lines", 
                                        "blanks", "comments", "uloc", 
                                        "complexity", "weighted_complexity")]
rlang_by_file_cm[order(rlang_by_file_cm$complexity, decreasing = TRUE), ] |> 
  head(10) |> 
  gt::gt() |> 
  gt::tab_header(
    title = "Top 10 Complex Files", 
    subtitle = "by complexity", 
    preheader = "rlang package")
Top 10 Complex Files
by complexity
language filename lines blanks comments uloc complexity weighted_complexity
C Header xxhash.h 4766 520 1588 2888 474 17.83296
R cnd-abort.R 1561 141 729 1023 152 21.99711
C parse.c 472 36 1 259 140 32.18391
R trace.R 1170 175 188 714 134 16.60471
R standalone-cli.R 572 49 172 395 131 37.32194
C dots.c 1143 175 24 680 129 13.66525
C exported.c 1096 165 59 606 112 12.84404
R deparse.R 1092 159 114 605 111 13.55311
R standalone-vctrs.R 669 116 48 364 107 21.18812
R call.R 1061 75 550 694 92 21.10092

Filter to just the R source files and sort by complexity:

r_files <- scc_by_file(rlang_path, include_ext = "r")
r_files_sub <- r_files[, c("filename", "lines", "code", "comments", "blanks", "complexity")]
r_files_sub[order(r_files_sub$complexity, decreasing = TRUE), ] |> 
  head(10) |> 
  gt::gt() |> 
  gt::tab_header(
    title = "Top 10 Complex R Files", 
    subtitle = "rlang package", 
    preheader = "rlang package")
Top 10 Complex R Files
rlang package
filename lines code comments blanks complexity
cnd-abort.R 1561 691 729 141 152
trace.R 1170 807 188 175 134
standalone-cli.R 572 351 172 49 131
deparse.R 1092 819 114 159 111
standalone-vctrs.R 669 505 48 116 107
call.R 1061 436 550 75 92
standalone-types-check.R 591 483 61 47 72
standalone-obj-type.R 360 242 79 39 67
cnd-entrace.R 405 213 147 45 61
utils.R 372 302 11 59 59

For visualization of these per-file metrics, see the Visualization vignette. ## Call stack

The tree below comes from stackcallr. call_tree_dir() parses the files in R/ without running them. scc_by_file() uses the same helpers as scc(), but passes by_file = TRUE to build_args() and parse_scc_json().

stackcallr::call_tree_dir("../R", root = "scc_by_file")
█─scc_by_file
├─find_scc
├─build_args
├─█─parse_scc_json
│ ├─%||%
│ └─empty_scc_tibble
└─scc_cocomo