R 中有没有办法提示用户(即 scanf)提供信息,并允许使用字符串数组作为可能的补全自动补全该提示?
基本上,寻找类似 GNU Readline for R 的东西(最好是一个例子)。
最佳答案
函数名称等的自动完成功能似乎是运行 R 的开发环境的一个属性。因此,与 Emacs 相比,R GUI 与 Eclipse 相比,与 RStudio 相比,它在 R GUI 中的工作方式略有不同。
因此,我认为您可能很难让 scanf
以可移植的方式自动完成工作。/readline
没有大量的hackery。
更好的解决方案是创建您自己的 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
)。挖掘它的来源可能很有用,但我仍然认为在检索用户输入时很难让它工作,以一种在开发环境之间可移植的方式。 (虽然我很高兴被证明是错误的。)
关于R 使用自动完成提示用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9003349/