Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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)

EconGeo commented Jun 8, 2014

I just wanted to point out that when I used this approach and homebrew, using the R CMD INSTALL installed the packages to /usr/local/Cellar/ and I needed to add a library flag to my installation command to point to the user library of R. For me it was

R CMD INSTALL -l /Users/username/Library/R/3.0/library ~/downloads/RGtk2_version.tar.gz

hope that helps some people out. Thanks for the guide!

The first posting was extremely helpful. Thanks!

Finally got it to work with workaround 2. Thanks!

Thor90 commented Jan 7, 2015

Hi there,
I have been trying to install GTK2 +x11 for mac in order to run (RQDA) and (rattle) packages in r/ r studio. However, while following the steps above, I encountered this problem.

MacBook-Pro:~ Thor$ sudo port install gtk2 +x11
---> Computing dependencies for gtk2
---> Dependencies to be installed: gdk-pixbuf2 tiff hicolor-icon-theme pango Xft2 harfbuzz graphite2 xorg-libXcomposite xorg-compositeproto xorg-libXfixes xorg-fixesproto xorg-libXcursor xorg-libXdamage xorg-damageproto xorg-libXi xorg-inputproto xorg-libXinerama xorg-xineramaproto xorg-libXrandr xorg-randrproto
---> Configuring tiff
Error: org.macports.configure for port tiff returned: autoreconf failure: command execution failed
Error: Failed to install tiff

Please see the log file for port tiff for details:
/opt/local/var/macports/logs/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_graphics_tiff/tiff/main.log
Error: The following dependencies were not installed: gdk-pixbuf2 tiff hicolor-icon-theme pango Xft2 harfbuzz graphite2 xorg-libXcomposite xorg-compositeproto xorg-libXfixes xorg-fixesproto xorg-libXcursor xorg-libXdamage xorg-damageproto xorg-libXi xorg-inputproto xorg-libXinerama xorg-xineramaproto xorg-libXrandr xorg-randrproto
To report a bug, follow the instructions in the guide:
http://guide.macports.org/#project.tickets
Error: Processing of port gtk2 failed
MacBook-Pro:~ Thor$

Any ideas with this? My information: OS X Yosemite version 10.10.1 MacPorts based version 2.3.3 XQuartz 2.7.7 (xorg-server 1.15.2). Thanks!

ElStef commented Feb 11, 2015

Hey, after running the previous advices, RQDA is installed and can be launched, but when creating a new file, I got the following messages:

(R:18985): Gtk-WARNING **: Attempting to store changes into `/Users/arbeitsmac/.local/share/recently-used.xbel', but failed: File »/Users/arbeitsmac/.local/share/recently-used.xbel.DDSXTX« could not be created: No such file or directory

(R:18985): Gtk-WARNING **: Attempting to set the permissions of `/Users/arbeitsmac/.local/share/recently-used.xbel', but failed: No such file or directory

Any ideas? There seems to be similar problems with GEDIT with similar error-messages, but the solutions won't work. I got OS X 10.10.2 and the latest Macport and XQuartz 2.7.7 versions.

Thanks a lot!

Incase, anyone is having trouble installing the Rgtk2 pkg on Mac try with specifying specifically that one needs a OSX binaries.
install.packages("RGtk2", dependencies = T, type = 'mac.binary.mavericks')

Hi I am using OS X El Capitan, I have followed every step listed for the MacOS installation. When I run library(RGtk2), I am wondering my R session is aborted, it says my R encountered a fatal error.

install.packages("RGtk2", depen=T, type="source") still doesn't work (for R version 3.3.0) :(

tobybot11 commented May 18, 2017 edited

On macosx Sierra, with R 3.4.0 and homebrew ..
the basic problem is that Rgtk requires the x11 libraries/headers (gdk/gdkx.h specifically)
and doesn't work with the quartz libraries/headers which now is the default for GTK

So I had to do the following to install RGtk2 after reading the configure options for GTK

# brew edit gtk+
in the def install section, removed the reference to quartz and switched to
"--with-gdktarget=x11",
"--enable-x11-backend"
# brew install --build-from-source --verbose gtk+

then had to make sure the pkg-config tool was installed and using a path that includes this file
/usr/local/lib/pkgconfig/gtk+-2.0.pc and the rest of the X11 .pc files in /opt/X11/lib/pkgconfig

for my system this worked..
# export PKG_CONFIG_PATH=pkg-config --variable pc_path pkg-config:/opt/X11/lib/pkgconfig
# R CMD INSTALL RGtk2_2.20.33.tar.gz

@tobybot11

before that, ensure you have cairo installed with --with-x11 arg

brew uninstall cairo --ignore-dependencies
brew install --with-x11 cairo

also, instead of:

export PKG_CONFIG_PATH=pkg-config --variable pc_path pkg-config:/opt/X11/lib/pkgconfig

which gave me some errors I did:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/local/lib/pkgconfig/gtk+-2.0.pc:/opt/X11/lib/pkgconfig

Lastly, remember to R CMD INSTALL RGtk2_2.20.33.tar.gz from the same terminal window, install.packages("Rgtk2") isn't going to work.

Nachospt commented Jun 1, 2017 edited

Cant make it work :S

install.packages("RGtk2", depen=T, type="source")
Installing package into ‘C:/Users/nacho/Documents/R/win-library/3.3’
(as ‘lib’ is unspecified)
Warning in install.packages :
package ‘RGtk2’ is not available (for R version 3.3.3)

DvP17 commented Jun 14, 2017

@Nachospt

If you update R to version 3.4.0 everything will work fine.

sharpe5 commented Jun 14, 2017

Got it working on Windows 10 + RStudio.

Tips:

  • I used the latest version of v1.0.143.
  • Must use R v3.4.0. Ensure that you point RStudio at this version of R in the options.
  • Must run RStudio as "Administrator".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment