I am trying to fit several different models using a master function as the workhorse (each function calls this master function with the variable model as a string and the required parameters passed via ...)
It works fine for model = "loess" and model = "glm", but fails for model = "gam". Specifically, do.call(model, args) will fail unless I hardcode the function name with the namespace. But I need model to be a variable. The same problem occurs for formalArgs which I use to get args. E.g (reprex) based on suggestions from similar SO questions:
model <- "mgcv::gam"
formalArgs(mgcv::gam) # Works but function is hard-coded
formalArgs(gam) # Fails
formalArgs(model) # Fails
formalArgs(eval(model)) # Fails
formalArgs(substitute(model)) # Returns NULL with warning
formalArgs(deparse(substitute(model))) # Fails
The error message is Error in get(fun, mode = "function", envir = envir) : object '"mgcv::gam"' of mode 'function' was not found (or in the case of substitute, returns NULL).
If model is changed to "glm" or "loess", then formalArgs(model) and formalArgs(eval(model)) work.
I think this has something to do with these two functions both being from the base stats package while gam is not. But how do I solve it?
The problem is that
"mgcv::gam"is text for an expression, not the name of a function. The function name is"gam".To retrieve the formals, you can use
formalArgs(x)wherexis the name of a function that's available on the search list, or an actual function object.That's why
formalArgs(mgcv::gam)works, because that expression retrieves the function object. Other ways to do it would beCreated on 2023-10-03 with reprex v2.0.2
Edited to add: as the comments to your question said, using
formalArgs("gam")would also work, but only ifmgcvwas on the search list (because you had runlibrary(mgcv)to put it there, or some equivalent).