Sharing R library's between linux subsystem for windows and windows - r

I have a window ubuntu VM. I have R installed in the windows. I recently tried to call an R script in my ubuntu but it told me the library is not installed. Is there a way to tell ubuntu's R installation to use my windows R libraries so I don't have to reinstall them all on ubuntu?
StratifiedFullModel/SecondRun$ Rscript StratifiedFullModels.R
Loading required package: car
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages(x, dependencies = TRUE) :
'lib = "/usr/local/lib/R/site-library"' is not writable
Error in install.packages(x, dependencies = TRUE) :
unable to install packages
Calls: lapply -> FUN -> install.packages
In addition: Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called ‘car’
Execution halted
This is strange because the script is set up to check for packages, one of which is car, and either load them or install them. I suspect it is not doing so because of some problem with permissions.
This is problematic because I may want to write scripts that just auto install packages without me having to manually do it which would be time consuming if I share a script with a colleague or if I transfer work to a new machine.
Edit: Tried turing R on in ubuntu and running install.packages("car") returned the following error:
> install.packages("car")
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages("car") :
'lib = "/usr/local/lib/R/site-library"' is not writable
Would you like to use a personal library instead? (yes/No/cancel) cancel
Error in install.packages("car") : unable to install packages
>
I just cancelled the install. I think this confirms my suspicion that there is an issue with the permissions.

The Linux and Windows installations of R are completely separate, and that includes packages. That said, you can run the Windows version from within WSL, so you don't need to maintain duplicate environments.
Here's a screenshot of both Linux and Windows R running on my machine, from within WSL:

Note that this answer is based on my experience with WSL rather than R specifically. I'd welcome updates or more authoritative answers from anyone who has used R under WSL.
I believe you are going to run into problems if you attempt to use, at least, some Windows R libraries from WSL. A 'pure-R' library should work, in theory, but it looks to me from some quick searches that R can also include compiled code which would be platform-specific. In this case, if any libraries end up in native-code, then a Windows library is going to have issues when called from Linux with Linux path structures (e.g. /home instead of C:\Users), processes, and other OS constructs.
As for the permissions issue, R is using a default library directory (under /usr/local/lib/R/site-library) that is only writable by root. It appears that you can change the library installation directory with something like:
> install.packages("car", lib="/home/<username>/.local/lib/R/site-library/")
Of course, you'll need to create that directory first. See this page for more details.

Related

Can`t install datasets in R, not cvs and not xlsx

I am new to RStudio. And it was working well, about a week ago my computer update the new version of the system. And from this moment R doesnt work like before. I am trying to load at datasets in .xlsx and .cvs - it doesn`t work. R write like this:
** Installing R Package Dependencies for Excel Import: `'readxl', 'Rcpp'`
[1/5] Installing cellranger...
�������������� � utils::install.packages("cellranger", repos = "https://cran.rstudio.com/") :
'lib = "C:/Program Files/R/R-4.0.2/library"' is not writable
������ � utils::install.packages("cellranger", repos = "https://cran.rstudio.com/") :
unable to install packages
������: sourceWithProgress -> eval -> eval -> <Anonymous>
���������� �����������
I trying to install xlsx packages, it writes like this:
`install.packages("xlsx")`
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
Warning in install.packages :
'lib = "C:/Program Files/R/R-4.0.2/library"' is not writable
Warning in install.packages :
не могу создать каталог 'C:\Users\Lina\OneDrive\?????????' по причине 'Invalid argument'
Error in install.packages : unable to create ‘C:/Users/Lina/OneDrive/?????????/R/win-library/4.0’
if somebody can help me?
Since you appear to be a Windows user, I can offer two pieces of advice.
First, make sure your R installation is in a location that you have write permissions to. In some Windows OS, the Program Files locations are by default locked down. If the computer is yours, you should be able to change the permissions to the R folder. How to do so will vary a little by your windows version, so I will not provide more instructions. One other thing you can try is to set R and RStudio to "Run as administrator".
Second, OneDrive is a bad place to put your R installation or your R packages. OneDrive does not support all filenames and file extensions. R and its packages will generate a number of unsupported files, and you will continually be notified that something will not sync and that you should do something about it.
On a Windows machine, I generally install R and set up the library at C:\R where I have appropriate permissions.

