Installing packages R (Ubuntu) - r

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).

Related

Cannot change working directory to folder in windows through R in Ubuntu command line

I have had to install Ubuntu command line on windows10 in order to install R and download a package that is not supported on R for windows. This has worked fine, however, I need to set the working directory in R to the folder containing the files the package needs to work with. I have looked at lots of forums about changing directory and I have no luck!
My default working directory is a hidden folder created by windows when I installed ubuntu command line. I can access this, but I dont need this folder...I need to change the working directory in R to a folder on the D drive on my computer. I have tried to no avail!!
I have tried the normal setwd() command with the following combinations, all of which return the error 'cannot change working directory'
setwd("D:/RNA-seq data/")
setwd("d:/RNA-seq)
setwd("~D:/RNA-seq)
I am really frustrated that I cannot carry out this simple command (maybeI am doing something fundamentally wrong) or maybe this is a limitation because of the hidden default home folder set by windows? Please not I cannot transfer the necessary files in to the default folder created by windows for 'home/R' as it is on the C drive and there isnt enough space on disk. I need to navigate to the folder on d drive in order to carry out my code!!
Any help would be really really appreciated! Many thanks!

Rstudio ignore the R path I manually set but use the deleted one

enter image description here
Even I export the R path to be the correct one, rstudio still use the deleted R path.
I checked my ~/.bashrc file and the deleted path is not in $PATH variable...
Ok , thanks for user 42's answer.
However, I can't find where is .Rprofile located and I've made too many changes to configuration files that it's hard to pinpoint where's the problem any more. I completely erase my disk division and reinstall ubuntu.
My system is ubuntu server so I try to install everything just as user rather than root. And specifically I install R, R-essentials using conda in the directory /usr. This time the Rstudio can find R without problem. Therefore I think Rstudio can only find R in certain directories... like /bin /usr/bin. And somehow it ignores R in other directories.

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!

Change default Install location in R Studio in Ubuntu

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.

Resources