Missing package in rpy2 (ggplot2)

When using rpy2, the python implementation of R, I quickly came up with the issue of having missing packages (e.g., ggplot2)

With a fresh installation of rpy2 (and R-core), if you type the following code in your python shell:

from rpy2.robjects.packages import importr
importr("ggplot2")

You may have the following error message


Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called 'ggplot2'
Error in .Primitive("as.environment")("package:ggplot2") :
no item called "package:ggplot2" on the search list

The solution is to install the missing package. This is not a Python package (so no use to try with easy_install). If you want to install a new R package, a compilation process will be required, so you must have install the R header files. For instance under Fedora,

sudo yum install R-devel

Then, start R by typing “R” in a unix shell. R should start and prompt a command line where you just type:


install.packages(c("getopt","ggplot2"),dependencies=TRUE)

This command will download the relevant R packages, compile them and install them on your system. It may be longish depeding on the dependencies. Finally, restart your python shell and you should now be able to use the freshly installed packages.

Please follow and like us:
This entry was posted in Computer Science and tagged , . Bookmark the permalink.

3 Responses to Missing package in rpy2 (ggplot2)

  1. Kiran says:

    When installing packages in R shell. One should be in root user or non root user ? and where do these packages get installed ?

    • admin says:

      Depends on your system. As far as I am concerned, mostly use Fedora, so I can tell you that under Fedora when you install a package from R, it is installed in your home directory in the directory called ./R (e.g., /home/username/R/).
      Then, it depends on your system and R version. In my case:

      ./R/x86_64-redhat-linux-gnu-library/2.14

      You do not need to be root.

      Hope this help.
      cheers
      T.

  2. Reykavik says:

    Brilliant post Thomas!

    I’d been trying to install ggplot2 on Korora for two days following numerous other posts without any luck.

    Your method worked first time – thank you!

Leave a Reply

Your email address will not be published.