Skip to contents

Approximately detect global and imported functions or variables from R-scripts, folders, R-code strings or R-packages by static code analysis. This function is simply a convenience wrapper around check_source and check_pkg and the return value is the same as calling these functions directly. If called without a file, dir, text or pkg argument, the function is run in the current working directory. If the current working directory is an R-package folder, this is identical to checkglobals(pkg = "."), otherwise the behavior is the same as checkglobals(dir = ".").

Usage

checkglobals(..., include_compiled = FALSE, skip_globals = NULL)

Arguments

...

can be any one of the following arguments:

  • file, file character path to R-script to analyze, can be either a file on the local filesystem or a remote file location (e.g. a server or the web).

  • text, character R-code string to analyze.

  • dir, character path to folder with R-scripts to analyze.

  • pkg, character path to R-package, can be either:

    • a local R-package folder;

    • path to bundled (tar.gz) R-package on local filesystem;

    • remote path to bundled (tar.gz) R-package, (e.g. a remote server or the web).

include_compiled

logical value indicating if compiled functions called with .Call, .C, .External, .Fortran should be included as global variables.

skip_globals

optional character vector of names to skip/exclude as (unrecognized) global variables.

Value

list S3-object of class "checkglobals" with three components:

  • globals, list of class "checkglobalsg"

  • imports, list of class "checkglobalsi"

  • missing_packages, character vector with missing packages

for programmatic use, cast the returned S3-object with as.data.frame, as.matrix, as.character or as_vector.

Examples

## local R-script
checkglobals(
  file = system.file(
    "unit_tests", "pkg", "testpkg", "R", "functions1.R",
    package = "checkglobals"
  )
)
#> 
#> ── Unrecognized global functions or variables ──────────────────────────────────
#> 
#>  <name>      <location>
#>  %>%()       functions1.R#97
#>  G           functions1.R#12
#>  dataset     functions1.R#135
#>  fAttach()   functions1.R#152
#>  fLoad()     functions1.R#150
#>  fLoad2()    functions1.R#156
#>  g           functions1.R#5 and 12 more...
#>  getMethod() functions1.R#73
#>  pvec()      functions1.R#94 and 1 more...
#>  sysdata     functions1.R#136
#>  xauth       functions1.R#151
#> 
#> ── Detected imported functions or variables ────────────────────────────────────
#> 
#>  R6             [◻◻◻◻◻◻◻◻◻] 1/17
#>   └─ R6Class    functions1.R#139
#>  checkglobals   [◻◻◻◻◻◻◻◻◻◻] 0/17
#>   └─ n/a
#>  grid           [◻◻◻◻◻◻◻◻◻] 1/17
#>   └─ is.unit    functions1.R#35
#>  methods        [◼◼◼◻◻◻◻◻◻◻] 3/17
#>   ├─ getMethod  functions1.R#72
#>   ├─ setGeneric functions1.R#54
#>   └─ setMethod  functions1.R#55
#>  parallel       [◻◻◻◻◻◻◻◻◻] 1/17
#>   └─ pvec       functions1.R#96
#>  stats          [◼◼◼◼◼◼◼◻◻◻] 7/17
#>   ├─ aggregate  functions1.R#93
#>   ├─ approxfun  functions1.R#77
#>   ├─ coef       functions1.R#75 and 1 more...
#>   ├─ median     functions1.R#31 and 1 more...
#>   └─ sd         functions1.R#161
#>  stats4         [◼◼◻◻◻◻◻◻◻◻] 2/17
#>   └─ coef       functions1.R#75 and 1 more...
#>  utils          [◼◼◻◻◻◻◻◻◻◻] 2/17
#>   ├─ head       functions1.R#26
#>   └─ tail       functions1.R#71

## local R-folder
checkglobals(
  dir = system.file(
    "unit_tests", "pkg", "testpkg", "R",
    package = "checkglobals")
)
#> 
#> ── Unrecognized global functions or variables ──────────────────────────────────
#> 
#>  <name>      <location>
#>  %>%()       functions1.R#97
#>  G           functions1.R#12
#>  dataset     functions1.R#135
#>  fAttach()   functions1.R#152
#>  fLoad()     functions1.R#150
#>  fLoad2()    functions1.R#156
#>  g           aaa.R#2 and 13 more...
#>  getMethod() functions1.R#73
#>  pvec()      functions1.R#94 and 1 more...
#>  sysdata     functions1.R#136
#>  xauth       functions1.R#151
#> 
#> ── Detected imported functions or variables ────────────────────────────────────
#> 
#>  R6             [◻◻◻◻◻◻◻◻◻] 1/20
#>   └─ R6Class         functions1.R#139
#>  checkglobals   [◻◻◻◻◻◻◻◻◻◻] 0/20
#>   └─ n/a
#>  grid           [◻◻◻◻◻◻◻◻◻] 1/20
#>   └─ is.unit         functions1.R#35
#>  methods        [◼◼◼◻◻◻◻◻◻◻] 3/20
#>   ├─ getMethod       functions1.R#72
#>   ├─ setGeneric      functions1.R#54
#>   └─ setMethod       functions1.R#55
#>  parallel       [◻◻◻◻◻◻◻◻◻] 1/20
#>   └─ pvec            functions1.R#96
#>  stats          [◼◼◼◼◼◼◼◻◻◻] 7/20
#>   ├─ aggregate       functions1.R#93
#>   ├─ approxfun       functions1.R#77
#>   ├─ coef            functions1.R#75 and 1 more...
#>   ├─ median          functions1.R#31 and 1 more...
#>   └─ sd              functions1.R#161
#>  stats4         [◼◼◻◻◻◻◻◻◻◻] 2/20
#>   └─ coef            functions1.R#75 and 1 more...
#>  utils          [◼◼◼◼◼◻◻◻◻◻] 5/20
#>   ├─ globalVariables aaa.R#1 and 1 more...
#>   ├─ head            functions1.R#26
#>   ├─ maintainer      aaa.R#8
#>   └─ tail            functions1.R#71