MacOS Rstudio devtools loading fails

I need to install the package devtools in R on my mac. The R version is 3.2.3.
It gives an error saying
Warning in install.packages :
download had nonzero exit status
Warning in install.packages :
download of package ‘curl’ failed
Other posts addressing similar issues provide solutions to Linux (with the apt-get command, etc) but doesn't seem to have a Mac solution. Highly appreciate if someone with insight can offer some guide here.
The problem pertains to the download, have a look at this discussion. You can consider downloading the package first and installing a package from a local zip file.
install.packages("local_package_file", repos = NULL)
or more specifically:
install.packages("devtools.zip", repos = NULL, type = "source")
as shown on the devtools # GitHub.
You didn't mentioned, but I understand that you have the Xcode installed as well. Alternatively, it could be informative to check if your R compiler has no problems with connecting to the Internet, you can use the code: is.character(getURL("www.google.com")).

Library is not writable

I have this issue during package installation in R version 3.0.2 (2013-09-25) on an Ubuntu machine:
install.packages("randomForest")
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
How to resolve it?
For R version 3.2.2 (2015-08-14) this problem should be dealt with since R suggests within the installation process a different path to store your R libraries. The installation looks like this:
(Here 'random' is used as an example package)
install.packages('random')
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages("random") :
'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
~/R/pc-linux-gnu-library/3.2
to install packages into? (y/n) y
So during the installation answering both questions with 'y' should install the package correctly.
Update 18/01/19
In case you don't want to store your R packages in an additional file:
As Antoine-Sac and Robert TheSim point out you can add yourself to the staff group in order to be able to write to 'site-library'. (Click on the names to see their important additions)
Before this update I mentioned in this comment the option of changing the permission of the folder 'site-library' using 'chmod o+w' in order to be able to write to it. Assuming security issues but unable to tell at the time I warned about it but have primarily been waiting for somone to clear this up. Antoine-Sac and Robert TheSim have done so in the meantime. Thanks!
add yourself to the group called 'staff'
sudo usermod -a -G staff your_user_name
replace your_user_name with your login username, then logoff and relogin.
DO NOT use chmod 777 which is a breach of security and btw. a complete non-sense!!!
If you are on Windows, you can run R (or RStudio) as an administrator.
Close your R, then go to the R or RStudio icon, right-click and "open as administrator". It works perfectly, all error messages while installing packages are gone forever.
For someone who tried to use install.packages() with multiple packages like this:
install.packages("vcd","vcdExtra","plyr")
and got similar warning:
Warning in install.packages :
'lib = "vcdExtra"' is not writable
Would you like to use a personal library instead? (yes/No/cancel) cancel
Error in install.packages : unable to install packages
You should put the package names in a vector:
install.packages(c("vcd","vcdExtra","plyr"))
as the second parameter in install.packages() is lib.
The problem is that the default install location is a place where you do not have write permissions.
The solution is to use an install location where you do have write permissions.
Specifically, I'd suggest using the following commands to create a personal library folder in a place that doesn't require special permissions and that will automatically be detected the next time you startup R:
dir.create(Sys.getenv("R_LIBS_USER"), recursive = TRUE) # create personal library
.libPaths(Sys.getenv("R_LIBS_USER")) # add to the path
install.packages("randomForest") # install like always
library(randomForest) # use library like always
The call to dir.create follows the suggestion in this faq to create a folder named according to Sys.getenv("R_LIBS_USER"). This is a good choice since it will be found on the next startup of R so you will be able to use install.packages and library without specifying special locations. The .libPaths function call lets us avoid restarting R by immediately adding the new folder to the library path. The first two lines are only needed if you do not yet have a personal library created, but there is no harm in running them repeatedly. After that, installing packages and using libraries can be done as usual.
It means exactly what it says. You don't have write permission in that folder. Either you need to change the permissions for that folder, or change the R library location.
If you are using OS windows 10 then maybe Ransomware protection is on. You need to disable that.
I was facing the same problem. I had the access to write. but suddenly it stopped. I couldn't install the packages. Disabling Ransomware protection worked for me.
The "XX" is not writable error can also mean that the library path you're providing does not exist.
Use sudo to Rscript code. I have same error fixed using sudo Rscript filename.R
Error
$ Rscript babynames.R
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
Warning in install.packages("babynames") :
'lib = "/usr/local/lib/R/site-library"' is not writable
Error in install.packages("babynames") : unable to install packages
Execution halted
Fix
$ sudo Rscript babynames.R
[sudo] password for abhay:
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
also installing the dependencies ‘cli’, ‘glue’, ‘utf8’, ‘fansi’, ‘lifecycle’, ‘magrittr’, ‘pillar’, ‘pkgconfig’, ‘rlang’, ‘vctrs’, ‘tibble’
Maybe can try sudo chmod +777 #nameoflib
It works for me
You can change permission to 'site-library' and all included directories.
sudo chmod 777 -R /usr/local/lib/R/site-library
If you are using R with RStudio, rather than starting RStudio with tray icon, start Rstudio or R with command line using sudo rstudio or sudo R.
It will solve your problem for sure, it works for me. It requires sudo privilege to write something in installation directory.

