devol
2009-Oct-08 17:14 UTC
[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.
Henrique Dallazuanna
2009-Oct-08 17:18 UTC
[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 <sunduck at gmail.com> 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 at 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
baptiste auguie
2009-Oct-08 17:22 UTC
[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 <sunduck at gmail.com>:> > 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 at 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
2009-Oct-08 17:35 UTC
[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] 1Use 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 at 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
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. Maybe it > is possible to set the var as some kind of global type and then it will be > returned from the function as I want it to see... > > Thanks! >?"<<-" although this should be used sparingly. See section 6 of <http://www.burns-stat.com/pages/Tutor/R_inferno.pdf> ... -- View this message in context: http://www.nabble.com/modifications-of-out-of-the-function-variable-tp25803308p25803347.html Sent from the R help mailing list archive at Nabble.com.