Reputation: 586
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!
Upvotes: 7
Views: 508
Reputation: 9656
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.
Upvotes: 2
Reputation: 3930
Just install latest v0.99.660 Rstudio and autocomplete as described in your question should work without an issue.
UPDATE:
Here's example for GRanges class:
library(GenomicRanges)
gr1 <- GRanges(seqnames=Rle(c("ch1", "chMT"), c(2, 4)),ranges=IRanges(16:21, 20),strand=rep(c("+", "-", "*"), 2))
Then you can type :
gr1@
And Rstudio will show the auto-complete pop-up as shown on next picture:
You can continue using @ to go deeper in the class structure and select specific elements.
Upvotes: -1