Skip to contents

Approximately detect global and imported functions or variables from R-scripts, folders or R-code strings by static code analysis. For inspection of individual R-scripts use the `file` argument, for R-code strings use the `text` argument, and for folders containing R-scripts use the `dir` argument. This function does not require executing the code under inspection.

Usage

check_source(file, text, dir, include_compiled = FALSE, skip_globals = NULL)

Arguments

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.

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
check_source(
  file = system.file(
    "unit_tests", "pkg", "testpkg", "R", "functions1.R",
    package = "checkglobals"
  )
)
#> 
#> ── Unrecognized global functions or variables ──────────────────────────────────
#> 
#>  <name>      <location>
#>  G           functions1.R#12
#>  dataset     functions1.R#128
#>  g           functions1.R#5 and 11 more...
#>  getMethod() functions1.R#67
#>  pvec()      functions1.R#88 and 1 more...
#>  sysdata     functions1.R#129
#> 
#> ── Detected imported functions or variables ────────────────────────────────────
#> 
#>  R6         [◻◻◻◻◻◻◻◻◻] 1/16
#>   └─ R6Class    functions1.R#132
#>  grid       [◻◻◻◻◻◻◻◻◻] 1/16
#>   └─ is.unit    functions1.R#35
#>  methods    [◼◼◼◻◻◻◻◻◻◻] 3/16
#>   ├─ getMethod  functions1.R#66
#>   ├─ setGeneric functions1.R#48
#>   └─ setMethod  functions1.R#49
#>  parallel   [◻◻◻◻◻◻◻◻◻] 1/16
#>   └─ pvec       functions1.R#90
#>  stats      [◼◼◼◼◼◼◻◻◻◻] 6/16
#>   ├─ aggregate  functions1.R#87
#>   ├─ approxfun  functions1.R#71
#>   ├─ coef       functions1.R#69 and 1 more...
#>   └─ median     functions1.R#31 and 1 more...
#>  stats4     [◼◼◻◻◻◻◻◻◻◻] 2/16
#>   └─ coef       functions1.R#69 and 1 more...
#>  utils      [◼◼◻◻◻◻◻◻◻◻] 2/16
#>   ├─ head       functions1.R#26
#>   └─ tail       functions1.R#65

## local R-folder
check_source(
  dir = system.file(
    "unit_tests", "pkg", "testpkg", "R",
    package = "checkglobals"
  )
)
#> 
#> ── Unrecognized global functions or variables ──────────────────────────────────
#> 
#>  <name>      <location>
#>  dataset     functions1.R#128
#>  g           aaa.R#2 and 12 more...
#>  getMethod() functions1.R#67
#>  pvec()      functions1.R#88 and 1 more...
#>  sysdata     functions1.R#129
#> 
#> ── Detected imported functions or variables ────────────────────────────────────
#> 
#>  R6         [◻◻◻◻◻◻◻◻◻] 1/18
#>   └─ R6Class         functions1.R#132
#>  grid       [◻◻◻◻◻◻◻◻◻] 1/18
#>   └─ is.unit         functions1.R#35
#>  methods    [◼◼◼◻◻◻◻◻◻◻] 3/18
#>   ├─ getMethod       functions1.R#66
#>   ├─ setGeneric      functions1.R#48
#>   └─ setMethod       functions1.R#49
#>  parallel   [◻◻◻◻◻◻◻◻◻] 1/18
#>   └─ pvec            functions1.R#90
#>  stats      [◼◼◼◼◼◼◻◻◻◻] 6/18
#>   ├─ aggregate       functions1.R#87
#>   ├─ approxfun       functions1.R#71
#>   ├─ coef            functions1.R#69 and 1 more...
#>   └─ median          functions1.R#31 and 1 more...
#>  stats4     [◼◼◻◻◻◻◻◻◻◻] 2/18
#>   └─ coef            functions1.R#69 and 1 more...
#>  utils      [◼◼◼◼◻◻◻◻◻◻] 4/18
#>   ├─ globalVariables aaa.R#1 and 1 more...
#>   ├─ head            functions1.R#26
#>   └─ tail            functions1.R#65

## R-code string
check_source(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-script from remote location
# \donttest{
  check_source(
    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
# }