Re: [R] Basic function output/scope question
Thanks Erik, Schalk, Patrick, and David for you helpful advice. I hope I'll, at some point, I'll become versed enough in R to return the favor. -- Best regards, David Young Marketing and Statistical Consultant Madrid, Spain +34 913 540 381 http://www.linkedin.com/in/europedavidyoung Monday, September 21, 2009, 5:42:35 PM, you wrote: EI> Hello, >> >> testfunc<-function(x) >> { y<-10 >> print(y) >> print(x) >> } >> >> testfunc(4) >> >> The variables x and y are accessible during execution of the function >> "testfunc" but not afterwards. EI> In R, expressions return values. When you define a function, ?function says that, "If the end of a function is reached without calling 'return', the value of the last evaluated expression is EI> returned." EI> So you are correct, 'x' and 'y' are local variables, and by all accounts they should be. If you want their values accessible, simply return them. EI> ## return just y EI> testfunc2 <- function(x) { EI>y <- 10 EI>y EI> } EI> ## return both x and y EI> testfunc2 <- function(x) { EI>y <- 10 EI>list(x, y) EI> } EI> There are ways to make x and y global from within a function, but in general that is not the R way to do things! EI> Hope that helps, EI> Erik Iverson __ R-help@r-project.org mailing list 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.
Re: [R] Basic function output/scope question
I see you've already been told about "<<-". The reason I (and presumably others?) stay away from that construct is that your function then has side effects that a user (even yourself) may not anticipate or want, namely possibly overwriting a previous variable in your global environment. The help for ?<<- says, "The operators '<<-' and '->>' cause a search to made through the environment for an existing definition of the variable being assigned. If such a variable is found (and its binding is not locked) then its value is redefined, otherwise assignment takes place in the global environment." So this is not even guaranteed to assign in the Global Environment!! To follow-up on my previous email, you usually assign the results of your function call to a variable if you want to use them further. For example, ## return both x and y testfunc2 <- function(x) { y <- 10 list(x, y) } my.var <- testfunc2(4) another.function(my.var) > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] > On Behalf Of Erik Iverson > Sent: Monday, September 21, 2009 10:43 AM > To: David Young; r-help@r-project.org > Subject: Re: [R] Basic function output/scope question > > Hello, > > > > > testfunc<-function(x) > > { y<-10 > > print(y) > > print(x) > > } > > > > testfunc(4) > > > > The variables x and y are accessible during execution of the function > > "testfunc" but not afterwards. > > In R, expressions return values. When you define a function, ?function > says that, "If the end of a function is reached without calling 'return', > the value of the last evaluated expression is returned." > > So you are correct, 'x' and 'y' are local variables, and by all accounts > they should be. If you want their values accessible, simply return them. > > ## return just y > testfunc2 <- function(x) { >y <- 10 >y > } > > ## return both x and y > testfunc2 <- function(x) { >y <- 10 >list(x, y) > } > > There are ways to make x and y global from within a function, but in > general that is not the R way to do things! > > Hope that helps, > Erik Iverson > > __ > R-help@r-project.org mailing list > 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. __ R-help@r-project.org mailing list 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.
Re: [R] Basic function output/scope question
On Sep 21, 2009, at 11:29 AM, David Young wrote: Hello Group, I'm trying to learn R and am having a problem getting output from a function I'm trying to write. The problem is clearly one of scope, but I can't find the documentation that tells me how to get around the issue. Here is an example of my problem. testfunc<-function(x) { y<-10 print(y) print(x) } testfunc(4) The print function calls done inside testfunc have side effects of printing to the console. See this additional code that demonstrates that the returned value is actually "x" which is the last evaluated object in the function. You should also look at the "return" function. > z<-testfunc(4) [1] 10 # side-effect but NOT the returned value [1] 4 > z [1] 4 # the returned value ?return -- David. The variables x and y are accessible during execution of the function "testfunc" but not afterwards. I've read through the Introduction to R, and the R language definition on functions, but do not see how to define the output of the function, or change the scope of a function variable using the R equivalent of a %global statement as would be done in SAS. Can someone tell me either where I can look for more information or how to make x and y accessible to operations after the function is run? Thanks in advance for any suggestions. -- Best regards, David Young Marketing and Statistical Consultant Madrid, Spain +34 913 540 381 http://www.linkedin.com/in/europedavidyoung mailto:dyo...@telefonica.net __ R-help@r-project.org mailing list 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. David Winsemius, MD Heritage Laboratories West Hartford, CT __ R-help@r-project.org mailing list 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.
Re: [R] Basic function output/scope question
Hello, > > testfunc<-function(x) > { y<-10 > print(y) > print(x) > } > > testfunc(4) > > The variables x and y are accessible during execution of the function > "testfunc" but not afterwards. In R, expressions return values. When you define a function, ?function says that, "If the end of a function is reached without calling 'return', the value of the last evaluated expression is returned." So you are correct, 'x' and 'y' are local variables, and by all accounts they should be. If you want their values accessible, simply return them. ## return just y testfunc2 <- function(x) { y <- 10 y } ## return both x and y testfunc2 <- function(x) { y <- 10 list(x, y) } There are ways to make x and y global from within a function, but in general that is not the R way to do things! Hope that helps, Erik Iverson __ R-help@r-project.org mailing list 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.
Re: [R] Basic function output/scope question
Don't know SAS, but you can use y<<-10 to make the answer available in the global environement. See ?'<<-' To define the output see ?return Schalk Heunis On Mon, Sep 21, 2009 at 5:29 PM, David Young wrote: > Hello Group, > > I'm trying to learn R and am having a problem getting output from a > function I'm trying to write. The problem is clearly one of scope, > but I can't find the documentation that tells me how to get around the > issue. > > Here is an example of my problem. > > testfunc<-function(x) > { y<-10 > print(y) > print(x) > } > > testfunc(4) > > The variables x and y are accessible during execution of the function > "testfunc" but not afterwards. I've read through the Introduction to > R, and the R language definition on functions, but do not see how to > define the output of the function, or change the scope of a function > variable using the R equivalent of a %global statement as would be > done in SAS. Can someone tell me either where I can look for more > information or how to make x and y accessible to operations after the > function is run? > > Thanks in advance for any suggestions. > > > > -- > Best regards, > > David Young > Marketing and Statistical Consultant > Madrid, Spain > +34 913 540 381 > http://www.linkedin.com/in/europedavidyoung > > mailto:dyo...@telefonica.net > > __ > R-help@r-project.org mailing list > 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. > __ R-help@r-project.org mailing list 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.