Having written a command-line interface (CLI), we will now write a graphical user interface (GUI). GUIs can be an effective way of managing the complexity of software, but their disadvantage is that they usually cannot be effectively scripted like CLI applications and that they usually cannot be extended or modified as simply or as deeply as code run from a REPL.
That said, if software is intended as a stand-alone tool for performing tasks that will not be repeated and do not require much setup, a GUI can be very useful. So we will write one for the code in image-properties.r
As with the CLI version, we will run this code using RScript. The script can be run from the command line, or an icon for it can be created in the operating system’s applications menu or dock.
#!/usr/bin/env Rscript ## -*- mode: R -*-
The GUI framework that we will use is the cross-platform gWidgets library. I have set it up to use Gtk here, but Qt and Tk versions are available as well. You can find out more about gWidgets at http://cran.r-project.org/web/packages/gWidgets/index.html.
## install.packages("gWidgetsRGtk2", dep = TRUE) require(gWidgets) options("guiToolkit"="RGtk2")
We source properties-plot.r to load the code that we will use to plot the image once we have gathered all the configuration information we need using the GUI
The first part of the GUI that we define is the top level window and layout. The layout of the top level window is a tabbed pane of the kind used by preferences dialogs and web browsers. We use this to organise the large number of configuration options for the code and to present them to the user in easily understood groupings.
Notice the use of “layout” objects as matrices to arrange interface widgets such as buttons within the window and later within each page of the “notebook” tabbed view.
The first tab contains code to create and handle input from user interface elements for selecting the kind of plot, the data file and folder of images to use, and the file to save the plot as if required. It also allows the user to specify which properties from the data file to plot.table
We use functions to allow the user to choose the data file, image folder, and save file. Using the GUI framework's built-in support for file choosing makes this code remarkably compact.setDataFile
Often part of the GUI must be updated, enabled or disabled in response to changes in another part. When the user selects a "Display" plot we need not require the user to select a file to save the plot in, as the plot will be displayed in a window on the screen. The next functions implement this logic.updateSaveFile
The second tab contains fields to allow the user to configure the basic visual properties of the plot, its height, width, and background colour.table
The third tab allows the user to control the plotting of images, labels, points and lines.table
The fourth (and final) tab allows the user to manage how the axes are plotted.table4
Having created the contents of each tab, we set the initial tab that will be shown to the user and display the window on the screen.svalue(nb)
Next we will write code to set the values of the global variables from the GUI, and perform a render. Until then, we can define a do-nothing renderImage function to allow us to run and test the GUI code.renderImage
If we save this code in a file called propgui and make it executable using the shell command:
chmod +x propgui
We can call the script from the command line like this:
We can enter values into the fields of the GUI, choose files, and press buttons (although pressing the Render button will of course have no effect yet).