How can I create a variable with name indicated by argument during function run?
1
0
Entering edit mode
my1stbox ▴ 40
@my1stbox-6702
Last seen 10.2 years ago
Hi All, How can I create a variable with a name identical to the character value of an argument that I passed to the function ? The following lines doesn?t work. Do I have to text an expression with a combination of paste0(), parse() first, and eventually evaluate it with eval()? f <- function(Argument.to.carry.the.value.that.I.want.to.use.as.name.o f.variable.to.be.created){ as.name(Argument.to.carry.the.value.that.I.want.to.use.as.name.of.var iable.to.be.created)=my.gene.list } fthe.name) Bests, Allen Chiu PS: What I really want to do is to create a global variable during the function run, like: de.limma=function(path_name){ ????? .GlobalEnv$?as indicated by the path_name argument?=de_gene_list } 2014-08-20 [[alternative HTML version deleted]]
• 964 views
ADD COMMENT
0
Entering edit mode
@michael-lawrence-6705
Last seen 7.6 years ago
United States
.GlobalEnv[[path_name]] <- de_gene_list I hope you have a good reason for this side effect. Also, general R questions like these are better suited for e.g. stackoverflow or R-help. On Wed, Aug 20, 2014 at 6:08 AM, my1stbox <my1stbox at="" 163.com=""> wrote: > Hi All, > How can I create a variable with a name identical to the character value > of an argument that I passed to the function ? The following lines doesn?t > work. Do I have to text an expression with a combination of paste0(), > parse() first, and eventually evaluate it with eval()? > > f <- > function(Argument.to.carry.the.value.that.I.want.to.use.as.name.of.v ariable.to.be.created){ > > as.name > (Argument.to.carry.the.value.that.I.want.to.use.as.name.of.variable. to.be.created)=my.gene.list > > } > fthe.name) > > Bests, > Allen Chiu > > PS: What I really want to do is to create a global variable during the > function run, like: > > de.limma=function(path_name){ > ????? > .GlobalEnv$?as indicated by the path_name argument?=de_gene_list > } > > > > 2014-08-20 > [[alternative HTML version deleted]] > > _______________________________________________ > Bioconductor mailing list > Bioconductor at r-project.org > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: > http://news.gmane.org/gmane.science.biology.informatics.conductor [[alternative HTML version deleted]]
ADD COMMENT
0
Entering edit mode
Michael's answer is of course correct, but if the intention is not to create it in the global environment, you typically use assign, like assign("tmp", 1:3) will create a variable called tmp with the value 1:3. assign supports an envir argument, so you can set it to create it in the Global environment. Like Michael, I am wondering why you want to do this, and I hope this is not intended for code that ever has to be used by other people besides yourself. Best, Kasper On Wed, Aug 20, 2014 at 9:16 AM, Michael Lawrence <michafla at="" gene.com=""> wrote: > .GlobalEnv[[path_name]] <- de_gene_list > > I hope you have a good reason for this side effect. > > Also, general R questions like these are better suited for e.g. > stackoverflow or R-help. > > > > On Wed, Aug 20, 2014 at 6:08 AM, my1stbox <my1stbox at="" 163.com=""> wrote: > > > Hi All, > > How can I create a variable with a name identical to the character value > > of an argument that I passed to the function ? The following lines > doesn?t > > work. Do I have to text an expression with a combination of paste0(), > > parse() first, and eventually evaluate it with eval()? > > > > f <- > > > function(Argument.to.carry.the.value.that.I.want.to.use.as.name.of.v ariable.to.be.created){ > > > > as.name > > > (Argument.to.carry.the.value.that.I.want.to.use.as.name.of.variable. to.be.created)=my.gene.list > > > > } > > fthe.name) > > > > Bests, > > Allen Chiu > > > > PS: What I really want to do is to create a global variable during the > > function run, like: > > > > de.limma=function(path_name){ > > ????? > > .GlobalEnv$?as indicated by the path_name argument?=de_gene_list > > } > > > > > > > > 2014-08-20 > > [[alternative HTML version deleted]] > > > > _______________________________________________ > > Bioconductor mailing list > > Bioconductor at r-project.org > > https://stat.ethz.ch/mailman/listinfo/bioconductor > > Search the archives: > > http://news.gmane.org/gmane.science.biology.informatics.conductor > > [[alternative HTML version deleted]] > > _______________________________________________ > Bioconductor mailing list > Bioconductor at r-project.org > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: > http://news.gmane.org/gmane.science.biology.informatics.conductor > [[alternative HTML version deleted]]
ADD REPLY
0
Entering edit mode
An off-list email ... Allen is not really set on the global environment stuff, but just want to control the name of the variable the return value of the function is assigned to. Let us say we have the following name_variable = "my_name" my_function = function() { 3 } Then you do assign(name_variable, my_function()) This will create a variable called my_name which will have a value of 3. In other words, when you want to control the name of the variable which holds the result of the function, you do it all outside the function, not as you were suggesting, which were roughly to have something like my_function = function(name_variable) { 3 MORE CODE } and then have my_function("my_name") create a variable with name "my_name" and value 3. Best, Kasper On Wed, Aug 20, 2014 at 9:33 AM, Kasper Daniel Hansen < kasperdanielhansen at gmail.com> wrote: > Michael's answer is of course correct, but if the intention is not to > create it in the global environment, you typically use assign, like > assign("tmp", 1:3) > will create a variable called tmp with the value 1:3. assign supports an > envir argument, so you can set it to create it in the Global environment. > > Like Michael, I am wondering why you want to do this, and I hope this is > not intended for code that ever has to be used by other people besides > yourself. > > Best, > Kasper > > > On Wed, Aug 20, 2014 at 9:16 AM, Michael Lawrence <michafla at="" gene.com=""> > wrote: > >> .GlobalEnv[[path_name]] <- de_gene_list >> >> I hope you have a good reason for this side effect. >> >> Also, general R questions like these are better suited for e.g. >> stackoverflow or R-help. >> >> >> >> On Wed, Aug 20, 2014 at 6:08 AM, my1stbox <my1stbox at="" 163.com=""> wrote: >> >> > Hi All, >> > How can I create a variable with a name identical to the character value >> > of an argument that I passed to the function ? The following lines >> doesn?t >> > work. Do I have to text an expression with a combination of paste0(), >> > parse() first, and eventually evaluate it with eval()? >> > >> > f <- >> > >> function(Argument.to.carry.the.value.that.I.want.to.use.as.name.of. variable.to.be.created){ >> > >> > as.name >> > >> (Argument.to.carry.the.value.that.I.want.to.use.as.name.of.variable .to.be.created)=my.gene.list >> > >> > } >> > fthe.name) >> > >> > Bests, >> > Allen Chiu >> > >> > PS: What I really want to do is to create a global variable during the >> > function run, like: >> > >> > de.limma=function(path_name){ >> > ????? >> > .GlobalEnv$?as indicated by the path_name argument?=de_gene_list >> > } >> > >> > >> > >> > 2014-08-20 >> > [[alternative HTML version deleted]] >> > >> > _______________________________________________ >> > Bioconductor mailing list >> > Bioconductor at r-project.org >> > https://stat.ethz.ch/mailman/listinfo/bioconductor >> > Search the archives: >> > http://news.gmane.org/gmane.science.biology.informatics.conductor >> >> [[alternative HTML version deleted]] >> >> _______________________________________________ >> Bioconductor mailing list >> Bioconductor at r-project.org >> https://stat.ethz.ch/mailman/listinfo/bioconductor >> Search the archives: >> http://news.gmane.org/gmane.science.biology.informatics.conductor >> > > [[alternative HTML version deleted]]
ADD REPLY

Login before adding your answer.

Traffic: 741 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6