I’d like to assign a function to variable, using a string. The
function in the
package does almost
exactly what I want. For example,
valueReadFromFile <- "median" ds <- data.frame(X=rnorm(10), Y=rnorm(10)) dynamicFunction <- get(valueReadFromFile) dynamicFunction(ds$X) #Returns the variable's median.
However, I want to qualify the function with its package, so that I don’t have to worry about (a) loading the function’s package with
, or (b) calling the wrong function in a different package.
Is there a robust, programmatic way I can qualify a function’s name with its package using
(or some similar function)? The following code doesn’t work, presumably because
doesn’t know how to interpret the package name before the
require(scales) #This package has functions called `alpha()` and `rescale()` require(psych) #This package also has functions called `alpha()` and `rescale()` dynamicFunction1 <- get("scales::alpha") dynamicFunction2 <- get("psych::alpha")
You can also call
directly with character values, or
dynamicFunction1 <- `::`('scales', 'alpha') dynamicFunction2 <- `::`('psych', 'alpha')
dynamicFunction1 <- getExportedValue('scales', 'alpha') dynamicFunction2 <- getExportedValue('psych', 'alpha')