[TxMt] R bundle: Suggestions for 'Insert Command Template'

Hans-Joerg Bibiko bibiko at eva.mpg.de
Wed Jan 3 16:42:29 UTC 2007


On 30 Dec 2006, at 11:31, Kevin Ballard wrote:

> On Dec 19, 2006, at 4:56 AM, Hans-Joerg Bibiko wrote:
>
>> Nevertheless I have some humble suggestions to improve it:
>>
>> 1)
>> Now this command executes the apropos function within a new fresh  
>> R session. This means that it doesn't know which libraries I'm  
>> currently using and it doesn't know which functions I defined  
>> within my R session. To use 'Insert Command Template' also for  
>> this you should load the function 'getSig.r' in my current session  
>> (renamed in '.getSig' ) and executes this within my session to get  
>> these signatures too.
>> By doing so you would increase the speed of it enormously and you  
>> can insert signatures for user-defined functions and functions  
>> coming from loaded libraries.
>> The disadvantage of it would be that you have such queries in your  
>> current session; ok you don't see them in the TM R console window,  
>> but if you save it workspace you would have these.
>> To avoid this you could think about to write the answer of the  
>> query into a file and load it in TM.
>> Or, if you don't interested in the user-defined functions you  
>> could start a new R session which loads all libraries which are  
>> loaded in my current session.
>> (I don't know whether this would also work for user-defined  
>> fucntions?)
>
> That only works if you have a "current session". I assume you use  
> the hackery that uses R.app to execute stuff? I, for example, don't  
> like that and so I don't use it at all, so I wouldn't have the  
> concept of a "current session".
>

You are right, I use that hackery. But this is purely a matter of  
taste. ;)

> I'd like to let it load libraries which are loaded in your current  
> session, as well as detect function definitions and provide those  
> as well. I just don't know how much work it takes to do that safely  
> (safely meaning you don't want any side effects) and cleanly.
>

To do so would mean that you have to load all libraries of the  
'current session' each time. This could take some time. That's why I  
prefer the way to auto-load '.getSig' if I start a new session.

For libraries I have an approach:

myLibs<-search();
print(paste(sub('.*?:(.*?)','library(\\1)',myLibs[i<-grep 
('.*?:.*?',myLibs)]),collapse=';'));
rm(myLibs)

This would give you:
[1] "library(RGUI);library(methods);library(stats);library 
(graphics);library(grDevices);library(utils);library(datasets);Library 
(maps);library(base)"

You could use it within 'Insert Command Template' if you write this  
command in BASH.

I mean:
- assign a variable LIB to the osascript output which executes this R  
code within your current R session
- put the entire Ruby code in this BASH script and write in line 16  
(of the original insert_command_template.rb)

...
14 wordEsc = word.gsub("\\|'", "\\\\\\0")
15 wordReg = Regexp.escape(word).gsub("\\|'", "\\\\\\0")
16 stdin.puts("$LIB;cat(paste(getSig(if ('#{wordEsc}' %in% (ary <-  
sort(apropos('^#{wordReg}', mode='function')))) '#{wordEsc}' else  
ary), collapse='\\n'))")
17 stdin.close
...


For functions it is a bit difficult.

Here is only a first thought:

- get all objects via 'ls()'
- check which object is a function via 'mode()'
- use 'formals()' to get the signature

>> 2)
>> If you use the approach in 1) then you can do more elaborated  
>> things with it because of the speed.
>> Examples:
>> -If I look for for a keyword it will insert the signature like the  
>> old version. If there is nothing found it will look for functions  
>> which begins with the keyword like the old version.
>> -If I type e.g. 'data.' it lists all methods for 'data.' like  
>> data.class, data.frame etc. but not 'dataentry'
>> -If I type e.g. '.difftime' you get all functions where  
>> '.difftime' is specified like mean.difftime, print.difftime. To  
>> insert the signature you have to select the keyword in beforehand  
>> otherwise it shows a tooltip.
>> -If I have no idea about the complete name of a function you can  
>> type e.g. '.load.'. This would look for '*load*' as regexp.  
>> Furthermore sometimes I cannot remember whether the a function is  
>> called 'shownames' or 'showNames', so you can write '.names.' and  
>> it looks for '*names*|*Names*'.
>> I would use periods for '.foo.' for indicating this because it  
>> will be caught be Ruby's getword function.
>>
>> Of course you can do more things:
>> -You could catch the 'apropos()' function and run it in TM and  
>> print the result as pop down menu
>>
>> and  and  and
>>
>> What do you think about it?
>>
>> To change to code would be relatively easy.
>
> I'm not really sure I understand the second point here.
>

My point only was to improve it in such a way that you can use it as  
a kind of search tool for functions.
Sometimes I don't know where 'difftime' is specified (mean.difftime  
or print.difftime)
or
there is a function showNames() but I don't know whether it is  
written with a capital 'N' or not.


Best,

Hans







More information about the textmate mailing list