4

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?

Basically, looking for something like GNU Readline for R (ideally with an example).

Vincent Zoonekynd
  • 31,893
  • 5
  • 69
  • 78
Kyle Brandt
  • 26,938
  • 37
  • 124
  • 165
  • If there is nothing like this , I would also be interested in other ways that user my easily select things from a long list -- I guess printing an "# index : "string" list and having the user select the number would be the most ... `BASIC` ;-) – Kyle Brandt Jan 25 '12 at 13:18
  • Ah, just found `select.list` as part of the `utils` which does what I just described in my above comment. – Kyle Brandt Jan 25 '12 at 13:23
  • Kyle, go ahead and post your solution as an answer, in case others are seeking for this tool. – Carl Witthoft Jan 25 '12 at 14:17
  • @CarlWitthoft: Well it doesn't support auto complete, so I wouldn't really call it a solution to my question. If something isn't posted in a day or so though I will turn it into an answer. – Kyle Brandt Jan 25 '12 at 14:24

2 Answers2

5

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.

From that, I think you may struggle to get autocomplete working in a portable way for scanf/readline without substantial hackery.

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)]
})

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.


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.)

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
0

You can sort of do this with rstudioapi

choices = list(test=\(){cat("test")})
rstudioapi::sendToConsole("choices$",execute = F)

pressing tab now has autocomplete options that run associated functions.

Thomas Luechtefeld
  • 1,316
  • 15
  • 23