## R-code string
checkglobals(text = "cc <- function(m) stats::coef(m)")
#> 
#> ── Unrecognized global functions or variables ──────────────────────────────────
#> 
#>  None detected
#> 
#> ── Detected imported functions or variables ────────────────────────────────────
#> 
#>  stats   [◻◻◻◻◻◻◻◻◻] 1/1
#>   └─ coef <text>#1

## R-package from folder
checkglobals(
  pkg = system.file(
    "unit_tests", "pkg", "testpkg",
    package = "checkglobals"
  )
)
#> ! Packages required but not installed: pkgA
#> 
#> ── Unrecognized global functions or variables ──────────────────────────────────
#> 
#>  <name>     <location>
#>  %>%()      functions1.R#97
#>  fAttach2() aaa.R#13
#>  fLoad2()   functions1.R#156
#>  g          aaa.R#2 and 13 more...
#> 
#> ── Detected imported functions or variables ────────────────────────────────────
#> 
#>  R6             [◻◻◻◻◻◻◻◻◻] 1/21
#>   └─ R6Class         functions1.R#139
#>  checkglobals   [◻◻◻◻◻◻◻◻◻◻] 0/21
#>   └─ n/a
#>  grid           [◻◻◻◻◻◻◻◻◻] 1/21
#>   └─ is.unit         functions1.R#35
#>  methods        [◼◼◼◻◻◻◻◻◻◻] 3/21
#>   ├─ getMethod       functions1.R#73
#>   ├─ setGeneric      functions1.R#54
#>   └─ setMethod       functions1.R#55
#>  parallel       [◼◼◻◻◻◻◻◻◻◻] 2/21
#>   └─ pvec            functions1.R#94 and 1 more...
#>  pkgA           [◻◻◻◻◻◻◻◻◻◻] 0/21
#>   └─ n/a
#>  stats          [◼◼◼◼◼◼◼◻◻◻] 7/21
#>   ├─ aggregate       functions1.R#93
#>   ├─ approxfun       functions1.R#77
#>   ├─ coef            functions1.R#75 and 1 more...
#>   ├─ median          functions1.R#31 and 1 more...
#>   └─ sd              functions1.R#161
#>  stats4         [◼◼◻◻◻◻◻◻◻◻] 2/21
#>   └─ coef            functions1.R#75 and 1 more...
#>  tools          [◻◻◻◻◻◻◻◻◻◻] 0/21
#>   └─ n/a
#>  utils          [◼◼◼◼◼◻◻◻◻◻] 5/21
#>   ├─ globalVariables aaa.R#1 and 1 more...
#>   ├─ head            functions1.R#26
#>   ├─ maintainer      aaa.R#8
#>   └─ tail            functions1.R#71

## R-script from remote location
# \donttest{
checkglobals(
  file = "https://raw.githubusercontent.com/rstudio/shiny-examples/main/004-mpg/app.R"
)
#> ! Packages required but not installed: shiny
#> 
#> ── Unrecognized global functions or variables ──────────────────────────────────
#> 
#>  <name>          <location>
#>  as.formula()    app.R#66
#>  boxplot()       app.R#66
#>  checkboxInput() app.R#13
#>  fluidPage()     app.R#13
#>  h3()            app.R#13
#>  mainPanel()     app.R#13
#>  plotOutput()    app.R#13
#>  reactive()      app.R#54
#>  renderPlot()    app.R#65
#>  renderText()    app.R#59
#>  selectInput()   app.R#13
#>  shinyApp()      app.R#75
#>  sidebarLayout() app.R#13
#>  sidebarPanel()  app.R#13
#>  textOutput()    app.R#13
#>  titlePanel()    app.R#13
#> 
#> ── Detected imported functions or variables ────────────────────────────────────
#> 
#>  datasets   [◻◻◻◻◻◻◻◻◻] 1/1
#>   └─ mtcars app.R#8
#>  shiny      [◻◻◻◻◻◻◻◻◻◻] 0/1
#>   └─ n/a
# }

## R-package from remote location
# \donttest{
check_pkg(
  pkg = "https://cran.r-project.org/src/contrib/tinytest_1.4.1.tar.gz",
  skip_globals = "cluster"
)
#> ! Packages required but not installed: tinytest
#> 
#> ── Unrecognized global functions or variables ──────────────────────────────────
#> 
#>  None detected
#> 
#> ── Detected imported functions or variables ────────────────────────────────────
#> 
#>  grDevices   [◼◼◻◻◻◻◻◻◻◻] 2/13
#>   ├─ dev.off          tinytest.R#560
#>   └─ pdf              tinytest.R#589
#>  parallel    [◼◼◼◼◼◼◼◻◻◻] 7/13
#>   ├─ clusterCall      tinytest.R#1046
#>   ├─ clusterEvalQ     tinytest.R#880 and 1 more...
#>   ├─ clusterExport    tinytest.R#1043
#>   ├─ makeCluster      tinytest.R#1030
#>   ├─ parLapply        tinytest.R#881
#>   └─ stopCluster      tinytest.R#1000
#>  utils       [◼◼◼◼◻◻◻◻◻◻] 4/13
#>   ├─ capture.output   methods.R#197
#>   ├─ file_test        tinytest.R#570
#>   ├─ getFromNamespace tinytest.R#384
#>   └─ install.packages tinytest.R#1140
# }