A Graphical User Interface for R in a Rich Client Platform for Ecological Modeling

For many ecological analyses powerful statistical tools are required for a profound analysis of spatial and time based data sets. In order to avoid many common errors of analysis and data acquisition a graphical user interface can help to focus on the task of the analysis and minimize the time to fulfill certain tasks in a programming language like R. In this paper we present a graphical user interface for R embedded in the ecological modeling software Bio7 which is based on an Eclipse rich client platform. We demonstrate that within the Bio7 platform R can not only be effectively combined with Java but also with the powerful components of Eclipse. Additionally we present some custom Bio7 components which interact with R and make use of some useful advanced concepts and libraries of this connection. Our overview on the Bio7 R interface also emphasizes a broad applicability for disciplines beyond ecological modeling.


Introduction
In recent years the R platform (R Development Core Team 2012) became a very popular tool for statistical analysis in ecology and ecological modeling not only because of the fact that R is open source but also because of the huge possibilities this platform offers to edit, analyze and shape data for analysis (Petzoldt 2003;Ellner and Guckenheimer 2006;Petzoldt and Rinke 2007;Bolker 2008;Soetaert and Herman 2008;Stevens 2009). The free availability of advanced statistical concepts and a vigor community supports the exploration of the sometimes overwhelming complexity of biotic and abiotic factors within ecological systems. Ecological models can help to explore and predict the impact of certain biotic and abiotic parameters like temperature changes, human induced nutrient enrichment, competitive interactions or the impact of land use changes on a real system. The creation and verification of such models can be a time consuming and an error-prone task and typically several software tools have to be combined for a complete analysis.
To reduce the time for model creation and analysis for scientists working with environmental data we developed the ecological modeling platform Bio7 based on a rich client platform (Austenfeld 2009). Eclipse is a very powerful Java integrated development environment (IDE) which offers the possibility to create such a rich client interface (McAffer et al. 2010). A rich client platform (RCP) typically consists of a set of components (plugins) which a developer can reuse to create a new application for a complete different task. Building applications on top of Eclipse has the great advantage to profit from the latest and ongoing development in software technology like the modular design of free available plugins which can extend a RCP platform. Also the availability of new graphical user interface (GUI) toolkits like the standard widget toolkit (SWT) are a bonus for the creation of such applications and can even be combined with the default Java GUI toolkits like Swing and JavaFX. SWT itself is the default GUI toolkit for the Eclipse platform (Northover and Wilson 2004;Warner and Harris 2004). For the creation of more advanced graphical user interfaces the rich client concept offers several useful templates which can be combined for an intuitive and flexible custom GUI. Eclipse menus, toolbars, consoles, update components, help facilities and a powerful Eclipse application programming interface (API) can be reused for a custom RCP application. Additionally the application supports the creation of customizable programming language editors and views (customizable GUI containers) which can be statically or dynamically arranged in a so called Perspective. A Perspective is an Eclipse term which describes a configurable superior container for different views and editors and is used in Eclipse to bundle and organize specific development tools for a task. Typically a RCP application embeds several perspectives which can be used to arrange several thematic interfaces for a special purpose e.g., for statistics (see Figure 1).
The application Bio7 uses the RCP perspective layout concept to arrange different tools for the creation and analysis of ecological models. One custom perspective of Bio7 embeds useful components to interact with the statistical software R (see Figure 1) This R interface provides an easy to use and extendable GUI and a link to the other integrated scientific tools and programming languages. Its applicability extends far beyond ecological modelling, where it was primarily developed for.
Since Bio7 is written in the programming language Java, a special interface to R was integrated to communicate with R. For a bidirectional communication the Rserve application is used to set and get values from R and evaluate R code from a R editor or R shell implemented in Bio7 (Urbanek 2012). Rserve provides an interface which can be used by applications to perform computations in R (Urbanek 2003) and can be called directly e.g., from the programming language Java the main programming language of Bio7. The connection to R with Rserve was chosen because of the speed of a server connection and the flexibility to connect the Bio7 application not only to a local R instance but also to a remote computer. So it is possible to transfer a heavy load calculation on a remote specialized device on another Rserve supported operating system. Most of the operations regarding the interaction with R and Rserve are executed by means of a special Eclipse API (Jobs API) which allows to perform the R interactions as background processes in which long running R operations will not block the Bio7 GUI. The progress of such a job is indicated in the status bar of Bio7 or in a special progress view in which these jobs can also be cancelled. Figure 1: The Eclipse IDE uses "Perspectives" to organize and layout several editors and views (customizable GUI containers) for a specific task. This concept can be reused for a RCP application like Bio7. Bio7 contains several customizable perspectives which can be enabled or opened with actions in the Perspective Bar or with the Window menu in the main menu bar of Bio7. The R perspective of Bio7 contains two views. The R shell view (left) displays the current R workspace and offers methods and templates to evaluate, edit, plot and transfer data. The Table view (right) contains a custom table in which data can be imported, saved, edited or transferred to the R workspace. (1) The main menu bar with an embedded R menu. (2) The main toolbar of Bio7 with one action to start the Rserve connection to R (the action to evaluate R scripts appears here if the R editor is active). (3) The Perspective Bar to switch from one perspective to another. Custom perspectives can be saved with an available context menu. (4) Menu and toolbar of the Table view with actions to customize the table, import or export data and transfer data to the R workspace. (5) The Table view area in which several sheets can be opened side by side. (6) The Bio7 R shell console in which R expressions can be evaluated. (7) Several tabs with R actions and thematic R templates. (8) A display for the workspace of R (in the first tab) which offers a context menu with several actions to edit, analyze and transfer data. (9) A console (in the first tab) for the display of values and results of R evaluations.

