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

Custom autocomplete functions in R possible?

我想在 R 中创建一个自定义函数,允许用户调用该函数,然后它会生成一个自动完成的管道供他们编辑,这样他们就可以快速进入自定义标准管道而不是从旧脚本复制粘贴或重新输入。我该如何设置这种自动完成功能:

#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 包并将代码放入剪贴板,然后提示用户点击 "paste" 按钮以在命令行上获取它。例如这个创建一个小代码字符串的函数:

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

它是一个额外的键供您的用户按下,但是在没有提示的情况下在命令行中出现一大块代码可能会让用户感到非常惊讶。