Reputation: 243
For example,
example.function = function(x){
return (x+1)}
and then I do:
test = example.function(2)
How can I check whether the variable test
is the result of the function example.function
?
Upvotes: 3
Views: 61
Reputation: 102920
Maybe you can try attr<-
+ sys.call
(or match.call
)
example.function = function(x) `attr<-`(x+1,"function call", sys.call()[[1]]))
such that
> test
[1] 3
attr(,"function call")
[1] "example.function"
Upvotes: 3
Reputation: 174641
If you are writing the function, you can use attributes
example.function = function(x) `attr<-`(x + 1, "created by", "example.function")
So that the result is still a completely standard numeric object, but it has an additional attribute:
x <- example.function(2)
x
#> [1] 3
#> attr(,"created by")
#> [1] "example.function"
A somewhat more sophisticated method would be to hide the attribute by using S3 dispatching, and creating a specific test function:
example.function <- function(x) `attr<-`(x + 1, "class", "special")
print.special <- function(x) print(as.numeric(x))
is.special <- function(x) class(x) == "special"
x <- example.function(2)
x
#> [1] 3
is.special(x)
#> [1] TRUE
If you are asking whether you can tell more generally if an object has come from any particular function, then the answer is no.
Upvotes: 3