0

I am wondering if there is a way to specify the package, e.g. with :: when making a call to a function. The following example shows that the do.call() only works when the package is loaded, and the function is named directly:

# doesn't work
do.call("pals::brewer.spectral", args = list(n=10))
# Error in `pals::brewer.spectral`(n = 10) : could not find function "pals::brewer.spectral"


# works when package is loaded
library(pals)
do.call("brewer.spectral", args = list(n=10))
# [1] "#9E0142" "#D53E4F" "#F46D43" "#FDAE61" "#FEE08B" "#E6F598" "#ABDDA4" "#66C2A5" "#3288BD" "#5E4FA2"
Marc in the box
  • 11,769
  • 4
  • 47
  • 97
  • 1
    Removing the double quotes around the function seems to work `do.call(pals::brewer.spectral, args = list(n=10))` – benson23 May 10 '23 at 08:14
  • 1
    Of course `"pals::brewer.spectral"` doesn't work. `::` is a function (like all operators) and `do.call` doesn't parse its first parameter. It just searches the search path for the specified function name (if a character string is passed to it). – Roland May 10 '23 at 08:30
  • Thanks @benson23 - any way that I can remove those quotes during the call? I am trying to input a string that I build elsewhere in the script. – Marc in the box May 10 '23 at 08:34
  • 2
    `fun <- "brewer.spectral"; pack <- "pals"; do.call(getFromNamespace(fun, pack), args = list(n=10))` – Roland May 10 '23 at 08:41

0 Answers0