Skip to content

Instantly share code, notes, and snippets.

@cabiling
Forked from sebkopf/gtk_install.md
Created August 29, 2014 07:11
Show Gist options
  • Save cabiling/e80a72ae5cb98d974d49 to your computer and use it in GitHub Desktop.
Save cabiling/e80a72ae5cb98d974d49 to your computer and use it in GitHub Desktop.

##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)

###MacOS With many versions of R > 3.1 and Mac OS X, GTK+ now works out of the box with the following installation instructions (tested last in July 2014 with R 3.1.1 and Mac OS X 10.9.3). Earlier versions of R v3 and 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).

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, 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 different 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):

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 work-around but it 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
  • 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)
  • export PATH=/opt/local/bin:/opt/local/sbin:$PATH
  • sudo port selfupdate
  • sudo port install pkgconfig
  • sudo port install gtk2 +x11
  • 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 a few minutes)
  • R CMD INSTALL ~/Downloads/RGtk2_version.tar.gz
  • R CMD INSTALL ~/Downloads/cairoDevice_version.tar.gz
  • Note: this last step (installing RGtk2 and cairoDevice from source) has to be repeated after upgrading to a new version of R / reinstalling R
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment