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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With