R提示自动完成用户

Kyl*_*ndt 3 r gwidgets

在R中是否有一种方法可以提示用户(即scanf)获取信息,还允许使用字符串数组作为可能的完成来自动完成该提示?

基本上,寻找类似GNU Readline for R的东西(理想情况下是一个例子).

Ric*_*ton 5

函数名称等的自动完成似乎是运行R的开发环境的一个属性.因此,与eclipse相比,它与Rclipse相比在R GUI中的工作方式略有不同,而与之相比,与RStudio相比.

从那时起,我认为你可能很难以便携的方式获得自动完成工作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)]
})
Run Code Online (Sandbox Code Playgroud)

在该处理程序内,h$obj引用下拉列表小部件,svalue(h$obj)是当前选择的值,h$obj[]是项集.


R GUI(可能还有其他)中的自动完成是基于utils包中的一组函数构建的(请参阅参考资料?rcompgen).挖掘它的源代码可能很有用,但我仍然认为在检索用户输入时,以一种可在开发环境之间移植的方式使其工作变得困难.(我很高兴被证明是错的.)