R gWidgets drag and drop between two gtables - r

I am writing a GUI with the gwidgets package in R. I am implementing a tool that will allow the user to select a number of variables from one list, and drag them to an empty list for collection. The inspiration is the gui from SPSS, see link:
spss drag and drop
I was planning do this with two gtables, i.e. first create one gtable with the list of variables, and an empty gtable to collect the selected variables. Below is my sample code:
portfolioBuilder <- function(h,...){
## globals
widgets <- list()
varNames <- c("var1","var2","var3","var4" )#with(.GlobalEnv, names(data))
#window
win <- gwindow("Test")
#groups
g <- ggroup(horizontal = FALSE, container = win, expand = TRUE)
gg <- ggroup(horizontal = FALSE, container = win, expand = TRUE)
#graphics container
ggraphics(container = gg)
#paned group
pg <- gpanedgroup(container = g, expand = TRUE)
nb <- gnotebook(container = pg)
## main group
qpg <- ggroup(horizontal = FALSE, container = nb, label = "portfolio")
parg <- ggroup(horizontal = FALSE, container = nb, label = "portfolio args")
## qplot group
tbl <- glayout(container = qpg)
#variable list
tbl[1,1,anchor = c(1,0)] <- "Variables"
tbl[2:10,2] <- (widgets[["table"]] <- gtable(varNames, multiple = TRUE, container = tbl, expand = TRUE))
tbl[3,3, anchor = c(1,0)] <- "y"
tbl[3,4] <- (widgets[["y"]] <- gedit("", container = tbl))
tbl[4,3, anchor = c(1,0)] <- "x"
tbl[4,4] <- (widgets[["x"]] <- gtable(c(""),container = tbl))
## make table visible and set tab
visible(tbl) <- TRUE
svalue(nb) <- 1
##################################end layout#################################
}
However my sample code spits out an error because of the empty gtable widget. Does anyone know how to accomplish this with gwidgets?

You'll want to work on the layout, but the key to this is addDropSource and addDropTarget:
options(guiToolkit="RGtk2")
library(gWidgets)
w <- gwindow(visible=FALSE)
g <- gpanedgroup(cont=w)
tbl <- gtable(names(mtcars), cont=g)
fl <- gframe("variables", horizontal=FALSE, cont=g)
dep <- gedit(initial.msg="Dependent variable", label="Dependent", cont=fl)
ind <- gedit(initial.msg="Independent variable(s)", label="Independent", cont=fl)
addDropSource(tbl, handler=function(h,...) svalue(h$obj))
addDropTarget(dep, handler=function(h,...) svalue(h$obj) <- h$dropdata)
addDropTarget(ind, handler=function(h,...) {
cur <- svalue(h$obj)
new <- ifelse(nchar(cur) > 0, paste(cur, h$dropdata, sep=", "), h$dropdata)
svalue(h$obj) <- new
})
visible(w) <- TRUE
Drag and drop support in gWidgets is really variable. Of the 6 possibilities for this to work in: gWidgetsRGtk2, gWidgets2RGtk2, gWidgetstcltk, gWidgets2tcltk, gWidgetsQt and gWidgets2Qt this code only worked in gWidgetsRGtk2.

Related

imap() - return results in list in R

