1

All the R packages installed on this Linux machine are installed in /usr/lib/R/library. I want to move these packages to another location, such as /home/username/.R_libs. I not only want to install packages in this new location, but I want to migrate all the previously installed packages to that location.

The output of .libPaths() is:

[1] "/home/username/.R_libs" "/usr/lib/R/library"

However, if I just move all the packages, like so:

sudo mv /usr/lib/R/library/* /home/username/.R_libs/

R cannot start anymore as it seems unable to find the base package.

As for environment variables, $R_LIBS is set to /home/username/.R_libs, while $R_HOME and $R_LIBS_USER are unset in the shell. However, for some reason:

> Sys.getenv("R_LIBS")
[1] "/home/username/.R_libs"
> Sys.getenv("R_HOME")
[1] "/usr/lib64/R"
> Sys.getenv("R_LIBS_USER")
[1] "~/R/x86_64-pc-linux-gnu-library/3.4"

Note that ~/R/x86_64-pc-linux-gnu-library/3.4 does not exist and I do not want to create it, I want to only use /home/username/.R_libs.

In short, I want that from now on R will only consider /home/username/.R_libs and always install libraries to (or read libraries from) that location without asking for confirmation or additional options.

The best option, if I understood this correctly, would probably to move only the non-base packages, so the packages that were manually installed. Is this possible? Can this be done? Note that I'd prefer not to have to uninstall then reinstall all packages,

AF7
  • 3,160
  • 28
  • 63
  • 1
    `export R_LIBS="/home/username/.R_libs"` – M-- Jun 07 '17 at 19:05
  • @Masoud Sorry, I do not to understand. `R_LIBS` is already set to the folder that I want the packages to be in, namely `/home/username/.R_libs` – AF7 Jun 07 '17 at 19:06
  • You can run this within R `.libPaths("/home/username/.R_libs")` – M-- Jun 07 '17 at 19:10
  • @Masoud `.libPaths()` already returns `[1] "/home/username/.R_libs" "/usr/lib/R/library"`. What should the difference be? – AF7 Jun 07 '17 at 19:11
  • You need to remove the `/usr/lib/R/library/` from that, right? – M-- Jun 07 '17 at 19:14
  • No, I do not think so. I do not see why. The library I want is first anyway, so the second library should not matter. I want to migrate packages from the second library to the first. – AF7 Jun 07 '17 at 19:15
  • 1
    What I meant was you want to set it as default. But let's put our discussion on hold. I will post an answer after I made sure that it'll work for you. – M-- Jun 07 '17 at 19:16
  • Possible duplicate of [How do you change library location in R?](https://stackoverflow.com/questions/2698269/how-do-you-change-library-location-in-r) Read the last one. I just edited `.RProfile` and it worked. Notice that I kept the copy of that in the default location. – M-- Jun 07 '17 at 19:24
  • @Masoud If I just add the line, new packages will be installed there. Which is OK. But If I try to update the older packages, they will still be in `/usr/lib/R/library/`. I do not want that. – AF7 Jun 18 '17 at 17:10
  • https://stackoverflow.com/questions/7133394/migrating-r-libraries – M-- Jun 18 '17 at 17:12
  • @Masoud This basically suggest to re-install all installed packages, which is something I'd like to avoid. Surely it can be done! – AF7 Jun 18 '17 at 17:50

1 Answers1

0

I solved this by listing all packages that have Priority NA or "Recommended" in R, and moving only that with a bash script. This will ignore all the base R packages that R seems to require being stored where they currently are. So from R create a text file to read from the bash script:

ip <- installed.packages(lib.loc="/usr/lib/R/library/")[ , "Priority"]
write.table(installed.packages(lib.loc="/usr/lib/R/library/")[is.na(ip) | ip=="recommended", 1], file="ip.txt", row.names=FALSE, quote=FALSE)

Note that you will need to change /usr/lib/R/library/ to the correct folder you want to copy from.

Then run this bash script (remember set your own target dir, stored in the output variable, and your username, stored in the user variable):

#!/bin/bash

set -e
{
input="/usr/lib/R/library/"
output="/home/username/.R_libs/"
user="username"
packages="ip.txt"

for p in $(cat ${packages})
do
    if [ -e ${input}/${p} ]; then
        echo Moving package ${p}
        mv ${input}/${p} ${output}/${p}
    else
        echo Package ${p} is not present in ${input}
    fi
done
echo Changing perimssions...
chown -R ${user} .R_libs/*
echo All done!
}

Everything should be moved correctly.

AF7
  • 3,160
  • 28
  • 63