I want to capture the "name" of the object passed to an argument of a function. In base R idioms, I would use deparse(substitute(arg)) to get the name:
What is the rlang equivalent of this base R idiom? What I'm looking for is something that will work with an S3 classed object, and might be provided as model[["gam"]], for example:
I'm happy to do some string processing on these results to get at the m_gamm bit if I want to clean them up at all, but I'm wondering what the rlang equivalent would be, if one exists.
In the second example it might help to use rlang::call_args(rlang::get_expr(arg)).
Now, I'm definitely no expert and just playing around with what I see in Function reference • rlang
Curious to see what you come up with in the end!
where the quosure seems to have captured the expression I want (although it's prepended ^ for some reason that I don't understand). From there I got to
which gets me very close but now I have a prepended ~ (!)
r$> foo(m_gamm$gam)
[1] "~m_gamm$gam"
I could easily get rid of the "~" part of this string, but that seems convoluted compared to deparse(substitute(arg)) which is what I have used for the time being in my package code.
Having got to rlang::expr_text(rlang::enquo(arg)) I noticed that the example there mentions using substitute() in place of rlang::enquo(). So I tried that and the extra decoration that expr_text() was adding (or perhaps rlang::enquo() was what was adding if) is gone:
So, while I don't understand the decoration added to expressions with enquo, I can avoid that and get what I want with substitute() and then conversion to a string with expr_text(). In this instance there doesn't seem to be an advantage to going beyond the base R deparse(substitute(arg)) (as I'm not doing anything further with the captured argument other than using it as character vector in a tibble) but for other uses what enquo does may be necessary/desirable.
Ah! Right. That actually solves an additional problem (that the user needs to pass m_gamm$gam is an implementation detail of how mgcv::gamm() returns the model as two sides of the same coin). It would be neater to capture the model name (m_gamm) than the thing they have to pass (for S3 dispatch to work).