The idea is to reassemble the language objects and collect information.
Examples of what we want to do are
fn = function()...
if(TRUE) ... else ...
to remove the else ...
These operations will allow us to implement
The following is a very simple example, intentionally so.
foo =
function(x, lambda = 2)
{
alpha = lambda/nrow(x)
do = function(val)
val * alpha
apply(x, 2, do)
}
We want to extract do
as a separate function.
It needs alpha, so we would add that as an additional parameter. We write it as
do = function(val, alpha)
val * alpha
Now, we need to add alpha
in the calls to do
.
We'd rewrite foo
as
foo =
function(x, lambda = 2)
{
alpha = lambda/nrow(x)
apply(x, 2, do, alpha)
}
A simpler version is when the original function calls the nested function directly.
foo =
function(x, lambda = 2)
{
alpha = lambda/nrow(x)
do = function(val)
val * alpha
a = do(x[, 1])
b = sum(do(x[ !is.na(x[ ,2]), 2]))
}
We would rewrite this as
foo =
function(x, lambda = 2)
{
alpha = lambda/nrow(x)
a = do(x[, 1], alpha)
b = sum(do(x[ !is.na(x[ ,2]), 2], alpha))
}
character vector of c(name = newName,...) and rewrite() like in propagate.R
passGlobals
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.