getInputs: Get input and output variables and literals from R...

Description Usage Arguments Value Note Author(s) See Also Examples

Description

This function is used to analyze an R expression and identify the input and output variables in the expressions and related packages that are loaded and files that are referenced.

This might be better called getCodeDepends. It is not to be confused with getVariableInputs.

Usage

1
2
getInputs (e, collector = inputCollector(), basedir = ".", reset =
  FALSE, formulaInputs = FALSE, ...)

Arguments

e

the expression whose code we are to process

collector

an object which collects the different elements of interest in the code.

basedir

the directory for the code relative to which we can resolve file names.

...

additional parameters for methods

reset

a logical value that controls whether we call the collector's reset method before starting to process the expressions in the script.

formulaInputs

Logical indicating whether symbols appearing in formulas should be treated as inputs to the expression. Defaults to FALSE.

Value

A ScriptInfo object containing information about the expression(s) in e.

Things tracked include:

files

the names of any strings used as arguments or literal values that correspond to file names.

strings

A vector of literal strings which appeared in e

libraries

the names of any libraries explicitly loaded within this code.

inputs

a character vector naming the variables that are used as inputs to the computations in this collection of expressions.

outputs

a character vector giving the names of the variables that are assigned values in this block of code, including assignments to elements of a variable, e.g. the variable x in the expression x[[1]] <- 10.

updates

character vector of variables which receive new values when evaluating the expression, but must already exist. Note this does not currently catch some situations, so checking if any symbols appear in both inputs and outputs is still prudent.

functions

a named logical vector, where the names are the names of the functions called and the values indicate whether the function is local (TRUE), from a package (FALSE) or unknown (NA). Note that this is not recursive.

removes

a vector of variables which were removed (via the rm function) in e

nsevalVars

A vector of variables which appear in appear in e, but which are non-standarly evaluated and thus are not typical inputs. Note this classification is determined by the functionhandlers in use by collector.

Note

Users should never call getInputs.langauge directly. It is listed here due to the vagaries of R CMD check documentation checks.

Author(s)

Duncan Temple Lang

See Also

parse

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
  frags = parse(system.file("samples", "dual.R", package = "CodeDepends"))  
  # formula involves non-df variables
  inputs = lapply(frags, getInputs, formulaInputs=TRUE)
  inputs
  sapply(inputs, slot, "outputs")

   # Specify the base directory in which to resolve the file names.
  getInputs(frags[[5]], basedir = system.file("samples", package = "CodeDepends"))


  f = system.file("samples", "namedAnnotatedScript.R", package = "CodeDepends")
  sc = readScript(f, "labeled")
  getInputs(sc)
  getInputs(sc[[2]])

Example output

[[1]]
An object of class "ScriptNodeInfo"
Slot "files":
character(0)

Slot "strings":
character(0)

Slot "libraries":
character(0)

Slot "inputs":
character(0)

Slot "outputs":
[1] "n"   "x"   "e"   "abc"

Slot "updates":
character(0)

