Title: | Static Analysis of R-Code Dependencies |
---|---|
Description: | A minimal R-package to approximately detect global and imported functions or variables from R-source code or R-packages by static code analysis. |
Authors: | Joris Chau [aut, cre] |
Maintainer: | Joris Chau <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.2 |
Built: | 2024-11-01 05:39:27 UTC |
Source: | https://github.com/jorischau/checkglobals |
as_vector
is a generic function to cast objects returned by checkglobals
,
check_pkg
or check_source
to list vectors. The function invokes particular
methods which depend on the class
of the first argument.
as_vector(x, pattern, which, ...)
as_vector(x, pattern, which, ...)
x |
an S3-object to convert. |
pattern |
an optional regular expression. Only names
matching |
which |
a character vector, either |
... |
additional arguments to configure the returned output. |
a list of character vectors.
Cast an S3-object of class "checkglobals"
to a list vector.
## S3 method for class 'checkglobals' as_vector(x, pattern, which = c("global", "import"), ...)
## S3 method for class 'checkglobals' as_vector(x, pattern, which = c("global", "import"), ...)
x |
object inheriting from class |
pattern |
an optional regular expression. Only names
matching |
which |
a character vector, either |
... |
additional arguments to configure the output:
|
a list consisting of three character vectors:
global
, vector of global function/variable names.
import
, vector of import function/variable names.
package
, vector of import package names.
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as_vector(chk) ## include only selected imports as_vector(chk, pattern = "coef", which = "import")
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as_vector(chk) ## include only selected imports as_vector(chk, pattern = "coef", which = "import")
Cast an S3-object of class "checkglobalsg"
to a list vector.
## S3 method for class 'checkglobalsg' as_vector(x, pattern, ...)
## S3 method for class 'checkglobalsg' as_vector(x, pattern, ...)
x |
object inheriting from class |
pattern |
an optional regular expression. Only names
matching |
... |
additional arguments to configure the output:
|
a list consisting of one character vector:
global
, vector of global function/variable names.
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as_vector(chk$globals)
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as_vector(chk$globals)
Cast an S3-object of class "checkglobalsi"
to a list vector.
## S3 method for class 'checkglobalsi' as_vector(x, pattern, ...)
## S3 method for class 'checkglobalsi' as_vector(x, pattern, ...)
x |
object inheriting from class |
pattern |
an optional regular expression. Only names
matching |
... |
additional arguments to configure the output:
|
a list consisting of two character vectors:
import
, vector of import function/variable names.
package
, vector of import package names.
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as_vector(chk$imports)
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as_vector(chk$imports)
Cast an S3-object of class "checkglobals"
to a character vector.
## S3 method for class 'checkglobals' as.character(x, pattern, which = c("global", "import"), ...)
## S3 method for class 'checkglobals' as.character(x, pattern, which = c("global", "import"), ...)
x |
object inheriting from class |
pattern |
an optional regular expression. Only names
matching |
which |
a character vector, either |
... |
additional arguments to configure the output:
|
a character vector containing the names of the global or imported function/variables.
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as.character(chk) ## include only imports as.character(chk, which = "import")
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as.character(chk) ## include only imports as.character(chk, which = "import")
Cast an S3-object of class "checkglobalsg"
to a character vector.
## S3 method for class 'checkglobalsg' as.character(x, pattern, ...)
## S3 method for class 'checkglobalsg' as.character(x, pattern, ...)
x |
object inheriting from class |
pattern |
an optional regular expression. Only names
matching |
... |
additional arguments to configure the output:
|
a character vector similar to as.character.checkglobals
.
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as.character(chk$globals)
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as.character(chk$globals)
Cast an S3-object of class "checkglobalsi"
to a character vector.
## S3 method for class 'checkglobalsi' as.character(x, pattern, ...)
## S3 method for class 'checkglobalsi' as.character(x, pattern, ...)
x |
object inheriting from class |
pattern |
an optional regular expression. Only names
matching |
... |
additional arguments to configure the output:
|
a character vector similar to as.character.checkglobals
.
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as.character(chk$imports)
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as.character(chk$imports)
Cast an S3-object of class "checkglobals"
to a data.frame.
## S3 method for class 'checkglobals' as.data.frame( x, row.names = NULL, optional = FALSE, pattern, which = c("global", "import"), ... )
## S3 method for class 'checkglobals' as.data.frame( x, row.names = NULL, optional = FALSE, pattern, which = c("global", "import"), ... )
x |
object inheriting from class |
row.names |
currently not used, included for compatibility with |
optional |
currently not used, included for compatibility with |
pattern |
an optional regular expression. Only names
matching |
which |
a character vector, either |
... |
additional arguments to configure the output:
|
a data.frame with three character columns:
name
, the name of the global or imported function/variable.
package
, the import package, only applies to imported functions/variables.
type
, the type of the detected entity, either "global"
or "import".
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as.data.frame(chk) ## include only selected imports as.data.frame(chk, pattern = "coef", which = "import")
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as.data.frame(chk) ## include only selected imports as.data.frame(chk, pattern = "coef", which = "import")
Cast an S3-object of class "checkglobalsg"
to a data.frame.
## S3 method for class 'checkglobalsg' as.data.frame(x, row.names = NULL, optional = FALSE, pattern, ...)
## S3 method for class 'checkglobalsg' as.data.frame(x, row.names = NULL, optional = FALSE, pattern, ...)
x |
object inheriting from class |
row.names |
currently not used, included for compatibility with |
optional |
currently not used, included for compatibility with |
pattern |
an optional regular expression. Only names
matching |
... |
additional arguments to configure the output:
|
a data.frame similar in format to as.data.frame.checkglobals
.
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as.data.frame(chk$globals)
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as.data.frame(chk$globals)
Cast an S3-object of class "checkglobalsi"
to a data.frame.
## S3 method for class 'checkglobalsi' as.data.frame(x, row.names = NULL, optional = FALSE, pattern, ...)
## S3 method for class 'checkglobalsi' as.data.frame(x, row.names = NULL, optional = FALSE, pattern, ...)
x |
object inheriting from class |
row.names |
currently not used, included for compatibility with |
optional |
currently not used, included for compatibility with |
pattern |
an optional regular expression. Only names
matching |
... |
additional arguments to configure the output:
|
a data.frame similar in format to as.data.frame.checkglobals
.
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as.data.frame(chk$imports)
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as.data.frame(chk$imports)
Cast an S3-object of class "checkglobals"
to a matrix.
## S3 method for class 'checkglobals' as.matrix(x, pattern, which = c("global", "import"), ...)
## S3 method for class 'checkglobals' as.matrix(x, pattern, which = c("global", "import"), ...)
x |
object inheriting from class |
pattern |
an optional regular expression. Only names
matching |
which |
a character vector, either |
... |
additional arguments to configure the output:
|
a character matrix with three columns:
name
, the name of the global or imported function/variable.
package
, the import package, only applies to imported functions/variables.
type
, the type of the detected entity, either "global"
or "import".
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as.matrix(chk) ## include only selected imports as.matrix(chk, pattern = "coef", which = "import")
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as.matrix(chk) ## include only selected imports as.matrix(chk, pattern = "coef", which = "import")
Cast an S3-object of class "checkglobalsg"
to a matrix.
## S3 method for class 'checkglobalsg' as.matrix(x, pattern, ...)
## S3 method for class 'checkglobalsg' as.matrix(x, pattern, ...)
x |
object inheriting from class |
pattern |
an optional regular expression. Only names
matching |
... |
additional arguments to configure the output:
|
a matrix similar in format to as.matrix.checkglobals
.
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as.matrix(chk$globals)
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as.matrix(chk$globals)
Cast an S3-object of class "checkglobalsi"
to a matrix.
## S3 method for class 'checkglobalsi' as.matrix(x, pattern, ...)
## S3 method for class 'checkglobalsi' as.matrix(x, pattern, ...)
x |
object inheriting from class |
pattern |
an optional regular expression. Only names
matching |
... |
additional arguments to configure the output:
|
a matrix similar in format to as.matrix.checkglobals
.
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as.matrix(chk$imports)
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) as.matrix(chk$imports)
Approximately detect global and imported functions or variables from R-packages by static code analysis. Conceptually, the function inspects all files in the package R-folder and contrasts the detected (unrecognized) globals and imports against the imports listed in the NAMESPACE of the R-package. R-scripts present elsewhere in the package (i.e. not in the R-folder) are not scanned, as these are not coupled to the package NAMESPACE file.
check_pkg(pkg = ".", include_compiled = FALSE, skip_globals = NULL)
check_pkg(pkg = ".", include_compiled = FALSE, skip_globals = NULL)
pkg |
character path to R-package, can be either:
|
include_compiled |
logical value indicating if compiled functions called with |
skip_globals |
optional character vector of names to skip/exclude as (unrecognized) global variables. |
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
.
## from R-package folder check_pkg( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) ## from bundled R-package check_pkg( pkg = "https://cran.r-project.org/src/contrib/tinytest_1.4.1.tar.gz", skip_globals = "cluster" )
## from R-package folder check_pkg( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) ## from bundled R-package check_pkg( pkg = "https://cran.r-project.org/src/contrib/tinytest_1.4.1.tar.gz", skip_globals = "cluster" )
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.
check_source(file, text, dir, include_compiled = FALSE, skip_globals = NULL)
check_source(file, text, dir, include_compiled = FALSE, skip_globals = NULL)
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 |
skip_globals |
optional character vector of names to skip/exclude as (unrecognized) global variables. |
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
.
## local R-script check_source( file = system.file( "unit_tests", "pkg", "testpkg", "R", "functions1.R", package = "checkglobals" ) ) ## local R-folder check_source( dir = system.file( "unit_tests", "pkg", "testpkg", "R", package = "checkglobals" ) ) ## R-code string check_source(text = "cc <- function(m) stats::coef(m)") ## R-script from remote location check_source( file = "https://raw.githubusercontent.com/rstudio/shiny-examples/main/004-mpg/app.R" )
## local R-script check_source( file = system.file( "unit_tests", "pkg", "testpkg", "R", "functions1.R", package = "checkglobals" ) ) ## local R-folder check_source( dir = system.file( "unit_tests", "pkg", "testpkg", "R", package = "checkglobals" ) ) ## R-code string check_source(text = "cc <- function(m) stats::coef(m)") ## R-script from remote location check_source( file = "https://raw.githubusercontent.com/rstudio/shiny-examples/main/004-mpg/app.R" )
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 = ".")
.
checkglobals(..., include_compiled = FALSE, skip_globals = NULL)
checkglobals(..., include_compiled = FALSE, skip_globals = NULL)
... |
can be any one of the following arguments:
|
include_compiled |
logical value indicating if compiled functions called with |
skip_globals |
optional character vector of names to skip/exclude as (unrecognized) global variables. |
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
.
## local R-script checkglobals( file = system.file( "unit_tests", "pkg", "testpkg", "R", "functions1.R", package = "checkglobals" ) ) ## local R-folder checkglobals( dir = system.file( "unit_tests", "pkg", "testpkg", "R", package = "checkglobals") ) ## R-code string checkglobals(text = "cc <- function(m) stats::coef(m)") ## R-package from folder checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) ## R-script from remote location checkglobals( file = "https://raw.githubusercontent.com/rstudio/shiny-examples/main/004-mpg/app.R" ) ## R-package from remote location check_pkg( pkg = "https://cran.r-project.org/src/contrib/tinytest_1.4.1.tar.gz", skip_globals = "cluster" )
## local R-script checkglobals( file = system.file( "unit_tests", "pkg", "testpkg", "R", "functions1.R", package = "checkglobals" ) ) ## local R-folder checkglobals( dir = system.file( "unit_tests", "pkg", "testpkg", "R", package = "checkglobals") ) ## R-code string checkglobals(text = "cc <- function(m) stats::coef(m)") ## R-package from folder checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) ## R-script from remote location checkglobals( file = "https://raw.githubusercontent.com/rstudio/shiny-examples/main/004-mpg/app.R" ) ## R-package from remote location check_pkg( pkg = "https://cran.r-project.org/src/contrib/tinytest_1.4.1.tar.gz", skip_globals = "cluster" )
"checkglobals"
objectPrint 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.
## S3 method for class 'checkglobals' print( x, format = c("basic", "detail"), pattern, which = c("global", "import"), ... )
## S3 method for class 'checkglobals' print( x, format = c("basic", "detail"), pattern, which = c("global", "import"), ... )
x |
object inheriting from class |
format |
character, one of the following two choices:
|
pattern |
an optional regular expression. Only names
matching |
which |
a character vector, either |
... |
additional arguments to configure the printed output. The following arguments can be specified:
|
Returns the object x
invisibly (via invisible
).
checkglobals
, check_pkg
, check_source
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) chk ## print globals with references to source code print(chk, format = "detail", which = "global", maxRef = 99) ## print selected imports print(chk, format = "detail", pattern = "coef", which = "import", maxRef = 99) ## print without cli-formatting print(chk, use_cli = FALSE)
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) chk ## print globals with references to source code print(chk, format = "detail", which = "global", maxRef = 99) ## print selected imports print(chk, format = "detail", pattern = "coef", which = "import", maxRef = 99) ## print without cli-formatting print(chk, use_cli = FALSE)
"checkglobalsg"
objectPrint method for S3-objects of class "checkglobalsg"
characteristic to the "globals"
list element of "checkglobals"
objects returned by checkglobals
,
check_pkg
or check_source
.
## S3 method for class 'checkglobalsg' print(x, format = "basic", pattern, ...)
## S3 method for class 'checkglobalsg' print(x, format = "basic", pattern, ...)
x |
object inheriting from class |
format |
character, one of the following two choices:
|
pattern |
an optional regular expression. Only names
matching |
... |
additional arguments to configure the printed output. The following arguments can be specified:
|
Returns the object x
invisibly (via invisible
)
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) chk$globals ## print globals with references to source code print(chk$globals, format = "detail", maxRef = 99) ## print without cli-formatting print(chk$globals, use_cli = FALSE)
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) chk$globals ## print globals with references to source code print(chk$globals, format = "detail", maxRef = 99) ## print without cli-formatting print(chk$globals, use_cli = FALSE)
"checkglobalsi"
objectPrint method for S3-objects of class "checkglobalsi"
characteristic
to the "imports"
list element of "checkglobals"
objects returned by checkglobals
,
check_pkg
or check_source
.
## S3 method for class 'checkglobalsi' print(x, format = "basic", pattern, ...)
## S3 method for class 'checkglobalsi' print(x, format = "basic", pattern, ...)
x |
object inheriting from class |
format |
character, one of the following two choices:
|
pattern |
an optional regular expression. Only names
matching |
... |
additional arguments to configure the printed output. The following arguments can be specified:
|
Returns the object x
invisibly (via invisible
)
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) chk$imports ## print selected imports print(chk, format = "detail", pattern = "coef", which = "import", maxRef = 99) ## print without cli-formatting print(chk$imports, use_cli = FALSE)
## R-package from folder chk <- checkglobals( pkg = system.file( "unit_tests", "pkg", "testpkg", package = "checkglobals" ) ) chk$imports ## print selected imports print(chk, format = "detail", pattern = "coef", which = "import", maxRef = 99) ## print without cli-formatting print(chk$imports, use_cli = FALSE)