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

时间:2019-04-10 14:23:41

标签: r autocomplete dplyr

我正在寻找在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?

1 个答案:

答案 0 :(得分:0)

将文本放入命令行可能是您用于运行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)

它为您的用户按下了一个额外的键,但是对于用户来说,在命令行上显示大量代码而没有任何提示可能会令人惊讶。

相关问题