Skip to content

Instantly share code, notes, and snippets.

@sebkopf
Last active February 22, 2024 06:10
Show Gist options
  • Star 32 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save sebkopf/9405675 to your computer and use it in GitHub Desktop.
Save sebkopf/9405675 to your computer and use it in GitHub Desktop.
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)
@sjkiss
Copy link

sjkiss commented Jun 14, 2018

Hi there: I am running

R version 3.4.1 (2017-06-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.4

i followed the steps above and things worked right up to the point when I tried to load RQDA. The install seemed to work but then running library(RQDA) returned this error.

GdkPixbuf-WARNING **: 12:27:14.350: Cannot open pixbuf loader module file '/opt/local/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache': No such file or directory This likely means that your installation is broken. Try running the command gdk-pixbuf-query-loaders > /opt/local/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache

I really don't have any idea what this means.

@oscci
Copy link

oscci commented Jun 24, 2018

This is hugely helpful. Thank you. However, I'm a newbie at using the command line, and so am not sure how to deal with this:
At the point where I do: R CMD INSTALL ~/Downloads/RGtk2_version.tar.gz

I get

ERROR: this R is version 3.3.2, package 'RGtk2' requires R >= 3.4.0.

I have uninstalled R & updated to R3.5.0 and restarted, but it persists in thinking I am on R 3.3.2, so presumably I somehow to reset the default path. Any idea how to do that? Thanks

@oscci
Copy link

oscci commented Jun 24, 2018

Just solved my own problem! It was trying to install into an Anaconda directory that I never use. I have deleted Anaconda and now it is OK

@naalipalo
Copy link

I have posted this question in a couple other forums. This is the closest discussion to my issue, however i am not using a Mac, Windows based issue. I can't get RGtk2 to install at all.
I am running R version 3.5.1 (2018-07-02) -- "Feather Spray" Platform: i386-w64-mingw32/i386 (32-bit) RStudio Version 1.1.456

I need to work with MixSIAR package and I am supposed to install several packages to get a GUI to work. However, I can't even get past the first step and it has nothing to do with MixSIAR package....I have tried to install the RGtk2 package and each time I get errors and the install fails, Ive even tried installing outside of R studio and still have issues. I don't know where to start.

install.packages("RGtk2", dependencies = TRUE)

Installing package into ‘C:/Users/myname/Documents/R/win-library/3.5’ (as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/RGtk2_2.20.35.zip'
Content type 'application/zip' length 16250952 bytes (15.5 MB)
downloaded 15.5 MB
package ‘RGtk2’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\myname\AppData\Local\Temp\RtmponDKXU\downloaded_packages

library(RGtk2)

A window pops up with a system error which says

The program can't start because libatk-1.0-0.dll is missing from your computer. Try reinstalling the program to fix this problem

I click OK and the next screen immediately to pop up is

Need GTK+?

I get 2 options:

Install GTK+ or Do not install GTK+

If I click DO NOT install, I get stuck in a loop that RStudio will not let me out of. It immediately sends the system error message again, I click OK and it brings back the Install GTK+ question. This cycle continues until I select Install GTK+ or I kill RStudio manually

When I click install and OK and every time I get this:

Loading required package: RGtk2
Error in inDL(x, as.logical(local), as.logical(now), ...) :
unable to load shared object 'C:/Users/myname/Documents/R/win-library/3.5/RGtk2/libs/i386/RGtk2.dll':
LoadLibrary failure: The specified module could not be found.

trying URL 'http://ftp.gnome.org/pub/gnome/binaries/win32/gtk+/2.22/gtk+-bundle_2.22.1-20101227_win32.zip'
Error: package or namespace load failed for ‘RGtk2’:
.onLoad failed in loadNamespace() for 'RGtk2', details:
call: download.file(dep_url, path, mode = "wb")
error: cannot open URL 'http://ftp.gnome.org/pub/gnome/binaries/win32/gtk+/2.22/gtk+-bundle_2.22.1-20101227_win32.zip'
Error: package ‘RGtk2’ could not be loaded
In addition: Warning messages:
1: Failed to load RGtk2 dynamic library, attempting to install it.
2: In download.file(dep_url, path, mode = "wb") :
InternetOpenUrl failed: 'The operation timed out'

@greebie
Copy link

greebie commented Jan 29, 2020

Installing gtk for High Sierra required the following which is roughly a repeat of this, except GTK has a different method of installation now apparently.

Download gtk setup script for Mac

Build it via jhbuild.

sh gtk-os-setup.sh
jhbuild bootstrap-gtk-osx
jhbuild build meta-gtk-osx-bootstrap meta-gtk-osx-core

Download install Cairo and RGTK2 in the command line

R CMD INSTALL ~/Downloads/cairoDevice_{VERSION}.tar.gz
R CMD INSTALL ~/Downloads/RGtk2_{VERSION}.tar.gz

@maelmm
Copy link

maelmm commented Jun 1, 2020

Thank you for the detailed steps! I had just upgraded to R v4.0.0, macOS Catalina 10.15.4 and Rstudio Version 1.2.5042 when I had to install the GTK2 package. All I had to do was to follow the steps in the first original posts and it worked like magic! Thanks again!

@inesanchondo
Copy link

I have followed all of the instructions above, except I do not know how to download the script for Mac as greebie mentions.
I still cannot install RGTk2 package. I have downloaded the package, but it is not installed. I get the message below, I'd appreciate any help. Thank you!

Loading required package: RGtk2
Error in dyn.load(file, DLLpath = DLLpath, ...) :
unable to load shared object '/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RGtk2/libs/RGtk2.so':
dlopen(/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RGtk2/libs/RGtk2.so, 6): Library not loaded: /usr/local/opt/gtk+/lib/libgtk-quartz-2.0.0.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RGtk2/libs/RGtk2.so
Reason: image not found

@alvaroiramirez
Copy link

Hi,

I am getting this error when installing RGtk2 on RStudio 2021.09.1 Build 372 on Windows 10:

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

A version of this package for your version of R might be available elsewhere,
see the ideas at
https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages

I checked folder 'C:/Users/alvar/OneDrive/Documents/R/win-library/4.1' but I do not see anything called RGtk2 or similar.

The R version I am using is:

R.version

               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          4                           
minor          1.2                         
year           2021                        
month          11                          
day            01                          
svn rev        81115                       
language       R                           
version.string R version 4.1.2 (2021-11-01)
nickname       Bird Hippie 

What could the problem be?

@BroVic
Copy link

BroVic commented Jan 4, 2022

@alvaroiramirez
RGtk2 now stands archived on CRAN and thus install.packages() won't work. If you want to install the package, you can use remotes::install_version().

@dawnofeva
Copy link

I'm about to throw in the towel. I've gone through this page several times and followed the workaround exactly as written.

I have macOS Monterey and RStudio version 4.1.2
I installed MacPorts / XQuartz / package dependencies.

On the last step this is what I'm seeing:

library(RGtk2)
Error in dyn.load(file, DLLpath = DLLpath, ...) :
unable to load shared object '/Library/Frameworks/R.framework/Versions/4.1/Resources/library/RGtk2/libs/RGtk2.so':
dlopen(/Library/Frameworks/R.framework/Versions/4.1/Resources/library/RGtk2/libs/RGtk2.so, 0x0006): symbol not found in flat namespace '_cairo_destroy'

@BroVic
Copy link

BroVic commented Jan 11, 2022

@evabea
Don't give up, cos you're almost there. I'm not very familiar with Mac OS, so I will find it difficult to help remotely. But here's what you have to do, in principle: Get the GTK distribution so that you can build the RGtk2 dynamic library (the missing object that is causing your error i.e. RGtk2 so). I've written a function that automatically does this on Windows, so it's possible. See this thread .

I don't think this workaround will work without some modifications around the fact that RGtk2 binaries are not currently available on CRAN and have to be built by the user.

@dawnofeva
Copy link

Thanks @BroVic - I saw most of the solutions on that thread were regards to Windows in lieu of Mac.
I get into a never-ending loop to install GTK+ with no end in sight. My XQuartz then freezes.

Here is a screenshot.
image

Again, I do not know what else to do, other than to delete everything.

I require this package for my course so not really sure if there are any other options that don't include the RGtk2 package.

@BroVic
Copy link

BroVic commented Jan 11, 2022

Hi, please generate the error, run sessionInfo() and paste the output.

Ordinarily, when you get to this stage, Gtk+ would be downloaded and installed automatically. So if it doesn't work, it means the RGtk2 itself was not properly built and/or installed (as I explained earlier). You can examine that folder shown in the error message; if you don't see a file called RGtk2.so, then that is likely the problem.

@dawnofeva
Copy link

dawnofeva commented Jan 11, 2022

Hello, I am prompted to install GTK+ and I follow all instructions. It does appear to be successful, however, whenever I run the library I get this verbiage to install again and again.

As per your request, here are my sessionInfo stats:
R version 4.1.2 (2021-11-01)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base

other attached packages:
[1] rattle_5.4.0 bitops_1.0-7 tibble_3.1.6 RGtk2_2.20.36

loaded via a namespace (and not attached):
[1] fansi_1.0.0 utf8_1.2.2 crayon_1.4.2 lifecycle_1.0.1
[5] magrittr_2.0.1 pillar_1.6.4 rlang_0.4.12 stringi_1.7.6
[9] vctrs_0.3.8 ellipsis_0.3.2 tools_4.1.2 compiler_4.1.2
[13] pkgconfig_2.0.3 tcltk_4.1.2

Also that .so file is present:
image

@BroVic
Copy link

BroVic commented Jan 11, 2022

Okay I presume you're trying to use rattle, which requires RGtk2. Unfortunately I can't reproduce the problem and walk you through (don't have a Mac). I also don't know when you installed RGtk2 as the versions have to match. The simplest hack I can suggest is to try manually installing GTK+ and make sure it's on your System PATH. Use this link: http://r.research.att.com/libs/GTK_2.24.17-X11.pkg​.

Source: https://github.com/lawremi/RGtk2/blob/master/RGtk2/R/zzz.R

@jpencarnacao
Copy link

When trying to install RGtk2 from source (https://cran.r-project.org/src/contrib/Archive/RGtk2/), i still have errors:

fatal error: glib.h: No such file or directory
#include <glib.h> ^~~~~~~~
compilation terminated.

ERROR: compilation failed for package 'RGtk2'

I'm on windows 11..
Tried all possible updates on R, R studio.. tried older versions, nothing.

@BroVic
Copy link

BroVic commented Sep 27, 2022

@jpencarnacao What version of R?

@jpencarnacao
Copy link

@BroVic thanks.. i believe i worked it around. That was only needed to install a GUI interface. Which after-all was only needed if you didn't want to just use code/scrips to work with the MixSIAR package.

thanks anyway!

@r4work
Copy link

r4work commented Oct 28, 2022

I followed the steps given on this page and did see the installation of pkgconfig and gtk2 on my terminal. However, I did not find any luck in installing RGtk2 and CairoDevice from the CRAN website archive (I did mentioned the path location and latest 2021 versions of both in my terminal). I am getting these errors

**_cairoDevice.c:1506:7: error: use of undeclared identifier 'PROBLEM'
PROBLEM "unable to start device cairo" ERROR;
^
cairoDevice.c:1532:5: error: use of undeclared identifier 'PROBLEM'
PROBLEM "Cairo device: invalid width or height" ERROR;
^
2 warnings and 2 errors generated.
make: *** [cairoDevice.o] Error 1
ERROR: compilation failed for package ‘cairoDevice’

  • removing ‘/Library/Frameworks/R.framework/Versions/4.2/Resources/library/cairoDevice’_**

I am using Mac Monterey 12.6 and R version 4.2.1. in my Rstudio. Am I not picking a right versions of RGtk2 and CairoDevice from the CRAN archive? Appreciate any idea how to make it work?

@Tap1990tap
Copy link

@BroVic hi! Resurrecting this old issue and hoping you have some insight! I'm trying to install RGtk2 on R to use Rattle. Unfortunately, it is running into the same issue as @jpencarnacao was having. I am using R 4.1.1 with Rtools4.0 on Window 10 64bit. I'm trying to install RGtk2_2.20.36.tar.gz. I followed your suggested solution but still getting the same issue:

remotes::install_version("RGtk2", "2.20.36", upgrade = "never", build = TRUE, repos = "https://cran.r-project.org", INSTALL_opts = "--no-multiarch")
Downloading package from url: https://cran.r-project.org/src/contrib/Archive/RGtk2/RGtk2_2.20.36.tar.gz
Running R CMD build...

  • checking for file 'C:\Users\TuanA\AppData\Local\Temp\RtmpM1jmkp\remotes41407eae1b9b\RGtk2/DESCRIPTION' ... OK
  • preparing 'RGtk2':
  • checking DESCRIPTION meta-information ... OK
  • cleaning src
  • checking for LF line-endings in source and make files and shell scripts
  • checking for empty or unneeded directories
  • building 'RGtk2_2.20.36.tar.gz'
  • installing source package 'RGtk2' ...
    ** using staged installation
    ** libs
    "C:/rtools40/mingw64/bin/"gcc -I"C:/PROGRA1/R/R-411.1/include" -DNDEBUG -D_R_=1 -DUSE_R=1 -mms-bitfields -I/include/gtk-2.0 -I/lib/gtk-2.0/include -I/include/atk-1.0 -I/include/cairo -I/include/pango-1.0 -I/include/glib-2.0 -I/lib/glib-2.0/include -I/include/libxml2 -I/include/gdk-pixbuf-2.0 -I/include -I. -O2 -Wall -std=gnu99 -mfpmath=sse -msse2 -mstackrealign -c RGtkDataFrame.c -o RGtkDataFrame.o
    In file included from ./RGtk2/atk.h:4,
    from RGtk2/gtk.h:4,
    from RGtkDataFrame.h:1,
    from RGtkDataFrame.c:1:
    ./RGtk2/gobject.h:7:10: fatal error: glib.h: No such file or directory
    #include <glib.h>
    ^~~~~~~~
    compilation terminated.
    make: *** [C:/PROGRA1/R/R-411.1/etc/x64/Makeconf:238: RGtkDataFrame.o] Error 1
    ERROR: compilation failed for package 'RGtk2'
  • removing 'C:/Program Files/R/R-4.1.1/library/RGtk2'
    Warning message:
    In i.p(...) :
    installation of package ‘C:/Users/TuanA/AppData/Local/Temp/RtmpM1jmkp/file414045047b17/RGtk2_2.20.36.tar.gz’ had non-zero exit status

Any suggestions?

@BroVic
Copy link

BroVic commented Feb 22, 2024

@Tap1990tap

Hi there. I've not revisited this script for long because I wrote a package based on this idea called {RQDAassist}, which you can find here on GitHub. That package has a function called install_rgtk2_and_deps. You can try that out on R-4.1. Let me know if you experience any issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment