Question

Another beginner question. I've been through so many examples online but can't find one that will help me tailor this portion of my own GUI.

As part of the GUI I'm piecing together for my R scripts I need to be able to populate comboboxes from a .csv file chosen by the user with the filebrowse command. I can see how to populate a combobox from a vector dataframe created within the script but I can't figure how to connect the newly imported file with the comboboxes.

#structure the main window
win1 <- gwindow( visible = TRUE)
g1 <- ggroup(container=win1, horizontal=FALSE)
df <- gfilebrowse("Select a csv file", container = g1)
#add two comboboxes, but how do I write this code to relate it to the user selected file?
cb1 <- gcombobox("X", cont = g1)
cb2 <- gcombobox("Y", cont = g1)

When the user selects the .csv file the two comboboxes should be populated with the column headers from df. For a simple relationship, lets just say the user would choose the X column and the Y column.
Next, these chosen column variables (X,Y) should be made available for use in other functions, equations, plots etc, probably actioned by a button. How would I do this? Thanks in advance

Was it helpful?

Solution

Something expanding this pattern should work.

library(gWidgets)

w <- gwindow("Testing", visible=FALSE)
g <- ggroup(cont=w, horizontal=FALSE)
fb <- gfilebrowse("Choose a CSV file", quote=FALSE,
                  filter = list("CSV files"=list(patterns="*.csv")),
                  cont=g)
cb <- gcombobox("", cont=g)

addHandlerChanged(fb, handler=function(h,...) {
  x <- read.csv(svalue(fb))
  cb[] <- colnames(x)
})

visible(w) <- TRUE
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top