Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Enable autocompletion in custom class

Tags:

autocomplete

r

I have created a new class, and I would like to enable R's autocompletion.

An example could be:

# Define class
setClass("customList",
     representation("list")
)

# Make example
tmp <- new("customList",
           list(
               test='a',
               b=1:3
           )        
)

Which result in:

tmp
# An object of class "customList"
# [[1]]
# [1] 'a'
#
# [[2]]
# [1] 1 2 3

This custom list does have the names and named arguments can be used

names(tmp)
[1] "a" "b"
tmp$test
[1] 'a'

Now I would like to somehow enable the autocompletion, so I can simply type

tmp$t <TAB> 

and get

tmp$test

How does one do that?

In advance - thanks!

like image 374
Kristoffer Vitting-Seerup Avatar asked Aug 11 '15 09:08

Kristoffer Vitting-Seerup


1 Answers

As a general solution there is a generic function in R called .DollarNames which controls the auto-completions you get when pressing <tab> after the dollar sign on your custom class object.

As an example here is a function that would make it so that only the first name of your customList class is autocompleted:

.DollarNames.customList <- function(x, pattern="") {
  grep(pattern, names(x), value=TRUE)[1]
}

tmp$<tab>
tmp$test

Of course you would want to return all the results in a real case scenario. So the [1] at the end should be removed.

like image 182
Karolis Koncevičius Avatar answered Oct 16 '22 02:10

Karolis Koncevičius