My question is the following. Given the following function:
spam <- function() {
cat("eggs\n")
}
bla <- spam()
I want to find the name of the object the function is returning its content to. In the example bla <- spam() this would be bla. Using match.call I can find the call that was performed:
match.call()
function() {
cat("eggs\n")
}
this, however, does not include the name of the object the function returns to. I’ve looked around on the R-help list, google and SO, but could not find anything useful. Any suggestions how to proceed?
Background
From the replies I gather that what I want is a bit on the esotheric side. I’d explain where I’m coming from, this might shed some light on my motives. The context I’m working in has to do with testing, and more specifically, testing whether or not a certain object changes in time (e.g. model output).
Normally a user will write a bunch of code that generates an object, save it and compare the saved object to newly generated objects as time goes on. The user can do this using the following function:
spam <- testReferenceChange(expression)
Normally this function will read the reference and generate a new object, which the user can compare to each other. For regenerating the reference file, I wanted to use the exact same code. I do this by changing a global option (see options and getOption), recalculate. We running this function in the recalculation mode, new references are stored. And now for the reason to my question. In saving the reference to file (using save()) I wanted to use the name of the object the user returns to, combined with a date, to create a unique name.
…I find it hard to imagine why you would want this. Also, your
spamfunction returnsNULLso it seems even more pointless…Nevertheless, the simplest/cleanest way to do this is to pass in the name to the
spamfunction!Advancing to the next level, you could have
spamdo the assignment. You can also play withsubstituteto allow specifying the name without quotes: