Rserve return type in case of multiple statements
Asked Answered
S

1

1

Hi i dont quite understand the return type of Rserve in case of multiple command. eg.

a<-rnorm(10);a[4];rnorm(3)

it seems Rserve only returns the last evaluated statement,i.e. rnorm(3). Is it possible to get all the three output values with Rserve?

Also i am interested to know how exactly in R can we get the return value of last evaluated expression? Is there a special character in R to get that like in perl?

Simmers answered 2/5, 2012 at 11:22 Comment(0)
B
3

This is normal R behavior, comparable to the behavior you get in a function. For example:

spam = function(x, y) {
  z = x + y
}

Here R returns z, because this was the last operation performed. To get all objects you can use a list:

spam = function(x, y) {
  z = x + y
  list(x,y,z)
}

This should work in your case:

a<-rnorm(10);list(a,a[4],rnorm(3))

In addition, I do not know of a way to extract the last performed expression, but I would not recommend using it anyway. This kind of syntax only makes the flow of the program harder to read, and does not save you any time.

Beers answered 2/5, 2012 at 11:42 Comment(3)
Hmm, I did not know that, maybe something I picked up using python. I deleted it from the answer.Beers
@SimonUrbanek hi i am too lazy to check the source code :) , do you put the commands send to Rserve inside a dummy function?Simmers
No, they get evaluated one by one (the parser returns an expression vector) and the last value is sent - this is really to avoid traps for novice users, it is more efficient to simply wrap the code in { } so you have only one expression. So, no, you can't use return() if that was the question :)Wellworn

© 2022 - 2024 — McMap. All rights reserved.