The graphical user interface for R
Bio7 embeds several GUI components and evolved over the past years since its first release in 2007 to a full featured R GUI interface. The main R components of Bio7 are a R menu in the menu bar of Bio7, a R editor for source files and a R perspective which embeds a spreadsheet and a custom R console. Furthermore R scripts can be collected in a Flowchart editor or extend a specialized scripts menu of Bio7 for execution. In addition some other Java components of Bio7 can interact dynamically with R data. For example a dynamic Java compiler and two Java scripting languages (Groovy, BeanShell) which can use the Rserve API to get and set values from and to the R workspace and visualize data in available Bio7 displays. An overview of selected Bio7 R components is illustrated in Figure 2.

The R menu
The main R menu of Bio7 contains custom methods to start the Rserve connection, load and  save R workspaces, evaluate expressions in the R console and delete workspace data. Two custom actions are available to install and load R packages from the internet (see Figure 3).
It is also possible to start the regular R GUI (Windows) or shell (Linux) with the current workspace as an option. The R preferences action allows the editing of several adjustments of the R editor, Rserve connection (remote, local) and options for the installation of packages. In an available dynamic submenu of the Scripts menu of Bio7 (besides the R menu) custom R scripts can be detected and executed if copied in a special available R scripts folder of Bio7.

The R editor
The R editor in Bio7 comes with functionalities to edit and mark R language expressions and offers several templates (which can be extended in the preferences) for language specific expressions. Since a R plotting device is not directly available with the Rserve application (a disadvantage of using Rserve) plotting commands can be marked in the R editor and will be displayed in a PDF reader or embedded image application from a temporary created plot Figure 4: The active Resource perspective of Bio7 with opened R editor actions in the main toolbar (1), the selected Resource perspective with the Perspective Bar (2), the Navigator view (3) and an opened R source file in the R editor (4). The Navigator view displays several projects which are stored in the default Bio7 workspace (a subfolder in the Bio7 install location). New Bio7 projects can be created in the File menu (New) of Bio7 or with the available context menu of the Navigator view. Scripts and Java files have to be created in a project (e.g., with the context menu). Opened script and Java files can be executed with the editor actions (1) which will be opened with the registered editor in the toolbar of Bio7. Evaluated results or expression errors (as well as Java and Java scripting exceptions) are printed to the default Bio7 console (5).
file. For the creation, storage, import and export of R script files (and other formats) Bio7 offers a typical file creation wizard in the File menu and a file navigator view (reusable Rich Client Components) which shows the Bio7 workspace the default storage location for all files in Bio7. The default perspective to edit e.g., R files in Bio7 is the Resource perspective which contains e.g., the Navigator view, the editor area, a console, a snippets view (e.g., for custom R code snippets) and a property view for files (see Figure 4). The Navigator view displays the Bio7 workspace and offers different methods to export and import files e.g., as a ZIP file.  Table view (1) several action are available to transfer data directly to the R workspace as dataframes or vectors of different datatype. Vice versa data of the R workspace can be transferred to new sheets in the Table view with actions available in the R shell view (2). A custom Scripts menu for Java scripting languages can be extended with custom scripts. The conceptional design of this Perspective separates a view which is extended with Java specific menus and actions (Table view) and a view which embeds R specific menus and actions (R shell view).

