Reputation: 13309
I've looked here but couldn't solve my problem: Extract the results of a function
Let's say we have this dummy function that returns what a user wrote, how can I extract what is inside this function. I draw inspiration from such functions as say those used in machine learning algorithms. For instance this kind of function:
z<-train(.........)#just an example
From the above I can extract several results e.g, z$finalmodel #an example
and so on. How is this done?
Here is my example function:
dummy_fun<-function(x,y){
y<-deparse(substitute(y))
x<-deparse(substitute(x))
z<-data.frame(X=x,Y=y)
q<-print(paste0("You wrote ",x," and ", y))
}
res<-dummy_fun(Hi,There)
The dummy_fun
contains objects z and q, how can I extract them?
Thanks a lot!
Upvotes: 1
Views: 184
Reputation: 3554
Simpler function could be (without deparse(substitute())
:
dummy_fun<-function(x,y){
z<-data.frame(X=x,Y=y)
q<-paste0("You wrote ",x," and ", y)
return(list(z = z, q = q))
}
which when called with arguments:
> dummy_fun(x = 1, y = 2)
$z
X Y
1 1 2
$q
[1] "You wrote 1 and 2"
Upvotes: 5