I want to call a function (hamming
) where its name is stored as a string. To do this, I used get
.
# Assign function name to variable
w <- "hamming"
# Load library for 'hamming' function
library(signal)
#>
#> Attaching package: 'signal'
#> The following objects are masked from 'package:stats':
#>
#> filter, poly
# Call function using 'get'
get(w)(10)
#> [1] 0.0800000 0.1876196 0.4601218 0.7700000 0.9722586 0.9722586 0.7700000
#> [8] 0.4601218 0.1876196 0.0800000
Created on 2019-11-19 by the reprex package (v0.3.0)
That works great.
Now, say I want to do exactly the same thing – and achieve the same result – but without calling library()
. To do this, I included the package name along with the function name in the string, as follows:
# Assign function name to variable
w <- "signal::hamming"
# Call function using 'get'
get(w)(10)
#> Error in get(w): object 'signal::hamming' not found
Created on 2019-11-19 by the reprex package (v0.3.0)
This, however, doesn't work. Why? And how should I achieve this result without loading the entire package?