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:
RGtk2
package by running: install.packages("RGtk2", depen=T)
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).library(RGtk2)
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:
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)RGtk2
package by running: install.packages("RGtk2", depen=T)
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.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:
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.
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).
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)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
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)