numTerms: Compute the Number of Language Elements in Given Code

View source: R/numTerms.R

numTermsR Documentation

Compute the Number of Language Elements in Given Code

Description

This traverses the R language object and computes the number of individual elements, e.g., symbols, calls, {, parameters/formal arguments.

getTerms can be helpful to see the individual elements and understand the details of a complex language object. It is like recursive unlist of a top-level language object.

Usage

numTerms(x, ctr = mkCounter(self = self, skipIfFalse = skipIfFalse), skipIfFalse = TRUE, self = FALSE)

Arguments

x

the R language/code object

ctr

the code walker which is a list of functions passed to walkCode. It is only useful to specify this explicitly if one wants to walk more than one code object, i.e., reuse the walker across multiple calls to numTerms or getTerms to cumulate all the terms together.

skipIfFalse

a scalar logical indicating whether to skip code in if(FALSE) constructs.

self

a scalar logical indicating whether to include certain objects in the count. Typically leave this as FALSE

Value

numTerms returns a number.

getTerms returns a list of all the language elements found in the code.

Author(s)

Duncan Temple Lang

See Also

walkCode

Examples

   numTerms(extractFunctions)

## Not run: 
   els = as.list(getNamespace("tools"), all.names = TRUE)

   #  takes about 3.7 seconds (on a macbook pro)
   nt = sapply(els, numTerms)
   summary(nt)

## End(Not run)


duncantl/CodeAnalysis documentation built on June 12, 2025, 6:44 a.m.