CodeDepends (version 0.6.5)

findWhenUnneeded: 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 Script which is a list containing the code blocks in the script. This is typically obtained via a call to readScript.

info

an object of class ScriptInfo which is a list of ScriptNodeInfo objects.

simplify

ignored

index

a logical value indicating whether findWhenUnneeded should return the indices of the code blocks/fragments or the code fragments themselves.

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.

See Also

readScript addRemoveIntermediates

Examples

Run this code
# NOT RUN {
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)]
# }

Run the code above in your browser using DataCamp Workspace