具有自动完成功能的R提示用户 [英] R Prompt User With Autocomplete
问题描述
R中是否有办法提示用户(即scanf)以获取信息,并允许使用字符串数组作为可能的补全来自动完成该提示?
Is there a way in R to prompt a user (i.e. scanf) for information and also allow auto-completion of that prompt using an array of strings as possible completions?
基本上,正在为R寻找类似GNU Readline的东西(理想情况下是带有示例).
Basically, looking for something like GNU Readline for R (ideally with an example).
推荐答案
函数名称等的自动完成功能似乎是运行R的开发环境的属性.因此,与R GUI相比,R GUI的工作方式略有不同eclipse相比emacs相比RStudio相比.
The autocomplete for function names, etc., seems to be a property of the development environment that is running R. So it works slightly differently in R GUI compared to eclipse compared to emacs compared to RStudio compared to whatever.
从那以后,我认为您可能很难在没有大量黑客攻击的情况下以便携式方式为scanf
/readline
进行自动完成工作.
From that, I think you may struggle to get autocomplete working in a portable way for scanf
/readline
without substantial hackery.
更好的解决方案是创建您自己的GUI,您可以在其中控制行为.这是一个使用gWidgets
的示例,带有一个下拉列表(又称组合框),下拉列表的选择取决于键入的内容.
A better solution would be to create your own GUI, where you control the behaviour. Here's an example using gWidgets
, with a dropdown list (aka combobox) whose choices reduce depending upon what is typed into it.
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[]
是项目集.
Inside that handler, h$obj
refers to the dropdown list widget, svalue(h$obj)
is the currently selected value and h$obj[]
is the set of items.
R GUI(以及可能的其他功能)中的自动完成功能是基于utils
程序包中的一组功能构建的(请参见?rcompgen
).深入研究它的来源可能是有用的,但是我仍然认为,以一种可在开发环境之间移植的方式来检索用户输入时,很难使其正常工作. (不过,我很乐意被证明是错误的.)
The autocompletion in R GUI (and possibly others) is built upon a set of functions in the utils
package (see ?rcompgen
). Digging through the source of that may be useful, but I still think it will be hard to get it working while you are retrieving user input, in a way that is portable between development enivronments. (I'd be happy to be proved wrong though.)
这篇关于具有自动完成功能的R提示用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!