Most of the included code serves reproducibility,my question is regarding the export of results from an imap() function.
I have written some functions that aggregate and summarize my data, as below. It creates a list, with multiple lists - one list for every gears.
splitCars <- split(mtcars, mtcars$cyl)
summarizeMtcarsYearly <- function(x)
{
#Ngears
v1 <- length(unique(x$gear))
v2 <- paste0(unique(levels(as.factor(x$gear))),collapse = ', ')
#Build data
y <- data.frame(Ngears=v1,gears=v2,stringsAsFactors = F)
return(y)
}
summarizeMtcars <-function(){
splitCars <- split(mtcars, mtcars$cyl)
splitCars <- lapply(splitCars,summarizeMtcarsYearly)
}
splitCars <- summarizeMtcars()
for every gear in the list, i want to create the summary table. I have also written a function for this (below). The details are not important, this is just for reproducibility. The important part of this function is where I export the table to a results folder - last 5 lines.
createSummaryTable <- function(x, y){
tab <- plot_ly(
type = 'table',
header = list(
values = c(paste0("Gears = "), y, ""),
align = c('left', rep('center')),
line = list(width = 1, color = 'black'),
fill = list(color = 'rgb(235, 100, 230)'),
font = list(family = "Arial", size = 14, color = "white")
),
cells = list(
values = rbind(c('number of gears', 'list of gears'),
c(x$Ngears, x$gears)),
align = c('left', rep('center')),
line = list(color = "black", width = 1),
fill = list(color = c('rgb(235, 193, 238)', 'rgba(228, 222, 249, 0.65)')),
font = list(family = "Arial", size = 12, color = c("black"))
))
test_dir <- "/Users/testFolder"
tab <- plotly_json(tab, FALSE)
tabName <- paste0("summaryVariables_gear_TEST", y, ".json" )
write(tab, paste0(test_dir, "/", tabName))
}
I pretend not to know how many gears my data will have i am then using imap() function to apply a createSummaryTable to every element of the list, and exported it directly to a predefined folder:
splitCars <- summarizeMtcars()
imap(splitCars, function(x, y) createSummaryTable(x,y))
which was working exactly the way i wanted to have it. However, now, i need to return all the tables for every single gear inside a list, something like this:
createSummaryTable <- function(x, y){
tab <- ... # this is the same as before
tabname <- paste0("summary_", y)
assign(tabname, tab)
}
analysis.summaryTables <- function(){
# create tables
splitCars <- summarizeMtcars()
imap(splitCars, function(x, y) createSummaryTable(x,y))
# append all tables to one list
tables <- ls(patter = "summary_")
out <- do.call(c,list(tables))
}
however when i run this
summaryTables <- analysis.summaryTables()
summaryTable is just an empty character string.
How can i store all the output from imap() in a single list in R ??
how can i access the elements from the function createSummaryTable environment and append them together in R?
If I understood correctly, you have a function createSummaryTable that creates an object, a table to be specific.
You have a list of named dataframe and you want to map this list into your function to return a list of objects (a list of tables to be specific) where their names will be the same but "summary_" has to appear before.
Therefore:
createSummaryTable <- function(x, y){
# do something here
return(tbl)
}
# map your list
out <- purrr::imap(list_of_named_dataframes, createSummaryTable)
names(out) <- paste("summary", names(out), sep = "_")
and out is what you're looking for.

How to Create gWidgets Form

