[英]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.