Skip to content
Prev 35422 / 63424 Next

Locating code that is outside of functions in R scripts

This sources the indicated file into the local environment (i.e. the
environment within src) and then only copies out the objects that are
functions into the workspace so that you are only left with them.

src <- function(file) {
   source(file, local = TRUE)
   for(nm in ls(all = TRUE)) if (is.function(get(nm))) assign(nm,
get(nm), .GlobalEnv)
}

# usage:
src("myfile.R")
On Wed, Dec 23, 2009 at 7:04 PM, <gheine at mathnmaps.com> wrote: