Change default Install location in R Studio in Ubuntu - r

I installed shiny using system wide installation method. So it is now at /usr/local/lib/R/site-library . And I'm using R Studio to install other packages such as RMySQL, ShinyBS etc. Those packages are located in /home/thisa/R/x86_64-pc-linux-gnu-library/3.0 . Now when I run my server.R with following libraries,
library("shinyBS", lib.loc="/home/thisa/R/x86_64-pc-linux-gnu-library/3.0")
It pops up following error.
ERROR: no library trees found in 'lib.loc'
What would be the possible solution for this should I change my default install directory of R Studio then how can I do that? I found similar problem here but the answer is not clear for me.

I truly dislike local installations below $HOME for that very reason.
Do this:
In R, do .libPaths() which will show the set of library directories known to R. Packages will go into the first of these.
On my system, I only get "/usr/local/lib/R/site-library", "/usr/lib/R/site-library"
and "/usr/lib/R/library"
I am explicitly setting (in /etc/R/Renviron) R_LIBS_SITE to be
${R_LIBS_SITE-'/usr/local/lib/R/site-library:/usr/lib/R/site-library:/usr/lib/R/library'} which excludes the user library.
I may have had to override a default in RStudio as well but I don't see right now where that has been. You can of course check by looking at .libPaths() as per 1. above.
In short, if you get the result I show in 2. then your libraries will all end up in /usr/local/lib/R/site-library and be visible to all instances, including Shiny apps.

For me I followed the steps listed above by #Dirk and I had to perform one extra step.
Adding the user to the staff group.
In the terminal run.
sudo usermod -a -G staff user_name
Replace user_name with your user name. After restart and login in again. You should be able to install packages system-wide.

Related

Installing packages R (Ubuntu)

