views:

99

answers:

3

Hi All,

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?

Regards,

Anthony.

+2  A: 

Check out this.

Also this related question.

gd047
Also http://stackoverflow.com/questions/3482513/multiple-comboboxes-in-r-using-tcltk
nico
+3  A: 

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
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
`xmlToDataFrame` is in the `RSXML` package. Do you have that installed for the version of R you are calling in batch mode?
Richie Cotton
@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
@Marek How do I make it interactive? Also how do I link the selected company to its file location?
Anthony Keane
@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
@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
@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
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
Use `c:\R\bin\Rterm.exe --ess < "c:\Users\user\Desktop\shares.r"`
Marek
@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
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
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
+3  A: 

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