Hi everyone,
Consider the following function:
source_as_ns <- function(namespace, file){
ns <- namespace::makeNamespace(namespace)
source(file, local = ns)
base::namespaceExport(ns, ls(ns))
}
It creates a namespace containing objects defined in the R script file
. So:
# script.R
foo <- function(x) x + 1
source_as_ns("bar", "script.R")
bar::foo(1)
# 2
Apart from the "why not using a package/namespace" points, are there other reasons not to use a function like this to avoid polluting the Global Env. and have the functions in the script available for use?
Thanks!