Thanks in advance for any help you can offer. I am trying to create a GUI that allows the user to choose choose which directory the files are located then input some other information. I then want to be able to calculate information like how many files are in the folder, and some other information that is gained from the files. I need the values that the user inputs to do any of the analysis. However, when I use svalue() I get an error. I also want to summarize the information on a second tab of the notebook. Here's my code so far:
library(gWidgets)
library(gWidgets2)
library(gWidgets2RGtk2)
library(gWidgetsRGtk2)
library(gWidgetstcltk)
library(gWidgets2tcltk)
library(pdftools)
require(gWidgets2RGtk2)
fileChoose <- function(action="print", text = "Select a file...",
type="open", ...) {
gfile(text=text, type=type, ..., action = action, handler =
function(h,...) {
do.call(h$action, list(h$file))
})
}
path <- fileChoose(action="setwd", type="selectdir", text="Select a
directory...")
files <- list.files(path, full.names = TRUE)
files_ex <- file_ext(files)
win <- gwindow("DataMate Project")
grp <- ggroup(horizontal = FALSE, container=win)
nb <- gnotebook(container = grp, expand=TRUE)
group1 <- ggroup(horizontal = FALSE, container=nb, label = "Input")
x <- c("SELECT CUSTOMER", "Eaton", "NG", "Spectris", "Other")
n <- length(files_ex)
lyt <- glayout(cont = frame)
glabel("File Path to Docs", container = group1)
p <- gedit(path, container=group1)
glabel("To your knowledge is this an ITAR quote?", container = group1)
itar <- gradio(c("Non-ITAR","ITAR"), container=group1)
glabel("Who is the customer? (Parent company)", container = group1)
cust <- gcombobox(x, container=group1)
glabel("How many RFQs / Assemblies?", container = group1)
val <- gspinbutton(from=1, to = 100, by =1, value=1,container=group1)
run <- gbutton("Run", container = group1, gmessage(svalue(val))
Maybe this will get you a start:
show_gui <- function(parent, path) {
files <- list.files(path, full.names = TRUE)
files_ex <- tools::file_ext(files)
nb <- gnotebook(container = parent, expand=TRUE)
group1 <- gframe(horizontal = FALSE, container=nb, label = "Input", expand=TRUE)
x <- c("SELECT CUSTOMER", "Eaton", "NG", "Spectris", "Other")
n <- length(files_ex)
glabel("File Path to Docs", container = group1)
p <- gedit(path, container=group1)
glabel("To your knowledge is this an ITAR quote?", container = group1)
itar <- gradio(c("Non-ITAR","ITAR"), container=group1)
glabel("Who is the customer? (Parent company)", container = group1)
cust <- gcombobox(x, container=group1)
glabel("How many RFQs / Assemblies?", container = group1)
val <- gspinbutton(from=1, to = 100, by =1, value=1,container=group1)
run <- gbutton("Run", container = group1, handler = function(h, ...) {
gmessage(svalue(val))
})
}
w <- gwindow("GUI")
parentgroup <- ggroup(cont=w)
g = ggroup(cont=parentgroup, horizontal=FALSE)
glabel("Choose a file to proceed", cont=g)
gfilebrowse(cont=g, type="selectdir", handler=function(h, ...) {
dname = svalue(h$obj)
if (dir.exists(dname)) {
delete(parentgroup, g)
show_gui(parentgroup, dname)
} else {
gmessage("That file does not exists")
}
})
You had several mistakes. I didn't do the rest for you, but in the button handler you would create the widgets in a new tab, and use svalue(nb) <- 2 to switch to that tab.

R: Vector doesn't change after addHandlerClicked is called (gWidgets)

I want to give a user a chance, to edit a vector - to add or to delete an element.
I have a problem, because the vector doesn't change after user's action.
This is my code:
y <- c(1,2,3)
RO_window <- gwindow("Edit vector", visiable=TRUE) #, parent = MainWindow)
group <- ggroup(horizontal = FALSE, container = RO_window)
tempRB <- gradio(y, container =group)
button1 <- gbutton("Delete", border = TRUE, container = group)
addHandlerClicked(button1, handler = function(h,...){
x <- svalue(tempRB, index = TRUE)
y <- y[! y %in% y[x]]
print(y)
return(y)
})
input_text <- gtext("", container = frame2, front.attr=list(style="bold"))
button2 <- gbutton("Confirm", border = TRUE, container = group)
addHandlerClicked(button2, handler = function(h,...){
y <- append(y, svalue(input_text))
print(y)
return(y)
})
When print(y) is called, vector is edited corectly, but when I use it outside the context of this code, it is the same as at the begining.

How to set up a "save pic as pdf" button in GUI

1) I used the package gWidget to make a GUI in R. I have had some problems. I want to add a "save" button in the window, but I don't know how to store the pic already drawn in ggraphics.
library("memoise")
library("gWidgets2RGtk2")
library("RGtk2")
library("digest")
library("gWidgets2")
library("stats")
options(guiToolkit="RGtk2")
d<-0
#the main window to make and some parts of it to make
win <- gwindow("Load curve analysis", visible=TRUE,expand = TRUE)
biggroup <- ggroup(horizontal = FALSE, container=win, expand = TRUE)
topgroup<-ggroup(horizontal = TRUE, container=biggroup,expand = TRUE)
bottomgroup<-ggroup(horizontal = TRUE, container=biggroup, expand = TRUE)
leftgroup<-ggroup(horizontal = FALSE, container=bottomgroup,expand= TRUE)
rightgroup<-ggroup(horizontal = FALSE, container=bottomgroup,expand=TRUE)
add(rightgroup, ggraphics(), expand=TRUE)
#draw a pic
updatePlot <- function(h,...) {
if(d==1){
if(svalue(Analyse1)=="Month duration curve")
plot(1:100,1:100,main="1")
if(svalue(Analyse1)=="Month load curve")
plot(1:100,1:100,main="2")
}
if(d==2){
if(svalue(Analyse2)=="Jahresdauerlinie"){
plot(1:100,1:100,main="3")
}
}
}
#the "save" button to make, this button will bring another window,
#but after setting up the road of the saving place, this smaller window will be closed
Store<-gbutton("Save as pdf",container=topgroup, handler = function(h,...){
win1 <- gwindow("set up road", visible=TRUE,expand = TRUE)
group <- ggroup(horizontal = FALSE, container=win1, expand = TRUE)
tmp <- gframe("Pls type the place you want to save in", container=group)
obj0<-gedit("",cont=tmp,expand = TRUE)
tmp <- gframe("Pls name the new diagram, and end it with .pdf", container=group)
obj1<-gedit("Lastganganalyse.pdf",cont=tmp,expand = TRUE)
#here the function recordPlot will be used,but it doesnt work,the document cant be opened
ok<-gbutton("Ok",container=group, handler = function(h,...){
p<-recordPlot()
# I dont know why this record Plot doesnt work
setwd(svalue(obj0))
pdf(svalue(obj1))
p
dev.off()
dispose(win1)
})
})
#the other parts of the main window
tmp <- gframe("Year(after input a year pls press Enter)", container=leftgroup)
#Jahren <- gradio(c(2012,2013,2014), horizontal=FALSE, cont=tmp, handler=updatePlot)
Jahren<-gedit("2012",cont=tmp, handler=updatePlot)
tmp <- gframe("Month", container=leftgroup)
Monat <- gslider(from=1,to=12,by=1, value=1, cont=tmp, handler=updatePlot)
tmp <- gframe("Analysis' way of a month", container=leftgroup)
Analyse1 <- gcombobox(c(" ","Month duration curve","Month load curve"), cont=tmp, handler=function(h,...){
d<<-1
updatePlot(h,...)
},expand = TRUE)
tmp <- gframe("Analysis' way of a year", container=leftgroup)
Analyse2 <- gcombobox(c(" ","Jahresdauerlinie"),cont=tmp,handler=function(h,...){
d<<-2
updatePlot(h,...)},expand = TRUE)
2) Besides, I don't know how to set up the size of the ggroup. Or how can I control all parts of the window's size to look better. I dont know that kind of function.
3) The line which is drawn in ggraphics is hard to be seen. And how can I change this situation?
Suppose we had the following plot in the graphics:
ggplot(dat = data.frame("x" = 1:100, "y" = rnorm(100)), aes(x = x, y = y)) + geom_point()
Within the handler for the button, you can try the following:
setwd(svalue(obj0))
dev.copy2pdf(file = svalue(obj1))

