Skip to contents

Count code occurrences

Usage

count_occurrences(
  x,
  codes = ".*",
  matchRegexAgainstPaths = TRUE,
  includeDescendents = FALSE,
  includeAncestors = FALSE,
  accumulateCounts = FALSE
)

Arguments

x

A parsed source(s) object.

codes

A regular expression to select codes to include, or, alternatively, a character vector with literal code idenfitiers.

matchRegexAgainstPaths

Whether to match the codes regular expression against the full code paths or only against the code identifier.

Value

A data.frame().

Examples

### Get path to example source
examplePath <-
  system.file("extdata", package="rock");

### Get a path to one example file
exampleFile <-
  file.path(examplePath, "example-3.rock");

### Load example source
loadedExample <- rock::parse_source(exampleFile);

### Show code occurrences
rock::count_occurrences(
  loadedExample
);
#> Called from: rock::count_occurrences(loadedExample)
#> debug: codingLeavesToInclude <- intersect(codesToInclude, x$convenience$codingLeaves)
#> debug: codingParentsToInclude <- setdiff(codesToInclude, c(x$convenience$codingLeaves, 
#>     names(rock::opts$get("codeRegexes"))))
#> debug: leafCodesInQDT <- codingLeavesToInclude[(codingLeavesToInclude %in% 
#>     names(x$qdt))]
#> debug: parentCodesInQDT <- codingParentsToInclude[(codingParentsToInclude %in% 
#>     names(x$qdt))]
#> debug: leafCodesNotInQDT <- codingLeavesToInclude[!(codingLeavesToInclude %in% 
#>     names(x$qdt))]
#> debug: parentCodesNotInQDT <- codingParentsToInclude[!(codingParentsToInclude %in% 
#>     names(x$qdt))]
#> debug: codesToIncludeInQDT <- c(leafCodesInQDT, parentCodesInQDT)
#> debug: counts_total <- apply(x$qdt[, codesToIncludeInQDT], 2, sum)
#> debug: totalUtterances <- nrow(x$qdt)
#> debug: totalCodings <- sum(x$qdt[, codesToIncludeInQDT])
#> debug: totalCodedUtterances <- sum(as.numeric(apply(x$qdt[, codesToIncludeInQDT], 
#>     1, function(row) {
#>         return(any(as.logical(row)))
#>     })))
#> debug: proportions_totalCodedUtterances <- counts_total/totalCodedUtterances
#> debug: res <- data.frame(codeId = codesToIncludeInQDT, count = counts_total, 
#>     totalCodedUtterances = totalCodedUtterances, totalUtterances = totalUtterances)
#> debug: return(res)
#>                          codeId count totalCodedUtterances totalUtterances
#> exampleCodeId_1 exampleCodeId_1     9                   20             102
#> exampleCodeId_2 exampleCodeId_2     7                   20             102
#> exampleCodeId_3 exampleCodeId_3     5                   20             102
#> exampleCodeId_4 exampleCodeId_4     2                   20             102