5

I am using the following code for the Share Price Application I have been developing (with plenty of help from people here that is greatly appreciated!). One of the things it should do is allow the user to pick a company to analyse from stored XML Files, I have been using the following code to do this:

df <- xmlToDataFrame(file.choose())

Instead of using file.choose () {as apparently the dialogue box reveals to much of the system structure}, it has been suggested to use a drop down menu, with a list of the companies and a link to the file.

Is such a thing possible in R and is there an easy-ish way of implementating it?

Kara
  • 6,115
  • 16
  • 50
  • 57
Anthony Keane
  • 821
  • 3
  • 12
  • 17
  • There is list.files() which lists all files of directory, e.g.: list.files(getwd()) would limit it to your working directory. I know, it's not interactive yet, but maybe you do something out of it. Maybe with the help of the built in http helpserver. – Matt Bannert Aug 26 '10 at 10:30
  • Check out [this](http://bioinf.wehi.edu.au/~wettenhall/RTclTkExamples/DropDown.html). Also this [related question](http://stackoverflow.com/questions/2540232/how-to-allow-multiple-inputs-from-user-using-r). – gd047 Aug 26 '10 at 10:50

2 Answers2

8

select.list allow you to select from a list. Check also menu.

Examples:

Using menu

companies <- c("AAA","BBB","CCC")
links <- c("c:/file1","c:/secret/file3","c:/file3")

i <- menu(companies, graphics=TRUE, title="Choose company")
df <- xmlToDataFrame(links[i])

Using select.list

companies <- c("AAA","BBB","CCC")
links <- c("c:/file1","c:/secret/file3","c:/file3")

i <- select.list(companies, title="Choose company")
df <- xmlToDataFrame(links[companies==i])

If you want to show name and link on list then use

menu_items <- paste(companies, " (", links, ")", sep="")
i <- select.list(menu_items, title="Choose company")
df <- xmlToDataFrame(links[menu_items==i])
Marek
  • 49,472
  • 15
  • 99
  • 121
  • Ok that works from within R. However, when I run it from the Batch file using Rscript it does not work. Is there additional packages / code I should be using? – Anthony Keane Aug 26 '10 at 11:17
  • `xmlToDataFrame` is in the `RSXML` package. Do you have that installed for the version of R you are calling in batch mode? – Richie Cotton Aug 26 '10 at 11:59
  • @Anthony No. It is expected behaviour. From `?menu`: "It is an error to use `menu` in a non-interactive session.". You could try `tk_select.list` from tcltk package. – Marek Aug 26 '10 at 12:40
  • @Marek How do I make it interactive? Also how do I link the selected company to its file location? – Anthony Keane Aug 26 '10 at 12:49
  • 1
    @Anthony Help to `interactive` said that (on Windows) you could use `--ess` (but you should use Rterm instead of Rscript). I extend my answer, is this what you meant under "link the selected company to its file location"? – Marek Aug 26 '10 at 13:23
  • @Marek The code under select list is what I am looking for. Thanks! Still having trouble with the BATCH file even after the suggestions you made. Rterm opens up R with a warning message that the parameters are ignored and so just starts R. Rscript still just flashes a command box and disappears. – Anthony Keane Aug 26 '10 at 13:59
  • @Anthony There are small differences between Rterm and Rscript, e.g. `RScript ex.R` is `Rterm < ex.R`. You could read more in [introduction](http://cran.r-project.org/doc/manuals/R-intro.html#Scripting-with-R) – Marek Aug 26 '10 at 14:34
  • My batch file is currently as follows: c:\R\bin\Rterm.exe --ess "c:\Users\user\Desktop\shares.r" when I run it I get the following error message: c:\R\bin\Rterm.exe --ess "c:\Users\user\Desktop\shares.r" ARGUMENT 'c:\Users\user\Desktop\shares.r' __IGNORED__ Underneath this in the command window R opens up as normal. – Anthony Keane Aug 26 '10 at 15:09
  • Use `c:\R\bin\Rterm.exe --ess < "c:\Users\user\Desktop\shares.r"` – Marek Aug 26 '10 at 15:34
  • @Marek Ok that is working for the most part, it starts going into some sort of infinite loop and I have to kill the process to get it to stop! – Anthony Keane Aug 26 '10 at 17:11
  • The endless loop, just keeps printing out: [code]Save Workspace Image? [y/n/c]: >Save Workspace Image? [y/n/c]: >[/code] Any way to stop this? – Anthony Keane Aug 26 '10 at 21:23
  • Sorted the endless loop problem, not by changing the BATCH file so Marek's answer is right (Thanks Marek), but by altering the quit function from quit() to quit(save = "no", status = 0, runLast = TRUE) – Anthony Keane Aug 27 '10 at 07:43
3

If you don't want to get into tcltk programming, try the gWidgets packages.

library(gWidgetstcltk) # or library(gWidgetsRGtk2), etc.
drp <- gdroplist(c("AAA", "BBB", "CCC"), container = gwindow())
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360