函数名称等的自动完成似乎是运行 R 的开发环境的一个属性。因此,与 eclipse 相比,emacs 与 RStudio 相比,它在 R GUI 中的工作方式略有不同。
因此,我认为您可能很难让自动完成功能以可移植的方式为 scanf/readline 工作,而无需大量黑客攻击。
更好的解决方案是创建您自己的 GUI,您可以在其中控制行为。这是一个使用 gWidgets 的示例,它带有一个下拉列表(也称为组合框),其选择会根据输入的内容而减少。
library(gWidgetstcltk) # or gWidgetsRGtk2, etc.
#some choices to complete to
choices <- c("football", "barometer", "bazooka")
#sort to make it easier for the user to find one, and
#prepend with a blank string to type in
items <- c("", sort(choices))
#create a gui
win <- gwindow()
drp <- gdroplist(items = items, editable = TRUE, cont = win)
#When the user types something, update the list of available items
#to those that begin with what has been typed.
addHandlerKeystroke(drp, handler = function(h, ...)
{
regex <- paste("^", svalue(h$obj), sep = "")
h$obj[] <- items[grepl(regex, items)]
})
在该处理程序中,h$obj 指的是下拉列表小部件,svalue(h$obj) 是当前选定的值,h$obj[] 是项目集。
R GUI(可能还有其他)中的自动完成功能基于utils 包中的一组函数(请参阅?rcompgen)。挖掘其来源可能很有用,但我仍然认为在检索用户输入时很难让它工作,以一种在开发环境之间可移植的方式。 (不过我很高兴被证明是错误的。)