position of the labels in a widget

In the widget below, is it possible to change the position of the label of the "radio" groups. I would like something like that instead of having "Type" above the items:
Type o Quantitative
o Qualitative
win <- gwindow("Empirical Phase Diagram")
BigDataGroup <- ggroup(cont=win)
DataGroup <- gframe("Data", container = BigDataGroup, horizontal=FALSE)
grp.file <- ggroup(horizontal=FALSE, container = DataGroup)
lbl.file <- glabel("File: ", container = grp.file)
browse.file <- gfilebrowse(container = grp.file)
Input1Group <- gframe("First input variable ", container = DataGroup, horizontal=FALSE)
grp.text.input1 <- ggroup(horizontal=FALSE, container = Input1Group)
lbl.text.input1 <- glabel("Column index", container = grp.text.input1)
insert.text.input1 <- gedit(text="A", container = grp.text.input1)
grp.type.input1 <- ggroup(horizontal=FALSE, container = Input1Group)
#addSpring(grp.type.input1)
lbl.type.input1 <- glabel("Type ", container = grp.type.input1)
insert.type.input1 <- gradio(items=c("Quantitative", "Qualitative"), container = grp.type.input1)
Input2Group <- gframe("Second input variable ", container = DataGroup, horizontal=FALSE)
grp.text.input2 <- ggroup(horizontal=FALSE, container = Input2Group)
lbl.text.input2 <- glabel("Column index", container = grp.text.input2)
insert.text.input2 <- gedit(text="B", container = grp.text.input2)
grp.type.input2 <- ggroup(horizontal=FALSE, container = Input2Group)
lbl.type.input2 <- glabel("Type ", container = grp.type.input2)
insert.type.input2 <- gradio(items=c("Quantitative", "Qualitative"), container = grp.type.input2)
grp.text.output <- ggroup(horizontal=FALSE, container = DataGroup)
lbl.text.output <- glabel("Output variable range ", container = grp.text.output)
insert.text.output <- gedit(initial.msg="type a range e.g. C:AD", container = grp.text.output)
OptionsGroup <- ggroup(horizontal=FALSE, container = BigDataGroup)
grp.colorspace <- ggroup(horizontal=FALSE, container = OptionsGroup)
insert.colorspace <- gradio(items=c("RGB", "LAB", "LUV"))
lbl.colorspace <- gframe("Color space ", container = grp.colorspace)
add(lbl.colorspace, insert.colorspace)
GoGroup <- ggroup(horizontal=FALSE, container = BigDataGroup)
addSpring(GoGroup)
read <- gbutton(text="Go", container = GoGroup,
handler = function(h, ...) {
print(EPD(filename=svalue(browse.file),
input1=svalue(insert.text.input1),
input2=svalue(insert.text.input2),
outputs=svalue(insert.text.output),
color.space=svalue(insert.colorspace))
)
}
)
Two things:
In gWidgets you can use a glayout container to put labels on the left. Something like:
tbl <- glayout(cont=parent_container)
tbl[1,1] <- "Type" ## or glabel("Type ", container = tbl)
tbl[1,2] <- (insert.type.input1 <- gradio(items=c("Quantitative", "Qualitative"), container = tbl))
The latter double assignment gives you access to the radio widget. You can also get this with tbl[1,2]. This works fine, but you need to do some bookkeeping for the row index.
In gWidgets2 (on github only right now) there is also the gformlayout container which makes this easier:
flyt <- gformlayout(cont=parent_container)
insert.type.input1 <- gradio(items=c("Quantitative", "Qualitative"),
horizontal=FALSE,
label = "Type", container = flyt)
As an aside, if you are using gWidgets2RGtk2 you can modify the font of this label, but it is super hacky. E.g.:
get_labels <- function(fl) {
children <- Map(function(x) x$getWidget(), fl$widget$getChildren())
labels <- Filter(function(x) is(x, "GtkLabel"), children)
names(labels) <- sapply(labels, function(x) x$getText())
labels
}
## Then to set the font of a label you can do:
labels <- get_labels(flyt)
flyt$set_rgtk2_font(labels[["Type"]], list(weight="bold"))
And, if you are still using tcltk, this function should work:
set_formlayout_font <- function(fl, value, row) {
l = tcl("grid", "slaves", fl$widget, row=row-1, column=0)
fl$set_font_ttk(value, l)
}
set_formlayout_font(fl, list(color="blue"), 2) ## 2nd row

Resources