The R perspective
The Bio7 R perspective by default embeds a Java SWT spreadsheet component (Table view) and a R shell view side by side (see Figure 1, Figure 5). With the spreadsheet component external files from e.g., Excel, OpenOffice.org can be imported with powerful Java libraries, edited in the spreadsheet and then transferred to the R workspace. Some specialized methods of the available Table menu offer the possibility to easily transfer the sheet data as different types and in selected portions to R. Vice versa data available in the R workspace can be transferred (as matrices or dataframes) to the Table view as a new sheet and e.g., exported to available export formats. Some typical spreadsheet functions helps in the creation of data and several tabs can be opened to edit data from different sources.

The R shell view
The R shell view within the R perspective (see Figure 6) is laid out that it can be opened detached in another perspective of Bio7. The shell itself consists of several tabs and an expressions text field in which R expressions can be evaluated. In addition the text field can also display a history dialog (up-arrow key). The history data can be saved and opened with Figure 6: The Objects tab of the R shell view (1) with the opened context menu which contains several actions to edit and analyze R data. The activated Plot Data tab (2) embeds methods to plot R workspace variables. Selected variables in the Objects tab can be plotted with the appropriate plot actions in the Plot Data tab (2). In the Plot Data tab a textarea is embedded (3) to customize a plot. If the Detour option is selected the available plot commands are sent to the enabled textarea for customization. The R commands of the textarea will be interpreted when the Draw Plot action is invoked. In the enabled textarea a context menu with templates is available, too. the available actions below the text field. In the first tab Objects some actions are available to refresh the displayed R workspace data, delete variables, store a temporary workspace, trigger the R garbage collection, some options for the display of the R data and a help button with some information about the use of the R shell. In addition the first tab is divided into two displays below the afore mentioned actions. In the left display the R workspace data are displayed and can be selected. Several actions are available with a context menu of the display to show and convert workspace data or transfer data from the R workspace to the afore mentioned spreadsheet. The right display of the first tab visualizes results of evaluated expressions and in addition works like a printing console for R evaluation errors. The second tab Plot Data offers actions to plot data in an easy way. Beside the plotting functionality in the R editor this is another possibility in Bio7 to plot R data which by default is not supported with an available display in Rserve (a counterpart of the R plot display is not supported). Some standard plotting actions can be found in this tab and can be configured with some basic attributes. For a customization of the plotting commands a Detour action is available which sends the plotting commands to an intermediate text field for customization (templates are available in the context menu of this text field) and can then be evaluated with another available (Draw Plot) action. In addition to the first two tabs of the R shell several other tabs are available with R expression templates in different categories. A click on the displayed expressions opens some context information for the different templates. General this thematic tabs and their embedded templates should help in the selection of appropriate "receipts" for data analysis and manipulation of data.

Usage example
In the following example a simple use of the Bio7 R functionality is demonstrated. To start an analysis with R in Bio7 the Rserve application has to be started with the Start Rserve action in the Bio7 toolbar to connect Bio7 with an R installation. After the startup we can create a new Java spreadsheet with random data with the availabe script action (see Figure 5) in the menu of the Table view (Scripts->Random). Optionally one can create his own data in the default available sheet. The sheet data can easily transferred to the R workspace with the different actions availabe in the R<-menu. Select the R<-Table action to transfer the data as a dataframe to the R workspace. The name of the dataframe is the name of the sheet in the Table view. Click on the Refresh action in the R shell Objects tab for the display of the current workspace. The dataframe is displayed and if selected an object description is given in the console beside the R data view. With the context menu of the R data view we can summarize the data of the selected dataframe (Summarize). Optionally we can use the expression text field to apply R commands to the data. To plot the selected data switch to the Plot Data tab and press the the Boxplot button. By default the plot is opened with the systems default PDF device but can also be opened in the ImageJ-Canvas view of Bio7.

Customization of the R perspective
All available editors and views of Bio7 can be activated in the available perspectives of the application (with the Window menu in the menu bar of Bio7). Additional views and editor files for example can be opened and positioned in the R perspective. This can be interesting when R is used in combination with another integrated tool in Bio7 like ImageJ for the purpose of advanced image analysis with R (see Figure 7). All this created custom perspectives can be saved in the context menu of the Perspective Bar for a later session.

