Re: [R] external variable by inside-function routines modifications
Thanks all of you for your help!!! 2009/10/8 Bert Gunter : > I am uncertain what you mean. Possibly... > > x <- 1 >> f <- function()x<<-2 >> f() >> x > [1] 2 > > HOWEVER, this is very dangerous and most unwise as it depends on scoping > rules and where f is called to determine exactly which "x" is being assigned > the value of 2. So I think you would do well to reconsider what you are > trying to do and recast it to work within R's recommended semantics, which > is to explicitly assign modified values as the output of functions with the > original values as arguments. > > > Bert Gunter > Genentech Nonclinical Biostatistics > > > > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf Of Alex Bird > Sent: Thursday, October 08, 2009 10:23 AM > To: Henrique Dallazuanna > Cc: r-help@r-project.org > Subject: Re: [R] external variable by inside-function routines modifications > > Is it possible to do what you mentioned somehow outside of the > function. I mean that there's some function and the only thing I can > do is to point on the variable to be modified inside the function > without any possibilities to modify the very function. > > 2009/10/8 Henrique Dallazuanna : >> See assign, you can use '<<-' assignment: >> >> foo <- function(var) var <<- var + 1 >> >> On Thu, Oct 8, 2009 at 2:14 PM, devol wrote: >>> >>> Dear all, >>> >>> could you please advice whether it is possible somehow to modify an >>> external (from the point of some function view) variable by some >>> function-internal operators. For example >>> >>>> var=1 >>>> foo<-function(var){var=var+1} >>>> foo(var) >>>> var >>> [1] 1 >>> >>> but the goal is to get the var equal to 2 in this specific case. >>> >>> Thanks! >>> -- >>> View this message in context: > http://www.nabble.com/external-variable-by-inside-function-routines-modifica > tions-tp25803308p25803308.html >>> Sent from the R help mailing list archive at Nabble.com. >>> >>> __ >>> 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. >>> >> >> >> >> -- >> Henrique Dallazuanna >> Curitiba-Paraná-Brasil >> 25° 25' 40" S 49° 16' 22" O >> > > __ > 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] external variable by inside-function routines modifications
I am uncertain what you mean. Possibly... x <- 1 > f <- function()x<<-2 > f() > x [1] 2 HOWEVER, this is very dangerous and most unwise as it depends on scoping rules and where f is called to determine exactly which "x" is being assigned the value of 2. So I think you would do well to reconsider what you are trying to do and recast it to work within R's recommended semantics, which is to explicitly assign modified values as the output of functions with the original values as arguments. Bert Gunter Genentech Nonclinical Biostatistics -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Alex Bird Sent: Thursday, October 08, 2009 10:23 AM To: Henrique Dallazuanna Cc: r-help@r-project.org Subject: Re: [R] external variable by inside-function routines modifications Is it possible to do what you mentioned somehow outside of the function. I mean that there's some function and the only thing I can do is to point on the variable to be modified inside the function without any possibilities to modify the very function. 2009/10/8 Henrique Dallazuanna : > See assign, you can use '<<-' assignment: > > foo <- function(var) var <<- var + 1 > > On Thu, Oct 8, 2009 at 2:14 PM, devol wrote: >> >> Dear all, >> >> could you please advice whether it is possible somehow to modify an >> external (from the point of some function view) variable by some >> function-internal operators. For example >> >>> var=1 >>> foo<-function(var){var=var+1} >>> foo(var) >>> var >> [1] 1 >> >> but the goal is to get the var equal to 2 in this specific case. >> >> Thanks! >> -- >> View this message in context: http://www.nabble.com/external-variable-by-inside-function-routines-modifica tions-tp25803308p25803308.html >> Sent from the R help mailing list archive at Nabble.com. >> >> __ >> 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. >> > > > > -- > Henrique Dallazuanna > Curitiba-Paraná-Brasil > 25° 25' 40" S 49° 16' 22" O > __ 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] external variable by inside-function routines modifications
On Oct 8, 2009, at 1:14 PM, devol wrote: Dear all, could you please advice whether it is possible somehow to modify an external (from the point of some function view) variable by some function-internal operators. For example var=1 foo<-function(var){var=var+1} foo(var) var [1] 1 Use functions the way they are designed to be used in R ... to return values: > var <- foo(var) > var [1] 2 > but the goal is to get the var equal to 2 in this specific case. Thanks! -- View this message in context: http://www.nabble.com/external-variable-by-inside-function-routines-modifications-tp25803308p25803308.html Sent from the R help mailing list archive at Nabble.com. __ 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] external variable by inside-function routines modifications
Hi, with assign, foo <- function(var){ assign("var", var+1, envir = .GlobalEnv) } var =1 foo(2) var # [1] 3 HTH, baptiste 2009/10/8 devol : > > Dear all, > > could you please advice whether it is possible somehow to modify an > external (from the point of some function view) variable by some > function-internal operators. For example > >> var=1 >> foo<-function(var){var=var+1} >> foo(var) >> var > [1] 1 > > but the goal is to get the var equal to 2 in this specific case. > > Thanks! > -- > View this message in context: > http://www.nabble.com/external-variable-by-inside-function-routines-modifications-tp25803308p25803308.html > Sent from the R help mailing list archive at Nabble.com. > > __ > 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] external variable by inside-function routines modifications
Is it possible to do what you mentioned somehow outside of the function. I mean that there's some function and the only thing I can do is to point on the variable to be modified inside the function without any possibilities to modify the very function. 2009/10/8 Henrique Dallazuanna : > See assign, you can use '<<-' assignment: > > foo <- function(var) var <<- var + 1 > > On Thu, Oct 8, 2009 at 2:14 PM, devol wrote: >> >> Dear all, >> >> could you please advice whether it is possible somehow to modify an >> external (from the point of some function view) variable by some >> function-internal operators. For example >> >>> var=1 >>> foo<-function(var){var=var+1} >>> foo(var) >>> var >> [1] 1 >> >> but the goal is to get the var equal to 2 in this specific case. >> >> Thanks! >> -- >> View this message in context: >> http://www.nabble.com/external-variable-by-inside-function-routines-modifications-tp25803308p25803308.html >> Sent from the R help mailing list archive at Nabble.com. >> >> __ >> 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. >> > > > > -- > Henrique Dallazuanna > Curitiba-Paraná-Brasil > 25° 25' 40" S 49° 16' 22" O > __ 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] external variable by inside-function routines modifications
See assign, you can use '<<-' assignment: foo <- function(var) var <<- var + 1 On Thu, Oct 8, 2009 at 2:14 PM, devol wrote: > > Dear all, > > could you please advice whether it is possible somehow to modify an > external (from the point of some function view) variable by some > function-internal operators. For example > >> var=1 >> foo<-function(var){var=var+1} >> foo(var) >> var > [1] 1 > > but the goal is to get the var equal to 2 in this specific case. > > Thanks! > -- > View this message in context: > http://www.nabble.com/external-variable-by-inside-function-routines-modifications-tp25803308p25803308.html > Sent from the R help mailing list archive at Nabble.com. > > __ > 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. > -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O __ 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] external variable by inside-function routines modifications
Dear all, could you please advice whether it is possible somehow to modify an external (from the point of some function view) variable by some function-internal operators. For example > var=1 > foo<-function(var){var=var+1} > foo(var) > var [1] 1 but the goal is to get the var equal to 2 in this specific case. Thanks! -- View this message in context: http://www.nabble.com/external-variable-by-inside-function-routines-modifications-tp25803308p25803308.html Sent from the R help mailing list archive at Nabble.com. __ 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.