Is it possible to extract the object name when the object is piped to a function?
(rationale: inside the function I would use both the object and the object name)
Just in case you want to see an example, I created a show_summaries() function where I use something similar
# Examples usage show_summaries -------------------------------------------
# mpg %>% show_summaries()
# ## call function w/ providing description for section titles
# mpg %>% show_summaries(description = "mpg tidyverse dataset")
#
# ## for description, determine name of input object
# mpg %>% show_summaries(description = substitute(.))
#
# ## same, but now for a list object
# ability.cov %>% show_summaries(substitute(.))
# ## use `deparse()` to extract the name of a list within a list
# ability.cov$cov %>% show_summaries(description = deparse(substitute(.)))
Notice that in option 1, the dot . in the substitute is basically a shorthand of the object passed in with the pipeline.
In option 2, that won't work, so you have to pass the object name explicitly.
@lars Thank you.
Your recommendation can work, although I don't prefer when formulas are included in the arguments, because I have to remember those "embedded" formulas (deparse(substitute())) and alter them when reusing the function for another object.
Like here: variable_name(mtcars, object_name = deparse(substitute(mtcars)))
My goal is to write a function which can be reused easily. Maybe this is harder to achieve, but later it will compensate when including in the code (and I can learn a lot during this process).
Yes, I fully understand your goal as that is exactly the same for me when I created my own show_summaries() functies - but this implementation was the best I could figure out.
Hopefully, someone else with a more thorough understanding of R and the (calling) environments would know if this can be achieved and what the approach would be then.