Using R in the interaction with specialized tools
Additional to the integration of a full featured graphical user interface to R and due to the interaction with R by means of the Java Rserve bridge, Bio7 offers access to several other Java based tools. One of these tools is a Flowchart editor (Figure 8) in which R source files can be arranged and executed (beside Java and different script files) to form a reproducible chain of simulation and analysis. The Flowchart editor was created with the graphical editing framework (GEF). With GEF it is possible to develop advanced graphical editors e.g., in RCP applications. The graphical visualization is done via the Draw2D framework, which is a Figure 7: The default R perspective customized with the ImageJ-Canvas view and the editor area of Bio7. All Bio7 views and the editor area can be added to existing perspectives of Bio7 (Window menu). Active custom perspectives can be stored and restored from the Perspective Bar (or Window menu) in Bio7.
standard 2D drawing framework based on SWT (Moore 2004). With the ability of the Bio7 Flowchart editor to handle nested loops and decisions, a dragged model file (e.g., Java, R script) can be executed and the results systematically be analyzed e.g., with the numerous statistical methods of R. The difference to existing general or specialized workflow environment solutions (with bindings to R) like Knime or Kepler (Ludäscher et al. 2006;Berthold et al. 2006) is the primary purpose of this editor to simplify a sensitivity analysis of ecological models in which the output of different parameterized model runs can be analyzed.

Using R for image analysis
Bio7 also integrates the popular scientific image analysis tool ImageJ (Rasband 2012;Abramoff et al. 2004;Burger and Burge 2008) for the primary purpose to assess geometrical features in digital images (see Figure 9). Several packages are available in R to load and save images and perform image analysis e.g., EBImage (Pau et al. 2010). However R still lacks many advanced image analysis capabilities which ImageJ offers by default with a specialized graphical user interface for image editing. Bio7 is also one of the first applications containing special meth- Figure 8: The Flowchart editor of Bio7 with several R source files to analyze simulation data. With the main Flow actions (1) a flow can be verified and executed. Files of different filetypes can be dragged from the Navigator view (2) to the Flowchart editor (3) and then executed or evaluated in an ordered sequence. In the context menu of the Flowchart editor several actions are available e.g., to visualize the current progress of the flow (Debug Mode). In the Outline view (4) an overview of the current flow is given and can be used to navigate to parts of the flow. ods to effectively transfer image data (as matrices or vectors) from ImageJ directly into R (without producing intermediate files). Vice versa vector or matrix data created in R can be transferred back efficiently to ImageJ for the creation and analysis of images (e.g., float, RGB, and grayscale images). In addition coordinates from single and multiple selections in images as well as specific geometrical particle measurements results of ImageJ can be transferred to R directly to perform advanced morphometrics on selected image shapes or to perform advanced multivariate analysis of image data in R. One typical application of this ImageJ to R feature is the possibility to perform a unsupervised or supervised classification of transferred image data in R and to display the results in ImageJ (e.g., to analyze plant structures or the composition of plant species in a digital image, see Figure 9). The combination of both applications greatly reduces the time for a profound statistical analysis of image data and avoids many mistakes and misconceptions in this process. Figure 9: The Image perspective of Bio7 with the opened ImageJ-Toolbar (1) and the Image-Methods view (2) which both can be opened with the Window menu of the ImageJ-Canvas view (3). The detached Image-Methods view contains several actions to transfer the current image data in R supported data types to the R workspace. Data in the R workspace can easily transferred back to ImageJ as different types of images. The left displayed image in the ImageJ-Canvas view (4) shows the R clustering result of R, G, B image data from an example image available from the internet. With the cluster result several parts of the image can be morphologically or statistically analyzed in R or in ImageJ. In additon the perspective contains a panel (Points view) for spatial 2D simulations (5) and a thumbnail browser (6) to preview and open ImageJ supported image types of a directory.
Example using R for image analysis the right component of the split panel with the available action) and a sufficient number of clusters for the image (e.g., 6 centers). Press OK to confirm the cluster operation. The images are transferred to R and clustered with the clara method of the cluster package of R (for details about this method consult the package documentation). After the clustering process in R the resulted image matrix is automatically transferred back to the ImageJ-Canvas view and can be coloured with the LUT (look up tables) actions in the ImageJ-Toolbar. The different cluster areas can be measured with ImageJ. Another way to measure the area of the clustered pixel data is to use R. The clustered image data can be transferred to R with the Pic<-action in the Image-Methods view. To transfer only parts of the image (selections e.g., to exclude regions) we can use the Pixel action in the Image-Methods view. Before the transfer select a part of the image with one of the different available selection methods in the ImageJ-Toolbar view. Press the Pixel action to open the transfer dialog. Select No to transfer only the selected pixels of the active image. The image (cluster) data can now be summarized and the pixel area can be measured (count of cluster data) in the R shell of Bio7.

