Skip to contents

Print method for S3-objects of class "checkglobals" as returned by checkglobals, check_pkg or check_source. Prints the name and location of all unrecognized global variables; and the name and location of all detected imported functions grouped by R-package. The location consists of the source file name and line number. If cli is installed and cli-hyperlinks are supported in the console, clicking the location links opens the source file at the given line number. The bars printed behind the import package names are filled based on the absolute number of detected imports per package.

Usage

# S3 method for checkglobals
print(
  x,
  format = c("basic", "detail"),
  pattern,
  which = c("global", "import"),
  ...
)

Arguments

x

object inheriting from class "checkglobals".

format

character, one of the following two choices:

  • "basic", (default) prints only the name and source code location of the detected globals and imports.

  • "detail", prints the name and location of the detected globals and imports, as well as the lines in the source code file comprising the detected globals and imports. The maximum number of lines printed per source code reference can be specified using maxLines.

pattern

an optional regular expression. Only names matching pattern are returned. glob2rx can be used to convert wildcard patterns to regular expressions.

which

a character vector, either "global" to print all unrecognized global variables, "import" to print all detected imported functions and variables, or both (default).

...

additional arguments to configure the printed output. The following arguments can be specified:

  • all.names, a logical value. If TRUE, all object names are returned. If FALSE, names which begin with a . are omitted. Defaults to TRUE.

  • maxRef, the maximum number of printed source code references per detected global/import. Defaults to 1.

  • maxLines, the maximum number of printed lines per source code reference, only used if format = "detail". Defaults to 5.

  • use_cli, a logical value indicating if cli should be used to format the printed output. Defaults to TRUE, which means that cli-formatting is attempted if cli is installed.

  • maxWidth, the maximum column width of the printed output. If cli is installed, the default width is determined by cli::console_width(). If cli is not installed, getOption("width") is checked. If getOption("width") is undefined, the column width defaults to 80.

Value

Returns the object x

invisibly (via invisible).

Examples

## R-package from folder
chk <- checkglobals(
  pkg = system.file(
    "unit_tests", "pkg", "testpkg",
    package = "checkglobals"
  )
)
chk
#> ! Packages required but not installed: pkgA
#> 
#> ── Unrecognized global functions or variables ──────────────────────────────────
#> 
#>  <name> <location>
#>  g      aaa.R#2 and 12 more...
#> 
#> ── Detected imported functions or variables ────────────────────────────────────
#> 
#>  R6         [◻◻◻◻◻◻◻◻◻] 1/19
#>   └─ R6Class         functions1.R#132
#>  grid       [◻◻◻◻◻◻◻◻◻] 1/19
#>   └─ is.unit         functions1.R#35
#>  methods    [◼◼◼◻◻◻◻◻◻◻] 3/19
#>   ├─ getMethod       functions1.R#67
#>   ├─ setGeneric      functions1.R#48
#>   └─ setMethod       functions1.R#49
#>  parallel   [◼◼◻◻◻◻◻◻◻◻] 2/19
#>   └─ pvec            functions1.R#88 and 1 more...
#>  stats      [◼◼◼◼◼◼◻◻◻◻] 6/19
#>   ├─ aggregate       functions1.R#87
#>   ├─ approxfun       functions1.R#71
#>   ├─ coef            functions1.R#69 and 1 more...
#>   └─ median          functions1.R#31 and 1 more...
#>  stats4     [◼◼◻◻◻◻◻◻◻◻] 2/19
#>   └─ coef            functions1.R#69 and 1 more...
#>  utils      [◼◼◼◼◻◻◻◻◻◻] 4/19
#>   ├─ globalVariables aaa.R#1 and 1 more...
#>   ├─ head            functions1.R#26
#>   └─ tail            functions1.R#65

## print globals with references to source code
print(chk, format = "detail", which = "global", maxRef = 99)
#> ! Packages required but not installed: pkgA
#> 
#> ── Unrecognized global functions or variables ──────────────────────────────────
#> 
#>  <name> <location>
#>  g      aaa.R#2 and 12 more...
#> 
#> ── Global source code references ───────────────────────────────────────────────
#> 
#>g at aaa.R#2:
#>     2: utils::globalVariables(g)
#>   g at functions1.R#5:
#>     5: ff1(g + y)
#>   g at functions1.R#23:
#>     23: requireNamespace(g)
#>   g at functions1.R#79:
#>     79: g
#>   g at functions1.R#83:
#>     83: do.call(args = list(y), what = "g")
#>   g at functions1.R#84:
#>     84: lapply(FUN = "g", 1, n = 1)
#>   g at functions1.R#85:
#>     85: lapply(X = 1, "g")
#>   g at functions1.R#86:
#>     86: Map("g", 1, n = 1)
#>   g at functions1.R#87:
#>     87: stats::aggregate(x ~ ., data = y, FUN = "g")
#>   g at functions1.R#88:
#>     88: pvec(1, "g")
#>   g at functions1.R#90:
#>     90: parallel::pvec(mc.cores = 1L, 1, "g")
#>   g at functions1.R#107:
#>     107: g(1, y)
#>   g at functions1.R#114:
#>     114: g

## print selected imports
print(chk, format = "detail", pattern = "coef", which = "import", maxRef = 99)
#> ! Packages required but not installed: pkgA
#> 
#> ── Detected imported functions or variables ────────────────────────────────────
#> 
#>  stats    [◼◼◻◻◻◻◻◻◻◻] 2/4
#>   └─ coef functions1.R#69 and 1 more...
#>  stats4   [◼◼◻◻◻◻◻◻◻◻] 2/4
#>   └─ coef functions1.R#69 and 1 more...
#> 
#> ── Import source code references ───────────────────────────────────────────────
#> 
#>coef at functions1.R#69:
#>     69: stats::coef(y)
#>   coef at functions1.R#70:
#>     70: stats4::coef(y)

## print without cli-formatting
print(chk, use_cli = FALSE)
#> ! Packages required but not installed: pkgA
#> 
#> ── Unrecognized global functions or variables ──────────────────────────────────
#> 
#>  <name> <location>
#>  g      aaa.R#2 and 12 more...
#> 
#> ── Detected imported functions or variables ────────────────────────────────────
#> 
#>  R6         [◼◻◻◻◻◻◻◻◻◻] 1/19
#>    • R6Class         functions1.R#132
#>  grid       [◼◻◻◻◻◻◻◻◻◻] 1/19
#>    • is.unit         functions1.R#35
#>  methods    [◼◼◼◻◻◻◻◻◻◻] 3/19
#>    • getMethod       functions1.R#67
#>    • setGeneric      functions1.R#48
#>    • setMethod       functions1.R#49
#>  parallel   [◼◼◻◻◻◻◻◻◻◻] 2/19
#>    • pvec            functions1.R#88 and 1 more...
#>  stats      [◼◼◼◼◼◼◻◻◻◻] 6/19
#>    • aggregate       functions1.R#87
#>    • approxfun       functions1.R#71
#>    • coef            functions1.R#69 and 1 more...
#>    • median          functions1.R#31 and 1 more...
#>  stats4     [◼◼◻◻◻◻◻◻◻◻] 2/19
#>    • coef            functions1.R#69 and 1 more...
#>  utils      [◼◼◼◼◻◻◻◻◻◻] 4/19
#>    • globalVariables aaa.R#1 and 1 more...
#>    • head            functions1.R#26
#>    • tail            functions1.R#65