R : How to control the right click of mouse in gWidgets - r

In gWidget GUI I have seen a feature on right click on mouse
Copy
Save
How can I use that Save handler so save my shown dataset in table ?
Kindly refer to the image below :
Also in my code I have used graphicspane1 <- ggraphics(cont = group1)
Kindly suggest answers ASAP
Here is a sample code,
library(cairoDevice)
library(gWidgets)
library(gWidgetsRGtk2)
require(RGtk2)
fun <- function(){
tbl <- data.frame(A=c(1,2,3),X=c('A','B','C'))
grid.draw(tableGrob(tbl, gp=gpar(fontsize=8, lwd=1.2)))
}
options(guiToolkit = "RGtk2")
w <-gwindow("GUI")
g <-ggroup(cont=w)
plotbutton4 <- gbutton('Draw Table', cont = g, handler=function(h,...){
fun()})
graphic1 <- ggraphics(cont=g)
Steps : 1. Click on the button "Draw Table"
Step : 2. Right Click and try to save the data.
How can I make a relation with that Save & Copy Button, more focus on SAVE

Related

In R tcltk, how to check whether a widget exists and replace it if it does?

I'm working on a tcltk interface in R that creates a set of drop down combo boxes based on the fields in whichever dataset is loaded by the user. Right now I have a button ("getbox") to press after loading the data that reads the data then creates a combo box with an option for each field. I'd like to be able to re-create the combo box each time I press the button instead of adding a new combo box. Here is an example of what I have:
tt <- tktoplevel()
comboframe <- tkframe(tt)
getbox <- tkbutton(tt, text = "Create Combo Box", command = function() {
fields <- names(mtcars)
cbox <- tkwidget(comboframe, "ComboBox", editable = FALSE, values = fields)
tkgrid(cbox)
})
tkgrid(getbox)
tkgrid(comboframe)
I tried adding an if statement in the getbox command function, but couldn't get it to work.
if (exists(comboframe)) {
tkdestroy(comboframe)
} # then create combobox ...
Any thoughts on how to replace the combo box instead of adding a new one? Thanks!

TclTk object in R

I'm new to R (and programming in general), so apologies if this has been answered elsewhere. I was not able to find an answer via searching, but any help or direction would be great!
I'm trying to make a clickable interface in R, where I can have users click to find a file of choice that proceeds to get automatically analyzed in R.
Here's what I'm having trouble with:
require(tcltk)
getfile <- function() {name <- tclvalue(tkgetOpenFile(
filetypes = "{{CSV Files} {.csv}}"))
if (name == "") return;
datafile <- read.csv(name,header=T)
}
tt <- tktoplevel()
button.widget <- tkbutton(tt, text = "Select CSV File to be analyzed", command = getfile)
tkpack(button.widget)
# The content of the CSV file is placed in the variable 'datafile'
Yet when I try to execute it, and click on a CSV file of interest after the button pops up, nothing happens. By that I mean R gives me the error below when I type in datafile.
Error: object 'datafile' not found
Once again, any help is much appreciated. Thanks!
The top level object has an environment in which you can store things, keeping them local to the GUI. Give your callbacks that object as an argument:
# callback that reads a file and stores the DF in the env of the toplevel:
getfile <- function(tt) {name <- tclvalue(tkgetOpenFile(
filetypes = "{{CSV Files} {.csv}}"))
if (name == "") return;
tt$env$datafile <- read.csv(name,header=T)
}
# callback that does something to the data frame (should check for existence first!)
dosomething <- function(tt){
print(summary(tt$env$datafile))
}
# make a dialog with a load button and a do something button:
tt <- tktoplevel()
button.choose <- tkbutton(tt, text = "Select CSV File to be analyzed", command = function(){getfile(tt)})
tkpack(button.choose)
button.dosomething <- tkbutton(tt, text = "Do something", command = function(){dosomething(tt)})
tkpack(button.dosomething)
That should be fairly robust, although I'm not sure if there's anything already in the environment that you should beware stomping on, in which case create an environment in that environment and use that for your local storage.
If you want to quit the dialog and save things for the user, prompt for a name and use assign to store them in .GlobalEnv on exit.

How do I update a gedit box instantly after using gfile to specify a filepath, using gWidgetsRGtk2, in R

I am trying to create a GUI using gWidgetsRGtk2 for a program that I have written in R. My GUI has a gedit() text box in which the user can type a file path for the input data file to be put into the program. It also has a 'browse' button, which, when clicked, opens up a gfile() box so that they can browse for the file that they want. What I am having trouble with is updating the value in my gedit() box, after the user has selected their file using the 'browse' button. The code below may make this clearer:
dir <- getwd()
sfilepath <- paste0(dir,"/")
win = gwindow("Set Parameters:",width=400,height=550)
nb = gnotebook(cont=win)
tab2 <- glayout(cont=nb, label = "Advanced Settings")
tab1 <- glayout(cont=nb, label = "Basic Settings")
tab1[2,2] <- glabel("BD:",cont=tab1)
tab1[2,4:5] <- gedit(1,cont=tab1)
addhandlerkeystroke(tab1[2,4],handler=function(h,...){BD <<- as.numeric(svalue(h$obj))})
tab1[3,2:5] <- gseparator(cont=tab1)
tab1[4,2:5] <- glabel("File path:",cont=tab1)
tab1[5,2:4] <- gedit(paste0(dir,"/"),cont=tab1)
tab1[5,5] <- gbutton(text="Browse", handler=function(h,...){ gfile("Select a file",type="open", filter = list("text files" = list(patterns = c("*.csv","*.txt")), "R files" =list(patterns = c("*.R","*.Rdata"))), handler = function(h,...){ sfilepath <<- h$file},cont=TRUE)},cont=tab1)
addhandlermousemotion(tab1[5,2],handler=function(h,...){svalue(h$obj) <- sfilepath})
So far I have tried using addhandlermousemotion, as in the code above, so the text in the gedit() box is only updated when you move the mouse over the box itself. However, I would prefer it if the text in the box updated instantly.
I have also tried using addhandleridle(), with an interval of 1 second, so that the text in the box will be automatically updated every 1 second. This worked. However, it made it impossible to type in the box properly, because the text box was being updated with the old 'sfilepath' before it had saved the new 'sfilepath' that was being typed in.
I am a beginner at making at GUIs (I have written a program for work, but it needs to be used by someone else once I leave, so decided last Friday that I should figure out how to make it into a GUI). So any help that anyone can offer would be greatly appreciated.
Here is the pattern you want (passing a handler to gfilebrowse):
w <- gwindow("test")
g <- ggroup(cont=w, horizontal=FALSE)
file_upload <- gfilebrowse(cont=g, handler=function(h,...) {
svalue(e) <- svalue(h$obj)
})
e <- gedit("", cont=g)

