function handle in R
1
0
Entering edit mode
@javier-perez-florido-3121
Last seen 6.1 years ago
Thanks Wolfgang and Arunava, What I would like to do is the following: I have the following character vector: functionsUsed=c("myexpresso","mythreestep","myvsnrma","myl.farms","myj ustPlier","mymmgmos","myexp.farms","myrma","mymas5","myq.farms") For each component in this vector, I have a function defined: myexpresso<-function(rawData,<otherarguments>), mythreestep<-function(rawData,<otherarguments>), etc From a file, I read a 'listOfFunctions' character vector of unknown length. This vector contains names like the ones given in functionsUsed vector: "myexpresso", "myvsnrma", etc.... Let's say, listOfFunctions=c("mythreestep","myvsnrma","mythreestep","mymas5","mye xpresso","myexpresso") I would like to call the functions related to this vector: for(i in 1:length(listOfFunctions) { currentFunction=listOfFunctions[i]: eset<-currentFunction(rawData,<otherarguments>) } The problem is that currentFunction is a character string, not a function and I cannot convert this character string to an object of function class. I can proceed with if/then/else blocks, but I think there must be a smart way to do this. Thanks again, Javier Wolfgang Huber escribi?: > Dear Javier > > Functions in R are just variables like everyone else, so you can > simply write: > handleFunction=function1 or > handleFunction=function2 etc. > > Have a look into the book by Robert Gentleman > http://www.bioconductor.org/pub/RBioinf > or the online introduction to R > http://cran.r-project.org/doc/manuals/R-intro.html > esp. chapter 10. This might save you a lot of time. > > Best wishes > Wolfgang > > Javier P?rez Florido wrote: >> Dear list, >> Is there any way to use function handle in R? I would like to do >> something like this (is written in MATLAB) >> >> handleFunction=@function1; >> handleFunction(arguments) # MATLAB knows that handleFunction is >> related to function1 >> >> Thus, handleFunction can be any function, for example: >> handleFunction=@function1 or handleFunction=@function2 or >> handleFunction=@function3, etc >> >> the function call is always the same >> handleFunction(arguments) >> >> Any ideas? >> Thanks, >> Javier >> >> _______________________________________________ >> Bioconductor mailing list >> Bioconductor at stat.math.ethz.ch >> https://stat.ethz.ch/mailman/listinfo/bioconductor >> Search the archives: >> http://news.gmane.org/gmane.science.biology.informatics.conductor >
convert convert • 1.1k views
ADD COMMENT
0
Entering edit mode
@vincent-j-carey-jr-4
Last seen 6 weeks ago
United States
use "get" e.g., if "myexpresso" is the string naming a function then get("myexpresso") evaluates to the function and get("myexpresso")(...) is valid R applying that function to ... do.call("myexpresso", list(...)) is also relevant [note that i have not followed your whole correspondence ... but for the question posed in this email, get() is your solution] 2010/2/6 Javier P?rez Florido <jpflorido at="" gmail.com="">: > Thanks Wolfgang and Arunava, > > What I would like to do is the following: > I have the following character vector: > functionsUsed=c("myexpresso","mythreestep","myvsnrma","myl.farms","m yjustPlier","mymmgmos","myexp.farms","myrma","mymas5","myq.farms") > > For each component in this vector, I have a function defined: > myexpresso<-function(rawData,<otherarguments>), > mythreestep<-function(rawData,<otherarguments>), etc > > From a file, I read a 'listOfFunctions' character vector of unknown length. > This vector contains names like the ones given in functionsUsed vector: > "myexpresso", "myvsnrma", etc.... Let's say, > listOfFunctions=c("mythreestep","myvsnrma","mythreestep","mymas5","m yexpresso","myexpresso") > > I would like to call the functions related to this vector: > > ? for(i in 1:length(listOfFunctions) > ? { > ? ? ? currentFunction=listOfFunctions[i]: > ? ? ? eset<-currentFunction(rawData,<otherarguments>) > ? ?} > > The problem is that currentFunction is a character string, not a function > and I cannot convert this character string to an object of function class. > > I can proceed with if/then/else blocks, but I think there must be a smart > way to do this. > > Thanks again, > Javier > > > > > Wolfgang Huber escribi?: >> >> Dear Javier >> >> Functions in R are just variables like everyone else, so you can simply >> write: >> ?handleFunction=function1 or >> ?handleFunction=function2 etc. >> >> Have a look into the book by Robert Gentleman >> http://www.bioconductor.org/pub/RBioinf >> or the online introduction to R >> http://cran.r-project.org/doc/manuals/R-intro.html >> esp. chapter 10. This might save you a lot of time. >> >> ? ?Best wishes >> ? ?Wolfgang >> >> Javier P?rez Florido wrote: >>> >>> Dear list, >>> Is there any way to use function handle in R? I would like to do >>> something like this (is written in MATLAB) >>> >>> handleFunction=@function1; >>> handleFunction(arguments) # MATLAB knows that handleFunction is related >>> to function1 >>> >>> Thus, handleFunction can be any function, for example: >>> handleFunction=@function1 or handleFunction=@function2 or >>> handleFunction=@function3, etc >>> >>> the function call is always the same >>> handleFunction(arguments) >>> >>> Any ideas? >>> Thanks, >>> Javier >>> >>> _______________________________________________ >>> Bioconductor mailing list >>> Bioconductor at stat.math.ethz.ch >>> https://stat.ethz.ch/mailman/listinfo/bioconductor >>> Search the archives: >>> http://news.gmane.org/gmane.science.biology.informatics.conductor >> > > _______________________________________________ > Bioconductor mailing list > Bioconductor at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: > http://news.gmane.org/gmane.science.biology.informatics.conductor >
ADD COMMENT
0
Entering edit mode
Thanks Vincent, do.call works! All the best, Javier Vincent Carey escribi?: > use "get" > > e.g., if "myexpresso" is the string naming a function then > get("myexpresso") evaluates to the function and > get("myexpresso")(...) is valid R applying that function to ... > > do.call("myexpresso", list(...)) is also relevant > > [note that i have not followed your whole correspondence ... but for > the question posed in this email, get() is your solution] > > 2010/2/6 Javier P?rez Florido <jpflorido at="" gmail.com="">: > >> Thanks Wolfgang and Arunava, >> >> What I would like to do is the following: >> I have the following character vector: >> functionsUsed=c("myexpresso","mythreestep","myvsnrma","myl.farms"," myjustPlier","mymmgmos","myexp.farms","myrma","mymas5","myq.farms") >> >> For each component in this vector, I have a function defined: >> myexpresso<-function(rawData,<otherarguments>), >> mythreestep<-function(rawData,<otherarguments>), etc >> >> From a file, I read a 'listOfFunctions' character vector of unknown length. >> This vector contains names like the ones given in functionsUsed vector: >> "myexpresso", "myvsnrma", etc.... Let's say, >> listOfFunctions=c("mythreestep","myvsnrma","mythreestep","mymas5"," myexpresso","myexpresso") >> >> I would like to call the functions related to this vector: >> >> for(i in 1:length(listOfFunctions) >> { >> currentFunction=listOfFunctions[i]: >> eset<-currentFunction(rawData,<otherarguments>) >> } >> >> The problem is that currentFunction is a character string, not a function >> and I cannot convert this character string to an object of function class. >> >> I can proceed with if/then/else blocks, but I think there must be a smart >> way to do this. >> >> Thanks again, >> Javier >> >> >> >> >> Wolfgang Huber escribi?: >> >>> Dear Javier >>> >>> Functions in R are just variables like everyone else, so you can simply >>> write: >>> handleFunction=function1 or >>> handleFunction=function2 etc. >>> >>> Have a look into the book by Robert Gentleman >>> http://www.bioconductor.org/pub/RBioinf >>> or the online introduction to R >>> http://cran.r-project.org/doc/manuals/R-intro.html >>> esp. chapter 10. This might save you a lot of time. >>> >>> Best wishes >>> Wolfgang >>> >>> Javier P?rez Florido wrote: >>> >>>> Dear list, >>>> Is there any way to use function handle in R? I would like to do >>>> something like this (is written in MATLAB) >>>> >>>> handleFunction=@function1; >>>> handleFunction(arguments) # MATLAB knows that handleFunction is related >>>> to function1 >>>> >>>> Thus, handleFunction can be any function, for example: >>>> handleFunction=@function1 or handleFunction=@function2 or >>>> handleFunction=@function3, etc >>>> >>>> the function call is always the same >>>> handleFunction(arguments) >>>> >>>> Any ideas? >>>> Thanks, >>>> Javier >>>> >>>> _______________________________________________ >>>> Bioconductor mailing list >>>> Bioconductor at stat.math.ethz.ch >>>> https://stat.ethz.ch/mailman/listinfo/bioconductor >>>> Search the archives: >>>> http://news.gmane.org/gmane.science.biology.informatics.conductor >>>> >> _______________________________________________ >> Bioconductor mailing list >> Bioconductor at stat.math.ethz.ch >> https://stat.ethz.ch/mailman/listinfo/bioconductor >> Search the archives: >> http://news.gmane.org/gmane.science.biology.informatics.conductor >> >> > >
ADD REPLY

Login before adding your answer.

Traffic: 1084 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