findWhenUnneeded {CodeDepends} | R Documentation |
Determine the code block after which a variable can be explicitly removed
Description
These functions analyze the meta-information about code blocks and determine when a variable is no longer needed and can add code to the relevant code block to remove the variable.
Usage
findWhenUnneeded(var, frags, info = lapply(frags, getInputs), simplify,
index = TRUE, end = NA, redefined = FALSE)
addRemoveIntermediates(doc, frags = readScript(doc),
info = getInputs(frags),
vars = getVariables(info))
Arguments
var |
the name of the variable(s) whose final |
doc |
the location of the script, given as a file name or a connection |
frags |
an object of class |
info |
an object of class
|
simplify |
ignored |
index |
a logical value indicating whether
|
vars |
the names of all the variables of interest |
end |
the value to use if the variable is used in the last code block, i.e. the end of the script. |
redefined |
a logical value which controls whether we return the earliest code block in which the variable is redefined rather than when the variable is no longer used. Redefinition is a kind of "no longer being used" but for the value, not the variable. |
Value
A vector of indices indicating the last expression in which each of the specified variables is an input.
Author(s)
Duncan Temple Lang
See Also
readScript
addRemoveIntermediates
Examples
f = system.file("samples", "cleanVars.R", package = "CodeDepends")
sc = readScript(f)
findWhenUnneeded("x", sc)
findWhenUnneeded(c("x", "y"), sc)
# z is never used
findWhenUnneeded("z", sc)
findWhenUnneeded("z", sc, end = 1L)
code = addRemoveIntermediates(f)
# Note that rm(x), rm(y) and rm(d) are added.
code[c(4, 5, 6)]