Adding a GIF image to a TclTk window

I wish to insert a 'loading' GIF image to my tcltk window but just can't get my head around it. Following is a reproducible example:-
backg <- 'white'
pdlg <- tktoplevel(background=backg)
tcl('wm', 'geometry', pdlg, '500x100+450+350')
tilg <- 'Package installation in progress'
tkwm.title(pdlg, tilg)
fn <- tkfont.create(family = 'helvetica', size = 12)
nwlabel <- " The requisite packages are being installed. This may take several \nminutes... \n"
tllab <- tklabel(pdlg, text = nwlabel, font = fn, pady = 0, background=backg)
clickEv <- tclVar(0)
OK.but <- tkbutton(pdlg, text=' Stop ', command=function() tclvalue(clickEv) <- 1, font=fn, background='grey', pady=0)
tkgrid(tllab, columnspan=1)
tkgrid(OK.but, row=3)
tkbind(pdlg, "<Destroy>", function() tclvalue(done) <- 2)
tkfocus(pdlg)
#This allows me to insert a GIF image but the animation is lost. Also it would be convenient if the output can be obtained using 'tkgrid' instead of 'tkpack'
pdlg2 <- tktoplevel(background='white')
loading <- tclVar()
tcl('image', 'create', 'photo', loading,
file='file path to GIF file')
trial <- tklabel(pdlg2, image=loading)
tkpack(trial)
An example GIF file can be downloaded from here -http://www.dlfpramericalife.com/library/images/final_loading_big.gif
Ideally, the GIF image should be placed above the 'Stop' button but below the text. Many thanks for your help!
In Tcl/Tk, it's quite easy.
set img [image create photo -file nameofthefile.gif]
label .l -image $img
I don't know R, but taking your code as a guide, I imagine something like this, but please check it!
img <- tkimage.create('photo', file='nameofthefile.gif')
imglab <- tklabel(pdlg, image = img)
... then you grid/pack/place it wherever you want. Please note that this don't work with animated gifs and I think animation must be hand-handler, using a timer which updates periodically the image content, but I never did it, nor I know how to do. You may check the Tcl/Tk wiki for more help.

How to create a status line with ttk?

How do you create your status line at the bottom of your window? An inactive entry does not look very nice. What other options are there?
Is possible to integrate a progress bar which is visible only on demand?
(I am using tk and ttk from within R.)
EDIT: Now here is my second version, which works fine for me, but I would like to display the whole status bar on demand only (similar to what the status bar in the Chrome browser does). How can I do that? Calling tklower(f) does not help...
library(tcltk)
library(tcltk2)
tkdestroy(root)
root <- tktoplevel()
status <- tclVar("")
progress <- tclVar("0")
b <- tk2button(root, text="fake doing something!")
tkpack(b, padx=40, pady=10)
o <- tk2checkbutton(root, text="show progress", variable=progress)
tkpack(o, pady=10)
f <- tk2frame(root, relief="sunken")
l <- tk2label(f, textvariable=status)
tkpack(l, side="left", pady=2, padx=5, expand=0, fill="x")
tkpack(f, side="left", expand=1, fill="x", anchor="s")
sg <- ttksizegrip(root)
tkpack(sg, side="left", expand=0, anchor="se")
doit <- function() {
tclvalue(status) <- "working (hard) ..."
tcl("update")
do.pb <- tclvalue(progress)=="1"
if(do.pb) {
pb <- tk2progress(f, length=60, mode="determinate")
tkpack(pb, side="right", expand=0, padx=3, pady=2)
tkconfigure(pb, maximum=100, value=0)
}
for(i in 1:100) {
if(do.pb) {
tkconfigure(pb, value=i)
tcl("update")
}
Sys.sleep(0.03)
}
if(do.pb) tkdestroy(pb)
tclvalue(status) <- "Ready."
}
tkconfigure(b, command=doit)
tclvalue(status) <- "Ready."
I use a ttk::frame widget. In that I'll place one or more ttk::label widgets, and a ttk::sizegrip widget on the far right.
As for the progress bar -- just add it as usual. If you use grid, you can use grid remove to remove it from the statusbar but grid will remember its settings so you can add it right back. Or, you can user lower and raise to remove it from view and bring it back again.
Your question about the sizegrip widget is unclear. What about it doesn't work for you on windows?
By status line do you mean like the status bar you find at the bottom of most browser windows? If so, I usually use a label with its textvariable linked to a variable containing the status string.

Resources