[英]Custom autocomplete functions in R possible?
我正在寻找在 R 中创建一个自定义 function ,这将允许用户调用 function ,然后它将生成一个自动完整的管道,以便他们可以编辑标准旧脚本或重新输入。 我如何 go 关于设置这种自动完成功能:
#pseudo code what I type---
seq.Date(1,2,by = "days") %>%
pblapply(function(x){
read.fst(list.files(as.character(x), as.data.table = T) %>%
group_by(x) %>%
count()
}) %>% rbindlist()
#how can I write a function so that when I call that function, it ouputs an autocomplete
#of the above so that I can go ahead and start just customizing the code? Something like this
my_autocomplete_function = function(x) {
print(
"
seq.Date(as.Date(Sys.Date()),as.Date(Sys.Date()+1),by = 'days') %>%
pbapply::pblapply(function(x){
fst::read.fst(list.files(as.character(x), as.data.table = T)) %>%
#begin filtering and grouping by below custom
group_by()
}) %>% rbindlist()
")
}
#I can just print the function and copy paste the text from the output in my console to my script
my_autocomplete_function()
#but I rather it just autocomplete and appear in the script if possible?
将文本放入命令行可能是您用来运行R的接口的函数 - 是普通的R,Rstudio等吗?
一种可能是使用clipr
包并将代码放入剪贴板,然后提示用户点击“粘贴”按钮以在命令行上获取它。 例如,这个函数会创建一个小代码字符串:
> writecode = function(x){
code = paste0("print(",x,")")
clipr::write_clip(code)
message("Code ready to paste")}
像这样使用它:
> writecode("foo")
Code ready to paste
然后当我按Ctrl-V进行粘贴时,我看到了这个:
> print(foo)
然后我可以编辑该行。 任何字符串都可以:
> writecode("bar")
Code ready to paste
[ctrl-V]
> print(bar)
它是用户按下的一个额外密钥,但是在命令行中出现一大块代码而没有提示可能对用户来说非常令人惊讶。
我每天都在阅读 utils 自动完成的源代码。 行缓冲区仅包含...一行的代码,因此无法完全满足您在此处查找的内容,但它是完全可定制的。 也许自动补全的 rstudio 源代码有更多的答案。 这是一个编写和激活您自己的自定义自动完成器的示例。 它不太适合包,因为任何新的 pacakge 都可能覆盖设置。
下面一个
#load this function into custom.completer setting to activate
rc.options("custom.completer" = function(x) {
#perhaps debug it, it will kill your rsession sometimes
#browser()
###default completion###
##activating custom deactivates anything else
#however you can run utils auto completer also like this
#rstudio auto completation is not entirely the same as utils
f = rc.getOption("custom.completer")
rc.options("custom.completer" = NULL)
#function running base auto complete.
#It will dump suggestion into mutable .CompletionEnv$comps
utils:::.completeToken() #inspect this function to learn more about completion
rc.options("custom.completer" = f)
###your custom part###
##pull this environment holding all input/output needed
.CompletionEnv = utils:::.CompletionEnv
#perhaps read the 'token'. Also 'linebuffer', 'start' & 'end' are also useful
token = .CompletionEnv$token
#generate a new completion or multiple...
your_comps = paste0(token,c("$with_tomato_sauce","$with_apple_sauce"))
#append your suggestions to the vanilla suggestions/completions
.CompletionEnv$comps = c(your_comps,.CompletionEnv$comps)
print(.CompletionEnv$comps)
#no return used
NULL
})
xxx<tab>
注意。 目前 rstudio IDE 将反引号引用不是由他们生成的任何建议。 我想在某一天提出一个问题。
奖励信息:A.DollarNames.mys3class-method 非常有用,可与 rstudio 和开箱即用的工具一起使用,例如
#' @export
.DollarNames.mys3class = function(x, pattern = "") {
#x is the .CompletionEnv
c("apple","tomato")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.