Last active
February 18, 2017 05:50
-
-
Save yht/80d92f8a70b2a0e9f38245137ca63e3b to your computer and use it in GitHub Desktop.
Sample Apllication on Journal of Statistical Software Issue 8, Volume 37, December 2010. RGtk2: A Graphical User Interface Toolkit for R.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(RGtk2) | |
mainWin <- gtkWindowNew(show = FALSE) | |
mainWin["title"] <- "Data Sheet" | |
mainWin$setDefaultSize(600,600) | |
cbOpen <- function(widget, window) { | |
dialog <- gtkFileChooserDialog("Choose file", window, "open", "gtk-cancel", | |
GtkResponseType["cancel"], "gtk-open", | |
GtkResponseType["accept"]) | |
if(dialog$run() == GtkResponseType["accept"]) { | |
df <- read.csv(dialog$getFilename()) | |
loadFile(df, basename(dialog$getFilename())) | |
} | |
dialog$destroy() | |
} | |
cbSave <- function(widget, window) { | |
dialog <- gtkFileChooserDialog("Filename", window, "save", "gtk-cancel", | |
GtkResponseType["cancel"], "gtk-save", | |
GtkResponseType["accept"]) | |
if(dialog$run() == GtkResponseType["accept"]) { | |
df <- read.csv(dialog$getFilename()) | |
loadFile(df, basename(dialog$getFilename())) | |
} | |
dialog$destroy() | |
} | |
cbExit <- function(widget, window) { | |
window$destroy() | |
} | |
actions <- list( | |
list("FileMenu", NULL, "_File"), | |
list("Open", "gtk-open", "_Open File", "<control>O", "Open CSV", cbOpen), | |
list("Save", "gtk-save", "_Save File", "<control>S", "Save CSV", cbSave), | |
list("Exit", "gtk-quit", "E_xit", "<control>X", "Exit", cbExit) | |
) | |
action_group <- gtkActionGroup("spreadsheetActions") | |
action_group$addActions(actions, mainWin) | |
uiManager <- gtkUIManager() | |
uiManager$insertActionGroup(action_group, 0) | |
merge <- uiManager$newMergeId() | |
# File Menu | |
uiManager$addUi(merge.id = merge, path = "/", name = "menubar", | |
action = NULL, type = "menubar", top = FALSE) | |
uiManager$addUi(merge, "/menubar", "file", "FileMenu", "menu", FALSE) | |
uiManager$addUi(merge, "/menubar/file", "open", "Open", "menuitem", FALSE) | |
uiManager$addUi(merge, "/menubar/file", "save", "Save", "menuitem", FALSE) | |
uiManager$addUi(merge, "/menubar/file", NULL, NULL, "separator", FALSE) | |
uiManager$addUi(merge, "/menubar/file", "exit", "Exit", "menuitem", FALSE) | |
# Toobar | |
uiManager$addUi(merge, "/", "toolbar", NULL, "toolbar", FALSE) | |
uiManager$addUi(merge, "/toolbar", "open", "Open", "toolitem", FALSE) | |
uiManager$addUi(merge, "/toolbar", "save", "Save", "toolitem", FALSE) | |
uiManager$addUi(merge, "/toolbar", "exit", "Exit", "toolitem", FALSE) | |
menubar <- uiManager$getWidget("/menubar") | |
toolbar <- uiManager$getWidget("/toolbar") | |
mainWin$addAccelGroup(uiManager$getAccelGroup()) | |
statusbar <- gtkStatusbar() | |
info <- statusbar$getContextId("info") | |
statusbar$push(info, "Ready") | |
create_tree_model <- function(df) { | |
df <- cbind(rownames = rownames(df), df) | |
filter_df <- cbind(filter = TRUE, df) | |
model <- rGtkDataFrame(filter_df) | |
filter_model <- gtkTreeModelFilterNew(model) | |
filter_model$setVisibleColumn(0) | |
sort_model <- gtkTreeModelSort(filter_model) | |
sort_model | |
} | |
create_tree_view <- function(model) { | |
tree_view <- gtkTreeView(model) | |
rdf <- model$getModel()$getModel() | |
sapply(tail(seq_len(ncol(rdf)), -1), function(j) { | |
renderer <- gtkCellRendererText() | |
column <- gtkTreeViewColumn(colnames(rdf)[j], renderer, text = j - 1) | |
column$setSortColumnId(j - 1) | |
column$setCellDataFunc(renderer, | |
function(column, renderer, model, iter) { | |
iter <- model$convertIterToChildIter(iter)$child.iter | |
child <- model$getModel() | |
iter <- child$convertIterToChildIter(iter)$child.iter | |
i <- rdf$getPath(iter)$getIndices()[[1]] + 1 | |
renderer["text"] <- as.character(rdf[i, j]) | |
}) | |
tree_view$appendColumn(column) | |
}) | |
tree_view$setHeadersClickable(TRUE) | |
if(is.null(gtkCheckVersion(2, 10, 0))) tree_view$setGridLines("both") | |
tree_view | |
} | |
create_entry <- function(model) { | |
entry <- gtkEntry() | |
gSignalConnect(entry, "activate", function(entry) { | |
model[, "filter"] <<- eval(parse(text = entry$text), as.dataframe(model)) | |
}) | |
} | |
notebook <- gtkNotebook() | |
notebook$setTabPos("bottom") | |
vbox <- gtkVBox(homogeneous = FALSE, spacing = 0) | |
vbox$packStart(menubar, expand = FALSE, fill = FALSE, padding = 0) | |
vbox$packStart(toolbar, FALSE, FALSE, 0) | |
vbox$packStart(notebook, TRUE, TRUE, 0) | |
vbox$packStart(statusbar, FALSE, FALSE, 0) | |
mainWin$add(vbox) | |
mainWin$show() | |
load_spreadsheet <- function(df, name) { | |
model <- create_tree_model(df) | |
tree_view <- create_tree_view(model) | |
entry <- create_entry(model$getModel()) | |
hbox <- gtkHBox(TRUE, 3) | |
hbox$packStart(gtkLabel("Filter expression:"), FALSE, FALSE, 0) | |
hbox$packStart(entry, FALSE, FALSE, 0) | |
vbox <- gtkVBox(TRUE, 3) | |
scrolled_window <- gtkScrolledWindow() | |
scrolled_window$add(tree_view) | |
vbox$packStart(scrolled_window, TRUE, TRUE, 0) | |
vbox$packStart(hbox, FALSE, FALSE, 0) | |
if(missing(name)) name <- deparse(substitute(df)) | |
notebook$appendPage(vbox, gtkLabel(name)) | |
statusbar$push(info, paste("Dataset", name, "loaded.")) | |
} | |
load_spreadsheet(mtcars) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Journal: https://www.jstatsoft.org/article/view/v037i08/v37i08.pdf
Package Manual: https://cran.r-project.org/web/packages/RGtk2/RGtk2.pdf