spencermathews
3/7/2014 - 5:18 AM

Installation information for R with GTK on Windows/Mac OS

Installation information for R with GTK on Windows/Mac OS

##Installation information for R with GTK+

###Windows Install the newest version of R. Additionally, I highly recommend R-Studio for working with R regularly (but the basic command line will work just fine for most applications). Once R is installed, you can install GTK directly from within R (details below). In short:

  1. From the R command line (e.g. in R-Studio), install the RGtk2 package by running: install.packages("RGtk2", depen=T)
    This might fail with the warning that package ‘RGtk2’ is not available (for R version xxx) if your version of R has been released very recently. If so, just run install.packages("RGtk2", depen=T, type="source") instead to install the RGtk2 package directly from its source code (this might take a few minutes).
  2. Then load the package by running: library(RGtk2)
    This will notice the missing GTK and prompt you to install it. Choose "Install GTK+" when prompted, it might take a few minutes to install. Afterwards it will likely still complain (restart required).
  3. Restart R/Rstudio and confirm GTK is now up and running by reloading the package: library(RGtk2)

Note: for some reason on Windows 8, the RGtk2 package is not always recognized during compilation of other source packages, even if it can be loaded and works perfectly fine. If the other package depends on RGtk2 and installation from source fails during package test loading with an error that RGtk2 could not be found, try installing without automatic reload, e.g. in installations from GitHub using devtools, try: install_github(..., reload=F, quick=T).

###MacOS With some versions of R > 3.1 and Mac OS X, GTK+ now works out of the box with the following installation instructions. Certain combinations of R and Mac OS X have trouble with this (details on the problem and instructions on how to work around it below). For example, with R 3.1.1 and Mac OS X 10.9.3 it seems to work mostly without trouble (tested in July 2014), but with R 3.1.2 and Mac OS X 10.10.1, the work-around was necessary again (tested in December 2014). When in doubt, I recommend giving the quick installation below a try and if RGtk2 does not install properly right away, not to waste any time trying to get it to work and instead just taking the work-around route below.

Install the newest version of R. Additionally, I highly recommend R-Studio for working with R regularly (but the basic command line will work just fine for most applications). For graphical user interfaces (GUIs) from R, you need both the X Window System (included by default with Mac OS X before Mountain Lion, i.e. < 10.8) and GTK. In short:

  1. Install R for Mac (get the right one for your version of Mac OS X)
  2. Optional but recommended: install R-Studio
  3. Check if you have the X11 application (relevant for newer versions of Mac OS X such as Mountain Lion, Maverick, Yosemite, etc.): go into your Applications folder and look for X11 in the Utilities subfolder. Start the program. In newer versions of Mac OS X, this will prompt an info box that X11 is no longer included with OS X. If you get this info box, follow the installation instructions to install the X11 replacement XQuartz (you might have to log out and back in after installation)
  4. Install GTK 2.24.
  5. From the R command line (e.g. in R-Studio), install the RGtk2 package by running: install.packages("RGtk2", depen=T)
    This might fail with the warning that package ‘RGtk2’ is not available (for R version xxx) depending on your version of R. If so, you can try to run install.packages("RGtk2", depen=T, type="source") instead to install the RGtk2 package from source (this might take a few minutes and ask you to install the Xcode command line tools - just hit Install and follow the instructions if you get this message, then rerun install.packages("RGtk2", depen=T, type="source")). If installation from source fails, you can try a slighlty older R version that has the binaries or check out the work-around below.
  6. Restart R/Rstudio and confirm GTK is up and running by loading the package: library(RGtk2) (should execute without errors)

#####Troubleshooting for GTK+ 2.24 issues R version 3 requires the GTK+ 2.24 framework which does not always work properly out of the box on Mac OS X throwing a malloc error when running the following simple example (or failing even before that during the installation of RGtk2 with an error that claims GTK to be missing, along the lines of configure: error: GTK version 2.8.0 required):

