简体   繁体   English

R 中的自定义自动完成功能可能吗?

[英]Custom autocomplete functions in R possible?

I am looking to create a custom function in R that will allow the user to call the function and then it will produce an auto complete pipeline ready for them to edit, this way they can jump into quickly customizing the standard pipeline instead of copy pasting from an old script or retyping.我正在寻找在 R 中创建一个自定义 function ,这将允许用户调用 function ,然后它将生成一个自动完整的管道,以便他们可以编辑标准旧脚本或重新输入。 How can I go about setting up this sort of autocomplete:我如何 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?

Putting text into the command line will probably be a function of the interface you are using to run R - is it plain R, Rstudio, etc? 将文本放入命令行可能是您用来运行R的接口的函数 - 是普通的R,Rstudio等吗?

One possibility might be to use the clipr package and put the code into the clipboard, then prompt the user to hit their "paste" button to get it on the command line. 一种可能是使用clipr包并将代码放入剪贴板,然后提示用户点击“粘贴”按钮以在命令行上获取它。 For example this function which creates a little code string: 例如,这个函数会创建一个小代码字符串:

> writecode = function(x){
    code = paste0("print(",x,")")
    clipr::write_clip(code)
    message("Code ready to paste")}

Use it like this: 像这样使用它:

> writecode("foo")
Code ready to paste

Then when I hit Ctrl-V to paste, I see this: 然后当我按Ctrl-V进行粘贴时,我看到了这个:

> print(foo)

I can then edit that line. 然后我可以编辑该行。 Any string will do: 任何字符串都可以:

> writecode("bar")
Code ready to paste
[ctrl-V]
> print(bar)

Its one extra key for your user to press, but having a chunk of code appear on the command line with no prompting might be quite surprising for a user. 它是用户按下的一个额外密钥,但是在命令行中出现一大块代码而没有提示可能对用户来说非常令人惊讶。

I spend my day reading the source code for utils auto completion.我每天都在阅读 utils 自动完成的源代码。 The linebuffer only contains the code for... one line, so that can't do fully what your looking for here, but it is quite customizable.行缓冲区仅包含...一行的代码,因此无法完全满足您在此处查找的内容,但它是完全可定制的。 Maybe the rstudio source code for autocompletion has more answers.也许自动补全的 rstudio 源代码有更多的答案。 Here is an example to write and activate your own custom auto completer.这是一个编写和激活您自己的自定义自动完成器的示例。 It is not well suited for packages as any new pacakge could overwrite the settings.它不太适合包,因为任何新的 pacakge 都可能覆盖设置。

Below a下面一个

#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>

例子

NB.注意。 currently rstudio IDE will backtick quote any suggestion which is not generated by them.目前 rstudio IDE 将反引号引用不是由他们生成的任何建议。 I want to raise an issue on that someday.我想在某一天提出一个问题。

Bonus info: A.DollarNames.mys3class-method can be very useful and works with both rstudio and utils out of the box eg奖励信息: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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM