View source: R/getToplevelAssigns.R
findAssignsTo | R Documentation |
findAssignsTo
identifies assignments in code with
the left-hand-side being one of a specified set of possible variables
of interest.
There are two types of assignments this function can detect.
The simple/non-complex assignments are of the form
x = val
, x <- val
and x -> val
where val can be a variable, a literal value or a call.
Complex assigments are include calls such as
x[[1]][[i]] = val
,
x$y$z = val
and foo(x, a) = val
.
These involve replacement functions.
findAssignsTo(code, var = character(), complex = TRUE,
pred = if (complex) isComplexAssignTo else isSimpleAssignTo)
isComplexAssignTo(x, var, simpleOk = TRUE)
isSimpleAssignTo(x, var = character())
code , x |
the R code/language object |
var |
the names of the variables being assigned. If this is empty for |
complex |
a logical value. If |
pred |
the predicate function to identify the assignments of interest.
One can specify a different predicate than |
simpleOk |
a logical value. If |
A list of the assignment calls found in the code.
Duncan Temple Lang
R Language manual.
findUnusedArgs
, findUnusedAssignments
bar = function(x) {
a = 1 + x
b = 2 + x^2
ans = a + b
ans
}
findAssignsTo(bar, "a")
findAssignsTo(bar, "b")
findAssignsTo(bar, "ans")
# simple and complex assignments
foo = function(x, y) {
z = 3
x[10] = 1
y$el = 3
foo(y, "abc") = 100
bar(y) = "xyz"
attr(z, "bar") = TRUE
x[[1]][[2]][[3]] = val
x$y$z = val
y$a + sum(x) + z
}
findAssignsTo(foo, "x")
findAssignsTo(foo, "y")
findAssignsTo(foo, "z")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.