I have a problem with R, I haven't experienced before. Before writing here I checked the forums but couldn't solved my problem. I saw somebody experiencing this problem.
First, I use Ubuntu 16.04 and R 3.4.1 (Single candle). But I can not install any packages. First it said the directory is not writeable and if I want to create a personal library?
lib = "/usr/local/lib/R/site-library"' is not writeable Would you like
to use a personal library instead? (y/n)
I couldn't :( I checked forums another option was changing permission, i coded:
1. cd /usr/local/lib/R
2. sudo chmod o+w site-library
3. ls -l but it didn't work.
When I want to download a package, it installs it at /tmp folder. And i can not use it, cause when i want to open it with "library()" it says there is no package like that. I copied the package from the /tmp folder and pasted it to the folder other packages are (i didn't installed them by myself, they were there) but still it doesn't recognize the package.
I didn't experienced this with my windows pc, and a friend with Ubuntu said he is usind R and Rstudio without problem. So what is wrong with mine :( ???
The problem is that r or rstudio doesn't have permission to write inside theses directories.
You should change your R_LIBS for a local directory, take a look in /etc/R/Renviron.site and uncomment the R_LIBS line or define it if needed.
Check if a R folder exists in your home like this ~/R/x86_64-pc-linux-gnu-library/3.4, it should be your R_LIBS address. If it doesn't exist you can create one like ~/R/library/ and define the same in Renviron.site.
More information, check here or help(Startup).

R 3.4.1 "Single Candle" Personal Library Path Error: unable to create ‘NA’

I just updated to R (3.4.1 "Single Candle") on my Linux Mint 18.1 Cinnamon machine and I attempted to install a package. R returned the following:
> install.packages('ggplot2')
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages("ggplot2") :
'lib = "/usr/local/lib/R/site-library"' is not writable
Would you like to use a personal library instead? (y/n) y
Would you like to create a personal library
NA
to install packages into? (y/n) y
Error in install.packages("ggplot2") : unable to create ‘NA’
I have encountered the 'lib not writable' output before but typically it offers a solution like this one:
Would you like to create a personal library
~/R/x86_64-pc-linux-gnu-library/3.4
to install packages into? (y/n) y
Any ideas why the personal library is suggesting NA? Is there a way to manually override this?
I don't know what's causing this problem (i'm also experiencing it on Ubuntu 16.04), but here's a quick workaround:
.libPaths(c("/home/your_username/R/x86_64-pc-linux-gnu-library/3.4/", .libPaths()))
Of course, you can replace "/home/your_username/..." for any another directory (that will store your personal library).
This solution makes install.packages() and library() work. Waiting for a full fix!
EDIT: I should note that this solution is not persistent. That is, it won't last after restarting R. You can fix this by adding the same line of code described above to the /home/your_username/.Rprofile file.
Looking at the details in #Dirk 's comment (https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=866768) this is a planned behaviour so that packages are installed once for all users of the system.
The solution is to make /usr/local/lib/R/ writable for all users, rather than to re-instate the old behaviour of having a personal package library for every individual user.
Open up a terminal and:
Navigate to /usr/local/lib/ with cd /usr/local/lib/
Change the owner:group so that all users can write to the folder. I happen to have a group on my computer that all users are a member of, so I used that, but see https://askubuntu.com/questions/66718/how-to-manage-users-and-groups for help with setting up a group if necessary
To change ownership use sudo chown owner:group -R R/. owner is an any user, it doesn't really matter. group is the key one; make sure anyone wanting to use R on your system is a member of this group. -R is recursive (i.e. do it to all files and folders in R/).
If you need to change group permissions, use chmod -R 775 R/. This gives the owner and group read, write, and execute permissions, and gives all others read and execute permissions.
Now restart R and you should be able to install packages to your this shared location.
My solution was the following:
In the file /usr/lib/R/etc/Renviron there is a configuration of R.
In lines 43-45 there is:
# edd Jun 2017 Comment-out R_LIBS_USER
#R_LIBS_USER=${R_LIBS_USER-'~/R/x86_64-pc-linux-gnu-library/3.4'}
##R_LIBS_USER=${R_LIBS_USER-'~/Library/R/3.4/library'}
I have uncommented R_LIBS_USER=${R_LIBS_USER-'~/R/x86_64-pc-linux-gnu-library/3.4'}, restarted RStudio and now it works.
EDIT: Looking at the comments, it seems like a planned behaviour. Here is another solution.
After July 8, 2017 this will resolve all problems
sudo apt-get update
The same happened to me while running the installation procedure for some Bioconductor packages.
Then I realized also I could write this (or similar) on the bash command line:
export R_LIBS_USER=$HOME/R/x86_64-pc-linux-gnu-library/3.4 && R
or
export R_LIBS_USER=$HOME/R/x86_64-pc-linux-gnu-library/3.4 && rstudio
and then run upgrade.packages() (or install.packages(), or biocLite()) inside R.
This way the change is temporary, and you don't have to update any config files.
This shell command is useless if subsequently a command in .Renvironor.Rprofile` sets R_USER_LIBS to a different location during R startup (-check your configuration).
Sticking with in $HOME/R/x86_64-pc-linux-gnu-library/3.X can be desirable
if you have already lots of packages in this location, you want them upgraded/installed there.
I have lots of Bioconductor packages in there, and I don't want them to download again, some of these packages download huge "Omics" datasets when used.
Maybe the partition where /usr/local/lib/R resides has too little disk space or is on a slow drive.
May be this is a bug of R 3.4.1, and my solution is change the line of
R_LIBS_SITE=${R_LIBS_SITE-'/usr/local/lib/R/site-library:/usr/lib/R/site-library:/usr/lib/R/library'}
in /etc/R/Renviron file into
R_LIBS_SITE=${R_LIBS_SITE-'~/R/x86_64-pc-linux-gnu-library/3.4.1:/usr/local/lib/R/site-library:/usr/lib/R/site-library:/usr/lib/R/library'}

Running R from Mac OSX terminal

I've searched the web, and I'm still unclear on how to run R from the Mac terminal. I have Rstudio and the standalone R app installed. I thought I could just type "R" from the command line as I do with "python", but that doesn't work. Is it necessary to edit the PATH in my bash profile? If so, how do I give the correct location of R?
Thanks for any help
Edits after receiving comments
So, I'm running Sierra, and when I type "r" or "R" at the terminal, I get "-bash: R: command not found." If I type, "which R" in the terminal I do not get any output.
Here is the output from "echo $PATH": /usr/local/heroku/bin:/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/Users/samuelcolon/anaconda/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Users/samuelcolon/.rvm/gems/ruby-2.1.0/bin:/Users/samuelcolon/.rvm/gems/ruby-2.1.0#global/bin:/Users/samuelcolon/.rvm/rubies/ruby-2.1.0/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/samuelcolon/.rvm/bin:/Users/samuelcolon/.rvm/bin
As for the installation, I believe I downloaded it directly from cran.r-project.org a while ago. I can locate the GUI in my applications and open it--
I have version 3.13. Is it possible, I only have R.app installed but not R? Perhaps that's the reason I'm getting the 'command not found' when typing "R" into the terminal?
Generally, I've been working in RStudio, but I'd still like to access R from the terminal and also to find where things are located. I'm fine with removing and re-installing R if it's easiest to start from square one. I hope the extra detail helps, and I appreciate the responses.
An answer for those not that familiar with Terminal and Bash.
I have done a fresh update install of R from the R.org cran site as part of seeking an answer to your question.
I found this latest install version 3.4.0 installs R for access in Terminal, and also installs R.app as part of the package.
To my understanding, reading support docs, if you have an older version of R it will update that. However it will not update an installation of R installed by the anaconda package.
Where are the R files stored?
I can only assume that with a fresh install of the latest R, R will work for you in Terminal.
To learn where the R files are that are being accessed - in Terminal after starting R, and in R.app, type:
>R.home()
In my case as example:
In R.app - the R version 3.4.0 is accessed in the top directory (not my user folder):
R.home()
[1] "/Library/Frameworks/R.framework/Resources"
In Terminal - the R version 3.3.2 is accessed in the Anaconda package, again in the top level directory.
R.home()
[1] "/anaconda/lib/R"
So I have two different versions of R, and Terminal accesses a different version to R.app.
How can I ensure I access the same version in Terminal as I do in the R.app?
For someone familiar with bash, and how the whole bash command system works I am sure there is a well constructed command. All the same here are some novice solutions.
-
• First Solution:
I could update the anaconda version, however, I would prefer not to as as other elements of the anaconda package my depend on this older version of R. For those not yet familiar with Terminal and bash, not such a novice solution.
-
• Second Solution:
This solution came from mko. It provides a single use solution. From the result above, and checking the directory structure a little further to find this R file.
Finding the significant R file enables me to edit an extension of the above path shown in the R.app. So add /bin/R to enter
/Library/Frameworks/R.framework/Resources/bin/R
Entering and pressing return will start R from this version.
Alternatively, one can find this file and icon in the GUI Finder, lead by the above result, and just double click on it, and it will open Terminal and a session with R running for you. Easy!
One could also make an alias of it and put it on your desktop for easy future starts.
-
• Third Solution:
My last solution I think may be best, adding to mko's solution. Make an alias.
Being in my home directory in Terminal I open .bash_profile using the nano text editor. (If you do not already know how to do this, then best not use this solution.)
I then add the line in this env file.
alias Rv340='/Library/Frameworks/R.framework/Resources/bin/R'
I then save the changes and exit this terminal session. I then open a new Terminal window. (This is so the changes to the env above are incorporated in the new terminal session).
Then when I enter the alias:
Rv340
The version of R I want opens.
You can choose a different alias name to "Rv340".
-
• Fourth Solution:
A second more permanent solution for opening the same version of R in Terminal is as follows.
Copy the path as showing in R.app in response to the R.home() command above, and add that path to PATH in your .bash_profile. (If you do not know already how to do this, then ignore this solution.) Do so as follows.
export PATH="/Library/Frameworks/R.framework/Resources:$PATH"
To my understanding, this ensures that bash looks here for R (and anything else), then moves on to the other paths in PATH. Since this adds this path to the beginning of $PATH, an env variable, bash looks here first where it finds the newer version first, and stops looking.
When it comes to understanding PATH in the env set up in .bash_profile the following two links were helpful.
About PATH.
How to correctly add a path to PATH.
This solution may muck with anaconda's invocation of R. I have yet to check this.
First of all, you have to start terminal application. You can use either built in Terminal.app, or you can use replacement. My favorite one is iTerm2
https://www.iterm2.com
Then, you simply open terminal window and run R. Just like shown below:
Have fun with R!
Just ran into the same issue when installing R-4.0.3.pkg on my MacBook (MacOS BigSur). Can open R.app to the clunky R GUI, but typing in 'R' in terminal doesn't work.
Turns out, an R executable lives here: /Library/Frameworks/R.framework/Versions/4.0/Resources/bin/R
So I added this alias to my newly created .zshrc script:
alias R '/Library/Frameworks/R.framework/Versions/4.0/Resources/bin/R'
Now when I type in R, it opens... I swear this all happened seamlessly in earlier versions.
There is currently a bug in CRAN's R installation package that results in it not correctly installing symbolic links to R and Rscript for commandline use. I've just verified this by inspecting the postflight script in their 4.0.5 installation package. This only impacts MacOS system releases of 20 and above (you can check with uname -r).
I've included more info here, along with what the "correct" fix should be: manually creating symbolic links to /usr/local/bin that point to the R and Rscript binaries themselves. If this is the current challenge, then this would be a far better solution to creating aliases or manipulating PATH in various ways, since it's what the installation package intended to do (and presumably will again soon).
R: command not found
In short, if this is the problem, then Ashkan Mirzaee's answer (https://stackoverflow.com/a/67202173/2093929) to create the symbolic links directly is correct in form, but might not have the right link command. The 4.0.5 package intends instead to use:
mkdir -p /usr/local/bin
cd /usr/local/bin
rm -f R Rscript
ln -s /Library/Frameworks/R.framework/Resources/bin/R .
ln -s /Library/Frameworks/R.framework/Resources/bin/Rscript .
You can create a symbolic link from R and Rscript binaries to /usr/local/bin to add them to the PATH:
sudo ln -s /Library/Frameworks/R.framework/Versions/Current/Resources/bin/R /usr/local/bin
sudo ln -s /Library/Frameworks/R.framework/Versions/Current/Resources/bin/Rscript /usr/local/bin
Now which R should return /usr/local/bin/R and you can use R.
An easy way to open RStudio with admin privilege on macOS:
Go to Applications, then right click on RStudio
Select "Show Package Contents"
Go to Contents/MacOS
Now open terminal(in bash mode). Type sudo and drag the RStudio.exec into terminal and press on ENTER
Now RStudio will have admin access!

R - How to set the path of install.packages() for shiny server ? - Ubuntu

For my system: Ubuntu 12.04 and R 3.03, whenever I install a custom package in R via
>install.packages()
the package is installed by default to
/home/USER/R/x86_64-pc-linus-gnu-library/3.0/
as opposed to system-wide in
/usr/local/lib/R/site-library/
which is needed for shiny-server to work with that package.
My temporary solution is to copy the packages to the correct folder after the fact.
Question: How can I set the default install path from the start to avoid this problem?
Yes -- I consider this to be a misfeature and disable my per-user directory.
Moreover, I mostly use a script install.r (of which a version is an example in the littler package you can install as part of Ubuntu) which simple explicitly set the /usr/local/lib/R/site-library directory as the default. With a patch we got into R 3.0.2 or 3.0.3, normal user can write into the directory and will now create group-writeable directories so other users can update and overwrite -- just make everybody a member of the same group, say staff or admin. And then you don't even need sudo or root.
I have essentially answered this same question a few times here over the years (minus the shiny angle, which is not really relevant) so feel free to search for the other for more details, examples, ...
I would propose a different approach.
The problem is that shiny-server cannot find the packages that you install because it runs them as a different user which is called shiny. This user is created upon installation of shiny-server
The easiest (and safest IMHO) way to solve this is to just install the packages as the shiny user, using the following steps.
Set a password for the user using sudo passwd shiny, now enter and confirm a password of your choosing.
Switch to the shiny account using: su - shiny
Call up R using $ R (without sudo)
Install the required packages, in this case: `install.packages("shinydashboard")
Note that if you have rstudio-server installed on the same machine then you can perform steps 2-4 using that interface. Simply go the same domain/ip and use :8787 for the rstudio-server interface instead of :3838 for shiny-server.
Adapted from my answer here

R: combining mutiple library locations with most up-to-date packages

Question: How do I move all of the most up-to-date R packages into one simple location that R (and everything else) will use from now and forever for my packages?
I have been playing around with R on Ubuntu 10.04 using variously RGedit, RCmdr, R shell, and RStudio. Meanwhile, I have installed packages, updated packages, and re-updated packages via apt, synaptic, install.packages(), etc... which apparently means these packages get placed everywhere, and (with the occasional sudo tossed in) with different permissions.
Currently I have different versions of different (and repeated) packages in:
/home/me/R/i486-pc-linux-gnu-library/2.10
/home/me/R/i486-pc-linux-gnu-library/2.14
/home/me/R/i486-pc-linux-gnu-library/
/usr/local/lib/R/site-library
/usr/lib/R/site-library
/usr/lib/R/library
First - I'm a single user, on a single machine - I don't want multiple library locations, I just want it to work.
Second - I am on an extremely slow connection, and can't keep just downloading packages repeatedly.
So - is there an easy way to merge all these library locations into one simple location? Can I just copy the folders over?
How do I set it in concrete that this is and always will be where anything R related looks for and updates packages?
This is maddening.
Thanks for your help.
Yes, it should almost work to just copy the folders over. But pre-2.14 packages WITHOUT a NAMESPACE file probably won't work in R 2.14 where all packages must have a namespace...
And you'd want to manually ensure you only copy the latest version of each package if you have multiple versions...
If you type .libPaths(), it will tell you where R looks for packages. The first in the list is where new packages are typically installed. I suspect that .libPaths() might return different things from RStudio vs. Rcmd etc.
After piecing together various bits of info here goes: A complete moron's guide to the R packages directory organization:
NB1 - this is my experience with Ubuntu - your mileage may vary
NB2 - I'm a single user on a single machine, and I like things simple.
Ubuntu puts anything installed via apt, or synaptic in:
/usr/lib/R/site-library
/usr/lib/R/library
directories. The default vanilla R install will try install packages here:
/usr/local/lib/R/site-library
Since these are system directories the user does not have write privileges to, depending on what method you are interacting with R you might be prompted with a friendly - "Hey buddy - we can't write there, you want us to put your packages in your home directory?" which seems innocent and reasonable enough... assuming you never change your GUI, or your working environment. If you do, the new GUI / environment might not be looking in the directory where the packages were placed, so won't find them. (Most interfaces have a way for you to point where your personal library of packages is, but who wants to muck about in config files?)
What seems to be the best practice for me (and feel free to correct me if I'm wrong) with a default install setup on Ubuntu, is to do any package management from a basic R shell as sudo: > sudo R and from there do your install.packages() voodoo. This seems to put packages in the usr/local/lib/R/site-library directory.
At the same time, update.packages() will update the files in /usr/lib/R/site-library and usr/lib/R/library directories, as well as usr/local/lib/R/site-library
(As for usr/lib/R/ division, it looks like /library/ has the core packages, while /site-library/ holds anything added, assuming they were installed by apt...)
Any packages previously installed and in the wrong place can be moved to the /usr/local/lib/R/site-library directory (assuming you are sudoing it) just by moving the directories (thanks #Tommy), but as usr/lib/R/ is controlled by apt - best not add or subtract anything from there...
Whew. Anyway - simple enough, and in simple language. Thanks everyone for the help.

Resources