library(gWidgets)
options(guiToolkit="RGtk2")
win <- gwindow("test")
glabel("test label", container = win)

gedit("test text field", container = win)
> guiWidget of type: gEditRGtk for toolkit: guiWidgetsToolkitRGtk2 
> R(9523,0x7fff76be1960) malloc: *** error for object 0x4024000000000000: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug
Abort trap: 6

The problem is documented on stack overflow in some detail. There are two options:

  1. If you're okay running an older version of R, you can install the older GTK+ 2.18 framework (http://r.research.att.com/libs/GTK_2.18.5-X11.pkg) instead and use R 2.15 (http://r.research.att.com/R-2.15-branch-leopard.pkg - works fine with OS X Lion). If you want to run multiple versions of R, no problem, the little helper program RSwitch (http://r.research.att.com/RSwitch-1.2.dmg) provides a simple means for switching back and forth easily. Also, having both GTK+ 2.24 and GTK+ 2.18 installed is no problem either, just make sure the 'Current' symlink in '/Library/Frameworks/GTK+.framework/Versions/' points to 2.18.X11.

  2. There is a relatively straight-forward (but several steps long) work-around that requires installing gtk2 via macports or homebrew and installing the R packages RGtk2 and cairoDevice from source. The approach listed below is inspired by suggestions on stack overflow (especially this contribution from John Verzani and worked well for me and others).

  • install macports, a tool for installing open-source software on MacOS (http://www.macports.org/install.php)
    • macports requires Apple's Xcode command line tools (free but you need an AppleID to download), which include all the necessary compilers for macports (NOTE: newer versions of Mac OS X and certain programs that also require these compilers might ask you directly to install the Xcode command line tools, in which case you don't need an AppleID! for example, the Git App for Mac does this - just hit Install and follow the instructions when you get this message)
    • if needed, you can install the full version of Xcode (~2Gb) or just the command line tools (in either case, make sure to install the latest version for your MacOS version)
      • for the full version, after installation go to the XCode preferences and install the command line tools
      • for the command line tools only, search for command line tools on https://developer.apple.com/downloads/ (the standalone command line tools are not recognized by the MacPorts installers but the warnings can be safely ignored)
  • after installing macports, open a terminal and run the following commands to install GTK (you will be asked for your password), also note that macports might complain about missing XCode if only the command line tools are installed but this is not a problem
  • export PATH=/opt/local/bin:/opt/local/sbin:$PATH
  • sudo port selfupdate
  • sudo port install pkgconfig
  • sudo port install gtk2 +x11 (this may take several minutes)
  • then download the RGtk2 and cairoDevice package source from CRAN:
  • http://cran.r-project.org/web/packages/RGtk2/ - download the package source (RGtk2_version.tar.gz)
  • http://cran.r-project.org/web/packages/cairoDevice/ - download the package source (cairoDevice_version.tar.gz)
  • install the two packages by running the following commands on the terminal (adjust for the correct path to the downloads and the correct version you downloaded, RGtk2 may take several minutes, both may show a few warnings about deprecated code)
  • R CMD INSTALL ~/Downloads/RGtk2_version.tar.gz
  • R CMD INSTALL ~/Downloads/cairoDevice_version.tar.gz
  • Note: this entire installation has to be repeated after operating system upgrades (e.g. from Mavericks to Yosemite, etc.) but only this last step (installing RGtk2 and cairoDevice from source) has to be repeated after upgrading to a new version of R / reinstalling R

To test that the installation was successul, restart R/RStudio and see if RGtk2 now loads without error:

library(RGtk2)

If you want, you can run the following small example too (installs the very user-friendly gWidgets wrapper for RGtk), it should create a simple GUI (Note: some warnings may be printed in RStudio).

install.packages("gWidgets", depen=T)
install.packages("gWidgetsRGtk2", depen=T, type="source") # type="source" only necessary if not available as binary
library(gWidgets)
options(guiToolkit="RGtk2")
win <- gwindow("test")
glabel("test label", container = win)
gedit("test text field", container = win)