Slot "functions":
    { rnorm 
   NA    NA 

Slot "removes":
character(0)

Slot "nsevalVars":
character(0)

Slot "sideEffects":
character(0)

Slot "code":
{
    n = 100
    x = rnorm(n)
    e = rnorm(n)
    abc = 1
}


[[2]]
An object of class "ScriptNodeInfo"
Slot "files":
character(0)

Slot "strings":
character(0)

Slot "libraries":
character(0)

Slot "inputs":
[1] "x" "e"

Slot "outputs":
[1] "y"

Slot "updates":
character(0)

Slot "functions":
 {  +  * 
NA NA NA 

Slot "removes":
character(0)

Slot "nsevalVars":
character(0)

Slot "sideEffects":
character(0)

Slot "code":
{
    y = 3 + 10 * x + e
}


[[3]]
An object of class "ScriptNodeInfo"
Slot "files":
character(0)

Slot "strings":
character(0)

Slot "libraries":
character(0)

Slot "inputs":
character(0)

Slot "outputs":
[1] "a" "b" "k"

Slot "updates":
character(0)

Slot "functions":
    {     :   sum rpois 
   NA    NA    NA    NA 

Slot "removes":
[1] "a" "b"

Slot "nsevalVars":
character(0)

Slot "sideEffects":
character(0)

Slot "code":
{
    a = 1:3
    b = sum(a)
    k = rpois(3, b)
    rm(a, b)
}


[[4]]
An object of class "ScriptNodeInfo"
Slot "files":
character(0)

Slot "strings":
character(0)

Slot "libraries":
character(0)

Slot "inputs":
[1] "y" "x"

Slot "outputs":
[1] "fit"

Slot "updates":
character(0)

Slot "functions":
   {   lm    ~ plot 
  NA   NA   NA   NA 

Slot "removes":
[1] "y" "x"

Slot "nsevalVars":
character(0)

Slot "sideEffects":
character(0)

Slot "code":
{
    fit = lm(y ~ x)
    plot(fit)
    rm(y, x)
}


[[5]]
An object of class "ScriptNodeInfo"
Slot "files":
character(0)

Slot "strings":
[1] "foo.R"

Slot "libraries":
character(0)

Slot "inputs":
character(0)

Slot "outputs":
character(0)

Slot "updates":
character(0)

Slot "functions":
     { source 
    NA     NA 

Slot "removes":
character(0)

Slot "nsevalVars":
character(0)

Slot "sideEffects":
character(0)

Slot "code":
{
    source("foo.R")
}


[[1]]
[1] "n"   "x"   "e"   "abc"

[[2]]
[1] "y"

[[3]]
[1] "a" "b" "k"

[[4]]
[1] "fit"

[[5]]
character(0)

An object of class "ScriptNodeInfo"
Slot "files":
character(0)

Slot "strings":
[1] "foo.R"

Slot "libraries":
character(0)

Slot "inputs":
character(0)

Slot "outputs":
character(0)

Slot "updates":
character(0)

Slot "functions":
     { source 
    NA     NA 

Slot "removes":
character(0)

Slot "nsevalVars":
character(0)

Slot "sideEffects":
character(0)

Slot "code":
{
    source("foo.R")
}

An object of class "ScriptInfo"
[[1]]
An object of class "ScriptNodeInfo"
Slot "files":
character(0)

Slot "strings":
character(0)

Slot "libraries":
character(0)

Slot "inputs":
character(0)

Slot "outputs":
[1] "n"   "x"   "e"   "abc"

Slot "updates":
character(0)

Slot "functions":
    { rnorm 
FALSE FALSE 

Slot "removes":
character(0)

Slot "nsevalVars":
character(0)

Slot "sideEffects":
character(0)

Slot "code":
{
    n = 100
    x = rnorm(n)
    e = rnorm(n)
    abc = 1
}


[[2]]
An object of class "ScriptNodeInfo"
Slot "files":
character(0)

Slot "strings":
character(0)

Slot "libraries":
character(0)

Slot "inputs":
[1] "x" "e"

Slot "outputs":
[1] "y"

Slot "updates":
character(0)

Slot "functions":
    {     +     * 
FALSE FALSE FALSE 

Slot "removes":
character(0)

Slot "nsevalVars":
character(0)

Slot "sideEffects":
character(0)

Slot "code":
{
    y = 3 + 10 * x + e
}


[[3]]
An object of class "ScriptNodeInfo"
Slot "files":
character(0)

Slot "strings":
character(0)

Slot "libraries":
character(0)

Slot "inputs":
character(0)

Slot "outputs":
[1] "a" "b" "k"

Slot "updates":
character(0)

Slot "functions":
    {     :   sum rpois 
FALSE FALSE FALSE FALSE 

Slot "removes":
character(0)

Slot "nsevalVars":
character(0)

Slot "sideEffects":
character(0)

Slot "code":
{
    a = 1:3
    b = sum(a)
    k = rpois(3, b)
}


[[4]]
An object of class "ScriptNodeInfo"
Slot "files":
character(0)

Slot "strings":
character(0)

Slot "libraries":
character(0)

Slot "inputs":
character(0)

Slot "outputs":
[1] "fit"

Slot "updates":
character(0)

Slot "functions":
    {    lm     ~  plot 
FALSE FALSE FALSE FALSE 

Slot "removes":
character(0)

Slot "nsevalVars":
[1] "y" "x"

Slot "sideEffects":
character(0)

Slot "code":
{
    fit = lm(y ~ x)
    plot(fit)
}


[[5]]
An object of class "ScriptNodeInfo"
Slot "files":
character(0)

Slot "strings":
[1] "foo.R"

Slot "libraries":
character(0)

Slot "inputs":
character(0)

Slot "outputs":
character(0)

Slot "updates":
character(0)

Slot "functions":
     { source 
 FALSE  FALSE 

Slot "removes":
character(0)

Slot "nsevalVars":
character(0)

Slot "sideEffects":
character(0)

Slot "code":
{
    source("foo.R")
}


An object of class "ScriptNodeInfo"
Slot "files":
character(0)

Slot "strings":
character(0)

Slot "libraries":
character(0)

Slot "inputs":
[1] "x" "e"

Slot "outputs":
[1] "y"

Slot "updates":
character(0)

Slot "functions":
 {  +  * 
NA NA NA 

Slot "removes":
character(0)

Slot "nsevalVars":
character(0)

Slot "sideEffects":
character(0)

Slot "code":
{
    y = 3 + 10 * x + e
}

CodeDepends documentation built on July 18, 2018, 9:02 a.m.