Dear members, I have the following code:> TB <- {x <- 3;y <- 5} > TB[1] 5 It is consistent with the documentation: For {, the result of the last expression evaluated. This has the visibility of the last evaluation. But both x AND y are created, but the "return value" is y. How can this be advantageous for solving practical problems? Specifically, consider the following code: F <- function(X) { expr; expr2; { expr5; expr7}; expr8;expr10} Both expr5 and expr7 are created, and are accessible by the code outside of the nested braces right? But the "return value" of the nested braces is expr7. So doesn't this mean that only expr7 should be accessible? Please help me entangle this (of course the return value of F is expr10, and all the other objects created by the preceding expressions are deleted. But expr5 is not, after the control passes outside of the nested braces!) Thanking you, Yours sincerely, AKSHAY M KULKARNI [[alternative HTML version deleted]]
Hello Akshai, I think you are confusing {...} with local({...}). This one will evaluate the expression in a separate environment, returning the last expression. {...} simply evaluates multiple expressions as one and returns the result of the last line, but it still evaluates each expression. Assignment returns the assigned value, so we can chain assignments like this a <- 1 + (b <- 2) conveniently. So when is {...} useful? Well, anyplace where you want to execute complex stuff in a function argument. E.g. you might do: data %>% group_by(x) %>% summarise(y = {if(x[1] > 10) sum(y) else mean(y)}) Regards, Valentin Petzel 09.01.2023 15:47:53 akshay kulkarni <akshay_e4 at hotmail.com>:> Dear members, > ???????????????????????????? I have the following code: > >> TB <- {x <- 3;y <- 5} >> TB > [1] 5 > > It is consistent with the documentation: For {, the result of the last expression evaluated. This has the visibility of the last evaluation. > > But both x AND y are created, but the "return value" is y. How can this be advantageous for solving practical problems? Specifically, consider the following code: > > F <- function(X) {? expr; expr2; { expr5; expr7}; expr8;expr10} > > Both expr5 and expr7 are created, and are accessible by the code outside of the nested braces right? But the "return value" of the nested braces is expr7. So doesn't this mean that only expr7 should be accessible? Please help me entangle this (of course the return value of F is expr10, and all the other objects created by the preceding expressions are deleted. But expr5 is not, after the control passes outside of the nested braces!) > > Thanking you, > Yours sincerely, > AKSHAY M KULKARNI > > ??? [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
?s 14:47 de 09/01/2023, akshay kulkarni escreveu:> Dear members, > I have the following code: > >> TB <- {x <- 3;y <- 5} >> TB > [1] 5 > > It is consistent with the documentation: For {, the result of the last expression evaluated. This has the visibility of the last evaluation. > > But both x AND y are created, but the "return value" is y. How can this be advantageous for solving practical problems? Specifically, consider the following code: > > F <- function(X) { expr; expr2; { expr5; expr7}; expr8;expr10} > > Both expr5 and expr7 are created, and are accessible by the code outside of the nested braces right? But the "return value" of the nested braces is expr7. So doesn't this mean that only expr7 should be accessible? Please help me entangle this (of course the return value of F is expr10, and all the other objects created by the preceding expressions are deleted. But expr5 is not, after the control passes outside of the nested braces!) > > Thanking you, > Yours sincerely, > AKSHAY M KULKARNI > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.Hello, Everything happens as you have described it, expr5 is accessible outside {}. Whether it is advantageous to solve pratical problems is another thing. The way f() is called creates variable `input` and this can be seen in many places of the R sources. Personally, I find it harder to read and prefer to break that one-liner into two instructions. The code below shows a seldom pratical feature, if ever, put to work. f <- function(X) { x <- X; y <- x*2 u <- { z <- y # expr5 creates a variable by assigning it a value z*pi # expr7's value is assigned to u } v <- u + z # expr5's value is accessible 10 * v } # the call also creates input f(input <- 1) #> [1] 82.83185 .Last.value / 10 #> [1] 0.1 .Last.value - input*2 #> [1] -1 .Last.value / pi #> [1] 0.3183099 .Last.value / 2 #> [1] 0.5 .Last.value == input #> [1] TRUE Hoep this helps, Rui Barradas
Unless you do something special within a function, only the value(s) returned are available to the caller. That is the essence of functional-type programming languages. You need to read up on (function) environments in R . You can search on this. ?function and its links also contain useful information, but it may too terse to be explicable to you. There are of course many available references on the internet. I believe your mental model for how R works is flawed, and you have some homework to do to correct it. I may be wrong, naturally, but you can judge by looking at some tutorials. -- Bert On Mon, Jan 9, 2023 at 6:47 AM akshay kulkarni <akshay_e4 at hotmail.com> wrote:> Dear members, > I have the following code: > > > TB <- {x <- 3;y <- 5} > > TB > [1] 5 > > It is consistent with the documentation: For {, the result of the last > expression evaluated. This has the visibility of the last evaluation. > > But both x AND y are created, but the "return value" is y. How can this be > advantageous for solving practical problems? Specifically, consider the > following code: > > F <- function(X) { expr; expr2; { expr5; expr7}; expr8;expr10} > > Both expr5 and expr7 are created, and are accessible by the code outside > of the nested braces right? But the "return value" of the nested braces is > expr7. So doesn't this mean that only expr7 should be accessible? Please > help me entangle this (of course the return value of F is expr10, and all > the other objects created by the preceding expressions are deleted. But > expr5 is not, after the control passes outside of the nested braces!) > > Thanking you, > Yours sincerely, > AKSHAY M KULKARNI > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Dear Valentin, But why should {....} "return" a value? It could just as well evaluate all the expressions and store the resulting objects in whatever environment the interpreter chooses, and then it would be left to the user to manipulate any object he chooses. Don't you think returning the last, or any value, is redundant? We are living in the 21st century world, and the R-core team might,I suppose, have a definite reason for"returning" the last value. Any comments? Thanking you, Yours sincerely, AKSHAY M KULKARNI ________________________________ From: Valentin Petzel <valentin at petzel.at> Sent: Monday, January 9, 2023 9:18 PM To: akshay kulkarni <akshay_e4 at hotmail.com> Cc: R help Mailing list <r-help at r-project.org> Subject: Re: [R] return value of {....} Hello Akshai, I think you are confusing {...} with local({...}). This one will evaluate the expression in a separate environment, returning the last expression. {...} simply evaluates multiple expressions as one and returns the result of the last line, but it still evaluates each expression. Assignment returns the assigned value, so we can chain assignments like this a <- 1 + (b <- 2) conveniently. So when is {...} useful? Well, anyplace where you want to execute complex stuff in a function argument. E.g. you might do: data %>% group_by(x) %>% summarise(y = {if(x[1] > 10) sum(y) else mean(y)}) Regards, Valentin Petzel 09.01.2023 15:47:53 akshay kulkarni <akshay_e4 at hotmail.com>:> Dear members, > I have the following code: > >> TB <- {x <- 3;y <- 5} >> TB > [1] 5 > > It is consistent with the documentation: For {, the result of the last expression evaluated. This has the visibility of the last evaluation. > > But both x AND y are created, but the "return value" is y. How can this be advantageous for solving practical problems? Specifically, consider the following code: > > F <- function(X) { expr; expr2; { expr5; expr7}; expr8;expr10} > > Both expr5 and expr7 are created, and are accessible by the code outside of the nested braces right? But the "return value" of the nested braces is expr7. So doesn't this mean that only expr7 should be accessible? Please help me entangle this (of course the return value of F is expr10, and all the other objects created by the preceding expressions are deleted. But expr5 is not, after the control passes outside of the nested braces!) > > Thanking you, > Yours sincerely, > AKSHAY M KULKARNI > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.[[alternative HTML version deleted]]
I am more than a little puzzled by your question. In the construct {expr1; expr2; expr3} all of the expressions expr1, expr2, and expr3 are evaluated, in that order. That's what curly braces are FOR. When you want some expressions evaluated in a specific order, that's why and when you use curly braces. If that's not what you want, don't use them. Complaining about it is like complaining that + adds. On Tue, 10 Jan 2023 at 03:47, akshay kulkarni <akshay_e4 at hotmail.com> wrote:> Dear members, > I have the following code: > > > TB <- {x <- 3;y <- 5} > > TB > [1] 5 > > It is consistent with the documentation: For {, the result of the last > expression evaluated. This has the visibility of the last evaluation. > > But both x AND y are created, but the "return value" is y. How can this be > advantageous for solving practical problems? Specifically, consider the > following code: > > F <- function(X) { expr; expr2; { expr5; expr7}; expr8;expr10} > > Both expr5 and expr7 are created, and are accessible by the code outside > of the nested braces right? But the "return value" of the nested braces is > expr7. So doesn't this mean that only expr7 should be accessible? Please > help me entangle this (of course the return value of F is expr10, and all > the other objects created by the preceding expressions are deleted. But > expr5 is not, after the control passes outside of the nested braces!) > > Thanking you, > Yours sincerely, > AKSHAY M KULKARNI > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]