Error when installing R package using Rstudio

I want to install http://cran.r-project.org/src/contrib/Archive/mecdf/ using RStudio but when I use
> install.packages('C:\\Users\\jandre\\Desktop\\mecdf_0.6.1.tar.gz', repos=NULL, type="source")
I get this error:
Installing package into ‘C:/Users/jandre/Documents/R/win-library/3.1’
(as ‘lib’ is unspecified)
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
Warning in install.packages :
running command '"C:/Program Files/R/R-3.1.1/bin/x64/R" CMD INSTALL -l "C:\Users\jandre\Documents\R\win-library\3.1" "C:/Users/jandre/Desktop/mecdf_0.6.1.tar.gz"' had status 1
Warning in install.packages :
installation of package ‘C:/Users/jandre/Desktop/mecdf_0.6.1.tar.gz’ had non-zero exit status
This occurs in every package I want to install using the tar.gz file. I'm on Win7 x64. RStudio Version 0.98.1028
Can't think of a way to fix this at the moment -- sorry -- but the R administration manual says explicitly:
Note that installing R into a directory whose path contains spaces is not supported, and at least some aspects (such as installing source packages) will not work.
I don't know why this doesn't bite people much more often, given that C:/Program Files/... seems like a relatively normal place to install things on Windows.
I would also expect that the double quotation marks around your R executable name ("C:/Program Files/R/R-3.1.1/bin/x64/R") in the system call should have protected you from this problem ... ?
I had the same error message with the installation of some packages(under Windows 10 OS with R and Rstudio).
It seems that the R software (not R Studio) is dealing with the library where package are installed.
I uninstalled R and Rstudio and installed it a path without space (ex: C:\Program\R).I tried to load the packages that previously failed and it seemed to fix the problem.

Installing ggplot on SolusOS

I've installed R and Rstudio on a netbook running SolusOS (Debian) and those installations seem to be working OK. However I've had no success with installing the R package ggplot2 .
I've tried the stock command from within R and in Rstudio of:
install.packages(“ggplot2”)
also:
install.packages(“ggplot2”, dependencies=TRUE)
both as user and with root privileges. But when I go to a mirror I get:
Warning message:
In getDependencies(pkgs, dependencies, available, lib) :
package 'ggplot2' is not available
I've also set up a /data/Rpackages/ directory as recommended in http://www.r-bloggers.com/installing-r-packages/ but this has also not been successful.
If anyone has advice on what else to try or indeed to how remove and reinstall R and start again I would be grateful.

Resources