[R] Assign and cmpfun

Duncan Murdoch murdoch.duncan at gmail.com
Fri Jan 6 20:34:26 CET 2012


On 12-01-06 12:18 PM, Michael Pearmain wrote:
> Hi All,
>
> I've just recently discovered the cmpfun function, and was wanting to to
> create a function to assign this to all the functions i have created, but
> without explicitly naming them.
>
> I've achieved this with:
>
> foo<- function(x) { print(x)}
> bar<- function(x) { print(x + 1)}
>
>> foo<- function(x) { print(x)}
>> foo
> function(x) { print(x)}
>> cmpfun(foo)
> function(x) { print(x)}
> <bytecode: 0x26e3d40>
>>
>
> find.all.functions<- ls.str(mode = 'function')
>   for(i in seq_along(find.all.functions)) {
>      assign(find.all.functions[i], cmpfun(get(find.all.functions[i])))
>    }
>
> But remember told that using assign is generally a bad idea, and ideally i
> want to functionalize this to say something like:
>
> CreateCompiledFunctions<- function() {
>    find.all.functions<- ls.str(mode = 'function')
>    for(i in seq_along(find.all.functions)) {
>      assign(find.all.functions[i], cmpfun(get(find.all.functions[i])))
>    }
> }
>
>
> Does anyone have a better solution?

Put your functions in a package, and install it with the --byte-compile 
option.  This is better in two ways:

1.  You have a package, which is *much* better than having a bunch of 
functions sitting around in an .Rdata file.

2.  You don't recompile functions that are already compiled.

Duncan Murdoch

>
> Thanks in advance
>
> Mike
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.



More information about the R-help mailing list