View source: R/source_selection.R
source_selection | R Documentation |
The source_selection()
function is the same as
base R's source function, except that it allows only placing
the selected objects and functions into the current environment,
instead of all objects.
The objects to be selected can be specified using any combination of the following:
by supplying a character vector of exact object names to the select
argument.
by supplying a character vector of regex
patterns to the regex
argument.
by supplying a character vector of fixed
patterns to the fixed
argument.
Note that the source_selection()
function does not suppress output
(i.e. plots, prints, messages)
from the sourced script file.
source_selection(lst, select = NULL, regex = NULL, fixed = NULL)
One can specify which objects to expose using arguments
select
, regex
, or fixed
.
The user can specify all 3 of them, but at least one of the 3 must be specified.
It is not a problem if the specifications overlap.
Any specified objects will be placed
in the current environment.
tinycodet_misc, base::source()
exprs <- expression({
helloworld = function()print("helloworld")
goodbyeworld <- function() print("goodbye world")
`%s+test%` <- function(x,y) stringi::`%s+%`(x,y)
`%s*test%` <- function(x,y) stringi::`%s*%`(x,y)
mymethod <- function(x) UseMethod("mymethod", x)
mymethod.numeric <- function(x)x * 2
mymethod.character <- function(x)chartr(x, old = "a-zA-Z", new = "A-Za-z")
})
source_selection(list(exprs=exprs), regex = "^mymethod")
mymethod(1)
mymethod("a")
temp.fun <- function(){
source_selection(list(exprs=exprs), regex = "^mymethod", fixed = c("%", ":="))
ls() # list all objects residing within the function definition
}
temp.fun()
temp.fun <- function(){
source_selection(list(exprs=exprs), select = c("helloworld", "goodbyeworld"))
ls() # list all objects residing within the function definition
}
temp.fun()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.