Can one create a variable through a function by name
createVariable <- function(name) {
outputVariable = name
name <- NULL
}
after calling
createVariable("myVar")
I would like to have a variable myVar initialized with NULL in my
environment. Is this possible?
Ralf
Create variable by name
7 messages · Ralf B, Peter Langfelder, Jeffrey Spies +2 more
On Wed, Oct 6, 2010 at 9:32 AM, Ralf B <ralf.bierig at gmail.com> wrote:
Can one create a variable through a function by name
createVariable <- function(name) {
? ? ? ?outputVariable = name
? ? ? ?name <- NULL
}
after calling
createVariable("myVar")
I would like to have a variable myVar initialized with NULL in my
environment. Is this possible?
A clean solution is to use the function assign where you can specify the environment more precisely. look at ?assign Peter
An alternative to Peter's solution:
createVariable <- function(name) {assign(name, NULL, envir=.GlobalEnv)}
Jeff.
On Wed, Oct 6, 2010 at 12:32 PM, Ralf B <ralf.bierig at gmail.com> wrote:
Can one create a variable through a function by name
createVariable <- function(name) {
? ? ? ?outputVariable = name
? ? ? ?name <- NULL
}
after calling
createVariable("myVar")
I would like to have a variable myVar initialized with NULL in my
environment. Is this possible?
Ralf
______________________________________________ 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.
On Oct 6, 2010, at 12:32 PM, Ralf B wrote:
Can one create a variable through a function by name
Isn't that what "<-" does?
createVariable <- function(name) {
outputVariable = name
name <- NULL
}
after calling
createVariable("myVar")
?assign # and isn't this covered in R-FAQ?
?"<-"
> "myVar" <- NULL
> myVar
NULL
Doing it a bit differently"
> "<-"("n1", NULL)
> n1
NULL
One could always wrap it in a function call.
> CVN <- function(name) { name <- NULL }
> CVN("test")
> test
NULL
If you needed a function that by default created the variable in the
global environment, then this would work:
> createGVarNULL <- function(name) {
+ assign(name, NULL, envir = .GlobalEnv) }
> createGVarNULL("test")
> test
NULL
An "assign" version of "<<-" I suppose.
"myVar" <- NULL
> myVar
NULL
I would like to have a variable myVar initialized with NULL in my environment. Is this possible? Ralf
David Winsemius, MD West Hartford, CT
On Oct 6, 2010, at 12:58 PM, David Winsemius wrote:
On Oct 6, 2010, at 12:32 PM, Ralf B wrote:
Can one create a variable through a function by name
Isn't that what "<-" does?
createVariable <- function(name) {
outputVariable = name
name <- NULL
}
after calling
createVariable("myVar")
?assign # and isn't this covered in R-FAQ? ?"<-"
Perhaps the fastest method would be to use back ticks:
> cvn <- `<-` # assign "assign" to a new name, "cvn"
> cvn("n2", NULL)
> n2
NULL
I would like to have a variable myVar initialized with NULL in my environment. Is this possible? Ralf
David Winsemius, MD West Hartford, CT
Possible? Yes (as others have shown), advisable? No, see fortune(106), fortune(236), and possibly fortune(181). What is your ultimate goal? Maybe we can help you find a better way.
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Ralf B
> Sent: Wednesday, October 06, 2010 10:32 AM
> To: r-help Mailing List
> Subject: [R] Create variable by name
>
> Can one create a variable through a function by name
>
> createVariable <- function(name) {
> outputVariable = name
> name <- NULL
> }
>
> after calling
>
> createVariable("myVar")
>
> I would like to have a variable myVar initialized with NULL in my
> environment. Is this possible?
>
> Ralf
>
> ______________________________________________
> 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.
Hi all,
I have scripts that have one variable called 'output', the script
creates a data frame and stores this data frame in 'output'. Then
the data frame is written to disk. This is simple when working with a
single script. However, as soon as one script calls other, variables
overwrite each other.
Here a little example that fixes the problem for one particular case
by using variables with different names:
#
# Script A, either called directly or from another script
#
outputA <- NULL
getOutput <- function(){
return(outputA)
}
outputA <- "script A"
#
# Script B, includes and executes script A as part of its own
#
output <- NULL
output <- "Script B"
source("C:/data/poodle/coder/overwritingTest/scriptA.R")
outputA <- getOutput()
print(paste("Output script B:", output))
print(paste("Output script A:", outputA ))
However, I simply want to ensure that script A's output does not interfere with
the the one produced by script B without script B's need to know how A called
its variable. I want script B to easily access the output of script A.
What I would
ideally need ( I think) is an OO approach. I was thinking that I could perhaps
store a generic variable that is generated based on the script name
(i.e. scriptA_output, scriptB_output)
On Wed, Oct 6, 2010 at 1:24 PM, Greg Snow <Greg.Snow at imail.org> wrote:
Possible? Yes (as others have shown), advisable? No, see fortune(106), fortune(236), and possibly fortune(181). What is your ultimate goal? Maybe we can help you find a better way. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of Ralf B
Sent: Wednesday, October 06, 2010 10:32 AM
To: r-help Mailing List
Subject: [R] Create variable by name
Can one create a variable through a function by name
createVariable <- function(name) {
? ? ? outputVariable = name
? ? ? name <- NULL
}
after calling
createVariable("myVar")
I would like to have a variable myVar initialized with NULL in my
environment. Is this possible?
Ralf
______________________________________________ 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.