Java and R
The use of Java in combination with R by means of an interface like Rserve offers numerous other useful implementations e.g., to visualize data from R in the different Bio7 panels. Because of the speed of this connection it is for example possible to reuse existing advanced specialized libraries of Java to draw R data not only statically but also dynamically on different available Bio7 panels. For instance the embedded Java Compiler of Bio7 can dynamically compile Java OpenGL commands (see Figure 12) to draw R data in 3D (see Figure 11) or to draw georeferenced data on a Java virtual 3D globe (see Figure 10).
The Rserve API is accessible by the dynamic Java compiler and the scripting languages of Bio7.
Example: Interaction of Java and R The following simplified code ( Figure 12) demonstrates an access to R methods and data with Java to produce the dynamic visualization of 3D data of Figure 11.
To use this code a new project has to be created in the Bio7 workspace. Switch to the Resource Perspective (see Figure 4) in the Perspective Bar and then select File->New in the main menubar of Bio7.
In the opened File Wizard dialog select General->Project to create a new project in the Bio7 workspace. Choose an appropriate name for the project and then press the Finish action to create the project. In this project you can now create a Java OpenGL file (a Java template with necessary methods to get the OpenGL drawing context).
Select File->New and select in the File Wizard Bio7->Java OpenGL File to create a new Java file with default defined OpenGL methods. The Listing can be copied over the afore created file. Optionally download the formatted file from https://github.com/Bio7/Bio7 (the file can be downloaded and copied to the afore created project of Bio7 without the necessity to create a new Java OpenGL file).
To execute the file start the Rserve application in the toolbar of Bio7 (see Figure 1) and open the 3d perspective with the Open Perspective action in the Perspective Bar (or with the Window->Open Perspective->Other... action in the main menu of Bio7).  (1) and the WorldWind view (2). The Options WorldWind view contains several expandable panels with custom actions to control the behaviour of the WorldWind 3D globe. In the WorldWind view georeferenced data (Meuse data) from R is displayed and plotted with accurate coordinates on the embedded virtual 3D globe. The meuse data set used here as an example is a data set comprising of four heavy metals measured in the top soil in a food plain along the river Meuse (Netherlands;. Example adapted from Pebesma (2004).
Extend the Time item in the Options Space view (left view) and press the Start button for the visualization (see Figure 11).
In a very similar way scripts can be created with the availabe scripting languages to evaluate R expressions or send data from Java to R (or vice versa).

Outlook
The graphical user interface for R in an ecological modeling platform represents an ongoing effort to simplify the use of the powerful statistical package R. Initially developed for ecological model verification and analysis, it evolved over the past years to a full featured R GUI useful for a broad scientific context. The R interface of Bio7 is not only well suited for experienced R users who want to use the advanced concepts of the Java -R interaction but also for unexperienced users who can profit from the advanced R usability.
The basis for the graphical user interface is the highly modular and extensible Eclipse rich client platform which offers plugins and tools for the creation of advanced interfaces. In addition applications built upon this platform profit from the ongoing improvements of new plugins and interfaces which often reflect the latest development of software design.
For the communication between R and Bio7 (Java) the Rserve application is used. Beside of some above mentioned disadvantages a great advantage of this Server-Client connection is the possibility to transfer calculations and data to a more powerful or specialized remote computer optional running a different Operating System. A future perspective of Bio7 in connection with Rserve could be the GUI support of several connections and remote machines to distribute calculation intensive tasks.
With the interface of Rserve many powerful Java applications can be combined with R thus supporting an easier access to specialized libraries in areas where R lacks until now the func- try { /*Creation of random data in R and the transfer to the Java arrays!*/ x = c.eval("rnorm(100000)*100").asIntegers(); y = c.eval("rnorm(100000)*100").asIntegers(); z = c.eval("rnorm(100000)*100").asIntegers(); } catch (RserveException rse) { /*error!*/ } catch (REXPMismatchException mme) { /*error!*/ } } } Figure 12: Java source to produce the dynamic visualization of 3D data of Figure 11. tionality of such specialized tools. In combination with the flexible layout of an RCP application and the many plugins available for Eclipse which can be reused for RCP applications new and manifold useful functionality can easily be integrated into Bio7. In future releases of Bio7 the R interface will be extended with more useful functionality and options.
The Bio7 development will definitely benefit from the ongoing improvement of new Eclipse RCP concepts for the upcoming Eclipse Release 4.x which will simplify the development and extend the functionality of RCP applications and plugins. The primary goal of all these efforts is a more reliable and easy to use Ecological Modeling platform in which R plays a fundamental role to detect patterns within the complexity of ecological systems.
Bio7 is an open-source application available for Windows, Linux and OS X which can be downloaded freely together with all documentation materials from the Bio7 project website (http://bio7.org/) or the associated SourceForge project website (http://SourceForge. net/projects/bio7/).