Skip to content

Create a graphical interface for your functions in R and analyze them more dynamically.

Notifications You must be signed in to change notification settings

DIGOARTHUR/GUI-for-R

Repository files navigation

GUI for R ft. Tcl/Tk

bashftreactgit
Create a graphical interface for your functions in R and analyze them more dynamically.

GitHub last commit GitHub

skills About Application


The creation of graphical interfaces is to facilitate user interaction with the resources that programming brings. In the R language, there is a library called tcl/tk responsible for generating widgets.


skills Example Interface

GUI-for-R.mp4

skills About Tcl/Tk

(...) R does not incorporate a statistical graphical user interface (GUI), but it does include tools for building GUIs. Based on the tcltk package (which furnishes an interface to the Tcl/Tk GUI toolkit) (...)

-- FOX, J. The R Commander: A Basic-Statistics Graphical User Interface to R. Journal of Statistical Software, [S. l.], v. 14, n. 9, p. 1–42, 2005. DOI: 10.18637/jss.v014.i09.

Tcl/Tk is a combination of a scripting language and a toolkit for graphical user interfaces. Since version 1.1.0, R has had a tcltk package to access the Tk toolkit, replacing Tcl code with R function calls (Dalgaard, 2001). There are still some design problems in it, but it is quite useful already in its current state.

-- Dalgaard, Peter. "A primer on the R-Tcl/Tk package." R News 1.3 (2001): 27-31.

skills Example Application

Simple GUI application

bashftreactgit
tt <- tktoplevel()
tkwm.title(tt,'Simple GUI application')
tkwm.geometry(tt, "600x400")
lbl <- tklabel(tt, text="Hello, World!")
tkpack(lbl)

Simple GUI application and Button

bashftreactgit
library(tcltk)
tt <- tktoplevel()
tkwm.title(tt,'Simple GUI application and Button')
lbl <- tklabel(tt, text="Hello, World!")
tkpack(lbl)
but <- tkbutton(tt, text="OK")
tkpack(but)

Widget Directions

bashftreactgit
tkdestroy(tt) # get rid of old example
tt <- tktoplevel()
tkwm.title(tt,'Widget Directions')
edge <- c("top","right","bottom","left")
buttons <- lapply(1:4,
                 function(i) tkbutton(tt, text=edge[i]))
for ( i in 1:4 )
 tkpack(buttons[[i]], side=edge[i],
        fill="both")

Grid Managers, Positions

bashftreactgit
t2 <- tktoplevel()
tkwm.title(t2,'Grid Managers, Positions')
heading <- tklabel(t2, text="Registration form")
l.name <- tklabel(t2, text="Name")
l.age <- tklabel(t2, text="Age")
e.name <- tkentry(t2, width=30)
e.age <- tkentry(t2, width=3)
tkgrid(heading, columnspan=2)
tkgrid(l.name, e.name)
tkgrid(l.age, e.age)
tkgrid.configure(e.name, e.age, sticky="w")
tkgrid.configure(l.name, l.age, sticky="e")

Creating menus

bashftreactgit
# Creating menus
tclvar$color<-"blue"
tt <- tktoplevel()
tkpack(mb <- tkmenubutton(tt, text="Color"))
m <- tkmenu(mb)
tkwm.title(tt,'Creating menus')
tkconfigure(mb,menu=m)
for ( i in c("red", "blue", "green"))
  tkadd(m, "radio", label=i, variable="color",
        value=i)

A simple application: Scripting widgets

bashftreactgit
tkscript <- function() {
 wfile <- ""
 tt <- tktoplevel()
 tkwm.title(tt,'A simple application: Scripting
widgets
')
 txt <- tktext(tt, height=10)
 tkpack(txt)
 save <- function() {
   file <- tkcmd("tk_getSaveFile",
                 initialfile=tkcmd("file", "tail", wfile),
                 initialdir=tkcmd("file", "dirname", wfile))
   if (!length(file)) return()
   chn <- tkcmd("open", file, "w")
   tkcmd("puts", chn, tkget(txt,"0.0","end"))
   tkcmd("close", chn)
   wfile <<- file
 }
 load <- function() {
   file <- tkcmd("tk_getOpenFile")
   if (!length(file)) return()
   chn <- tkcmd("open", file, "r")
   tkinsert(txt, "0.0", tkcmd("read", chn))
   tkcmd("close", chn)
   wfile <<- file
 }
 run <- function() {
   code <- tkget(txt,"0.0","end")
   e <- try(parse(text=code))
   if (inherits(e, "try-error")) {
     tkcmd("tk_messageBox",
           message="Syntax error",
           icon="error")
     return()
   }
   cat("Executing from script window:",
       "-----", code, "result:", sep="\n")
   print(eval(e))
 }
 topMenu <- tkmenu(tt)

 tkconfigure(tt, menu=topMenu)
 fileMenu <- tkmenu(topMenu, tearoff=FALSE)
 tkadd(fileMenu, "command", label="Load",
       command=load)
 tkadd(fileMenu, "command", label="Save",
       command=save)
 tkadd(topMenu, "cascade", label="File",
       menu=fileMenu)
 tkadd(topMenu, "command", label="Run",
       command=run)
}

Canvas

bashftreactgit
# Main Window
base <- tktoplevel(padx=10, pady=10)
tkwm.geometry(base, "600x400")
# Change title using tk window manager
tkwm.title(base,'Canvas')
# Main Window Frame
main_frame <- tkframe(base, relief="sunken", borderwidth = 1)

# Canvas
canvas <- tkcanvas(main_frame)

tkpack(main_frame, canvas, fill = "both", expand=TRUE)
#tkpack(main_frame, fill = "both", expand=TRUE)

tkconfigure(canvas, "-background", "#483D8B")

skills Motivation

I built this repository to share with the community, especially those who work with the R Language, the possibility of building Graphical Interfaces. This project midrangeMCP, the result of research at the Federal University of São João del-Rei, where I collaborated, was the beginning of research into a construction language for the application of Statistics functionalities . Through research we found Tcl/Tk. I hope it is of good use. Hugs.

PS: I want to thank my Advisor Ben Dêivide, and my Federal University of São João del-Rei. 💜



skills References

FOX, J. The R Commander: A Basic-Statistics Graphical User Interface to R. Journal of Statistical Software, [S. l.], v. 14, n. 9, p. 1–42, 2005. DOI: 10.18637/jss.v014.i09.

Dalgaard, P. (2001). The R-tcl/tk interface. In Proceedings of DSC (Vol. 1, No. 2).

Dalgaard, P. (2001). A primer on the R-Tcl/Tk package. R News, 1(3), 27-31.

Ben Deivide and Daniel Furtado (NA). midrangeMCP: Multiples Comparisons Procedures Based on Studentized Midrange and Range Distributions.

Ospina G., J. (2015). Uso de la librería TCL/TK en el desarrollo de una interfaz gráfica de usuario (GUI) en R.

About

Create a graphical interface for your functions in R and analyze them more dynamically.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages