R and RStudio installation and package error

13,954

Solution 1

I don't know why R 3.4.1 changes the library path. To change it back, simply edit /etc/R/Renviron:

$ sudo gedit /etc/R/Renviron

Comment-out R_LIBS_SITE=... then uncomment R_LIBS_USER=...

# edd Jun 2017  Comment-out R_LIBS_USER
R_LIBS_USER=${R_LIBS_USER-'~/R/x86_64-pc-linux-gnu-library/3.4'}

# edd Apr 2003  Allow local install in /usr/local, also add a directory for
#               Debian packaged CRAN packages, and finally the default dir 
# edd Jul 2007  Now use R_LIBS_SITE, not R_LIBS
#R_LIBS_SITE=${R_LIBS_SITE-'/usr/local/lib/R/site-library:/usr/lib/R/site-library:/usr/lib/R/library'}  

Solution 2

This answer is about installing additional R packages within R and RStudio. If you are looking for how to install R and RStudio see Installing RStudio -- is this very different from other packages?

Two Ways to Install R packages

I do not recommend RStudio or R with sudo privileges. Running a single R command with sudo like sudo R INSTALL fGarch should be fine. The two methods described below are other alternatives.

1. R package as Ubuntu software

As you use R, you will need to install other R packages. Sometimes these are available in the repositories. Say the r package you want it fGarch then the package in the repositories is called r-cran-fGarch. Open a terminal by pressing Cntrl+Alt+T and enter:

sudo apt install r-cran-fGarch

Installing R packages in this way has two benefits:

  1. It will keep fGarch up-to-date. Whenever fGarch is updated in R-Cran repository, it will be updated in your computer along with the system update process.
  2. All users of this computer will be able to use the fGarch package within R and RStudio.

However, not every R package has a corresponding precompiled installation deb package in the Cran repository. fGarch just happens to to one of them. So, the above method does not work for this package.

2. R Package from inside RStudio

R packages such as fGarch can be easily installed and updated from inside RStudio. If you do this, you will see the error message and find that RStudio puts the new files (including updates) inside in your home directory /home/<user>/R/site-library/. This is fine if you are the only user of this computer who use R and RStudio. The "warning" you see will not stop you from using the package fGarch. However, other users of this computer won't have access to it.

One way to solve this problem and let RStudio install all future additional packages in their proper place is to add yourself to the group staff. Open a terminal by pressing Cntrl+Alt+T and enter:

sudo adduser <user> staff

Replace <user> with your username.

After this, you will get the option of installing or updating packages /usr/local/lib/R/site-library/ or in /home/<user>/R/site-library/. Then you will be able to choose the former as the default install location.

Source: https://stackoverflow.com/questions/5560139/install-r-package-xml-in-debian-ubuntu

Hope this helps

Solution 3

If you want to install the R add-on package for all users to /usr/local/lib you'll need to give R super-user privileges to create files there. To install R add-on packages as super-user run:

sudo R CMD INSTALL <PACKAGES…>

where you replace <PACKAGES…> by the names of any number add-on packages to install.


If you want to install a package for yourself only you don't need to give R super-user privileges but you need to specify an installation target. One convenient way to do this is with an environment variable:

export R_LIBS_USER=<PATH>

where you replace <PATH> by the path to a directory for your user-specific R packages. I recommend ~/.local/lib/R/site-library. Now you can install the packages:

R CMD INSTALL <PACKAGES…>

You'll need to assure that this environment variable is set before starting R or it won't find the packages installed to that location. The best option would be to append the above export command to the file ~/.profile. See Environment Variables for more.

Share:
13,954

Related videos on Youtube

Sumeet Deshmukh
Author by

Sumeet Deshmukh

Bonjour, I was a frustrated Windows user now converted completely to Ubuntu and learning the power of Linux, I'm not too good at code But I'm a curious person and I generally generate good questions from that curiousness. And the only thing that I know in French is "Bonjour" Few things you should know about me I Prefer Gnome with Adapta over Everything else. I'll always prefer GUI over CLI if that option is available. I generally use my computer for watching movies, listening to music and surfing the web. I'm planning to add more points in this bullet list soon.

Updated on September 18, 2022

Comments

  • Sumeet Deshmukh
    Sumeet Deshmukh over 1 year

    After installing R and RStudio in Ubuntu 16.04 with the following code:

    sudo apt-get update
    sudo apt-get install r-base
    sudo apt-get install r-base-dev
    

    and

    sudo apt-get install gdebi-core
    wget https://download1.rstudio.org/rstudio-1.0.143-amd64.deb
    sudo gdebi rstudio-1.0.143-amd64.deb
    

    I am trying to install packages and I am getting errors in R console like

    install.packages("fGarch")
    Installing package into ‘/usr/local/lib/R/site-library’
    (as ‘lib’ is unspecified)
    Warning in install.packages :
      'lib = "/usr/local/lib/R/site-library"' is not writable

    As I am new in Linux I do not know in which directory install the R packages. I am not sure whether this is the problem or if I did any mistake on the installation. Can anyone help me with this issue? Thanks in advance.

  • Sumeet Deshmukh
    Sumeet Deshmukh almost 7 years
    Could you clear it up a little bit?
  • Nik P
    Nik P almost 7 years
    I cleared it up, now just run your install packages through the terminal as if you were inside R studio