Tcl (Tool Command Language) is a dynamic scripting language that is easily embedded in other applications. Tk is a cross-platform graphical user interface (GUI) toolkit. Both are useable from within R thanks to the tcltk package. The Tk toolkit is a decent one, but not the most feature-rich. However, the big advantage of Tcl/Tk is its wide availability in all platforms supported by R: the package is maintained by the R Core Team, and it is shipped with R itself. To check if Tcl/Tk is available, use the command
The tcltk2 package is also available from CRAN. It offers additional possibilities and more widgets. These recipes show how to use both the tcltk and the tcltk2 packages with R to build a GUI, or to use other potentials of Tcl.
Create a simple Tk window with one widget (an OK button that closes the window).
Use Tk message boxes in R.
Select files with dialog boxes.
Add menus and pop-up menus to a R Tcl/Tk window.
Create a dialog box that freezes R until it is closed (modal).
Create a non-modal dialog box with a title and OK / Cancel buttons.
Create a Tk button that calls a R function when clicked.
Adding and changing text labels with Tk.
Creating and interacting with Tk checkboxes.
Radiobuttons in Tk window for selection in a limited number of items.
Single text line input widgets in Tk.
Create and populate list boxes with or without scrollbars in Tk.
The text widget allows to display and possibly edit textual information.
A drop-down combobox widget for R TclTk.
Use frames to organize widgets in Tk Windows.
Select values in a range with sliders in Tk Windows.
Select colors with a dialog box.
Load an image in Tcl/Tk and display it a label.
Tk table is a spreadsheet-like widgets for Tk.
Display and manipulate a tree with Tk.
Enter dates more easily with specialized Tk widgets.
Organize your widgets with tabs.
Manage larger areas that the window size easily.
Advanced tcltk coding
Specifying the layout.
Change the focus to Tk windows programmatically.
Use different fonts in your Tk windows.
How to bind Tk events to R functions and generating events in R TclTk.
Manage cursors in Tk windows and widgets.
Control errors and the way exceptions are handled in Tcl.
Create a script window to edit and evaluate R code in Tk.
Include R plots in a Tk GUI using the tkrplot R package.
Interact with R plots in a Tk window using the tkrplot R package.
Other sources of R tcltk help/examples
help.start()in R to get HTML help, then click on
Packages, then click on
Read the ActiveTcl help and learn how to convert Tcl options to R arguments, e.g.
becomes in R
background = "white"
Study the demos in the R
Read and participate in R-Help and other R mailing lists.
Search the web for Tcl/Tk examples, and don’t ignore them completely if they use a language other than R, e.g. Perl or Python. A lot can be learned from these examples.