Using the `get ()` function of R, while qualifying with the packet

I’d like to assign a function to variable, using a string. The get()
function in the base
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 library()
, 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 get()
(or some similar function)? The following code doesn’t work, presumably because get()
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 getExportedValue
, which ::
uses internally


dynamicFunction1 <- `::`('scales', 'alpha')
dynamicFunction2 <- `::`('psych', 'alpha')


dynamicFunction1 <-  getExportedValue('scales', 'alpha')
dynamicFunction2 <-  getExportedValue('psych', 'alpha')
Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » Using the &grave;get &lpar;&rpar;&grave; function of R&comma; while qualifying with the packet

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录