Error ".onLoad failed in loadNamespace() for 'tcltk'"

warship picture warship · Aug 8, 2014 · Viewed 23.4k times · Source

I have a general question about how to effectively load any kind of external package into R. I have found that many sources detailing this information are simply insufficient, but I will leave out mentioning those respective URLs. I have successfully loaded external packages into R before, but I am having difficulties loading the fgui package.

In my specific case, I cannot load the library fgui into R Studio. First I download directly from CRAN with the command:

install.packages("fgui", lib="~/Documents/R_dir")

This works fine for me. My working directory is set to ~/Documents/R_dir

The next command I use is:

library("fgui", lib.loc="~/Documents/R_dir")

The error I get is:

Error : .onLoad failed in loadNamespace() for 'tcltk', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.1/Resources/library/tcltk/libs/tcltk.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.1/Resources/library/tcltk/libs/tcltk.so, 10): Library not loaded: /opt/X11/lib/libX11.6.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.1/Resources/library/tcltk/libs/tcltk.so
Reason: image not found
Error: package or namespace load failed for ‘fgui’

I have examined the thread "Error when trying to load library(ggplot2) in R" but it did not answer my question.

Please help me with any suggestions to get the package fgui to work properly in R Studio and feel free to address the more general issue of effectively loading external packages into R.

Answer

Thomas picture Thomas · Aug 9, 2014

tcltk is not a typical package. It's probably already installed on your system and it cannot be installed using the usual install.packages procedure because it is not on CRAN (just like the base packages that come with R, like stats, etc.). Also, tcltk2 is not the same as tcltk; that is a package that expands upon (and depends on) the functionality of tcltk.

So, start with this:

> capabilities("tcltk")
tcltk 
 TRUE

If that comes back FALSE it means that your R was not built with tcltk support. You don't say what OS you're using, so it's a little bit difficult to help you.

That said, if you're on Linux, you need to rebuild R with tcltk support. This should be relatively straightforward. Basically, make sure Tcl and tk are available on your system in a standard location (something like sudo apt-get install tcl8.5-dev tk8.5-dev) and then rebuild R per usual procedures.

If you're on Mac OS, you're encountering the same error that John Fox describes here in relation to Rcmdr. In short, it's probably one of two things:

  1. A file permission issue related to the directory where you have Tcl installed. To quote Fox: "You can verify the source of this problem by issuing the following command at the R command prompt:

    system("ls -ld /usr/local /usr/local/lib /usr/local/lib/libtcl*")
    # ls: /usr/local/lib: Permission denied
    # ls: /usr/local/lib/libtcl*: Permission denied
    # drwx------  8 root  wheel  272 Sep 24 10:21 /usr/local
    

If the output of the system command looks like the above, you should go to Terminal and do the following: sudo chmod -R a+rX /usr/local and enter your password to change directory permissions.

  1. You have an out-dated version of X-Windows. Try running Mac OS Software Update.

It may also be solvable by creating a symbolic link as described in this answer.

This should never come up on Windows, unless you built R from source without tcl/tk support.