Tidyverse in R: Cannot load package [duplicate] - r

I attempted install.packages("RMySQL") and it runs for a second and then says:
Error in install.packages : cannot remove prior installation of package ‘DBI’
I did remove.packages("DBI") and it says:
remove.packages("DBI")
Removing package from ‘/Library/Frameworks/R.framework/Versions/3.2/Resources/library’
(as ‘lib’ is unspecified)
Then I retry install.packages("RMySQL") and it says:
Error in install.packages : cannot remove prior installation of package ‘DBI’
The DBI dependency is not actually getting removed or something. Or possibly I removed it from the wrong area? I'm confused.

Try starting R without any startup files to prevent DBI from being loaded (however that happens on your box).
You should then be able to just delete the unattached package.
Alternatively, just remove the DBI/ directory from library directory.
It should look similar to this:
$ R --vanilla ## prevent inits at load
R version 3.4.1 (2017-06-30) -- "Single Candle"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> ls() ## no variables
character(0)
> search() ## no packages besides Base
[1] ".GlobalEnv" "package:stats" "package:graphics"
[4] "package:grDevices" "package:utils" "package:datasets"
[7] "package:methods" "Autoloads" "package:base"
>

I had that same problem with package 'rlang', while trying to reinstall it would present me the error: "Error in install.packages : cannot remove prior installation of package ‘rlang’ "
The problem here is that the old installation is not totally gone.
When that happens just look for the location where you have your packages installed (use the command .libPaths() in R console) , then look for a directory that has a name that is not a regular name included in installed packages, that is the folder that needs to be removed.
After removing that old installation folder you are ready to reinstall the package again, it worked for me, hope it helps!

Go to the R library that has all the packages folder and delete the particular package folder that you want to uninstall and then again try installing. Mere removing the packages with remove.package is not going to work.

Check your task manager for R related programs running in the background, I had files open from "microsoft.r.host.broker.exe" preventing me from accessing or deleting them. Closing it fixed the problem.

I've met the same problem. And I tried to restart R or RStudio, it just happened again.
Then I realized when I restarted the RStudio, it loaded previous data all the time because I saved the .Rdata and .Rhistory file in my workspace (working directory).
I deleted the two files and tried again to restart and reinstall, it worked. Maybe it's helpful if you are in the same situation.

first clear the environment so that the packages are not loaded:-
go to session in nav bar -> clear workspace.
go to session in nav bar -> restart R
then the update.packages() worked.

If you are working in RStudio:
First update all your installed packages using RStudio toolbar -> Tools -> Check for package updates.
Try installing your package again.
For me this resolved the warning 'cannot remove prior installation of package...' when trying to update (=reinstall) a previously installed R package from a GitHub repository.

I also had this issue, and no matter how many times I restarted R or terminated R, tried to delete the package file, or terminated R processes, I could not install the package (in my case, openxlsx).
Finally, I ended up restarting my PC, creating a new project that would serve as a "throwaway", called install.packages(), and that did the trick.

I reinstalled R 4.0.1 and packages again after removing R and its libraries completely
sudo rm -rf /Library/Frameworks/R.framework /Applications/R.app \
/usr/bin/R /usr/bin/Rscript
It worked

On Windows 10 and R version 3.6.3
I don't know how many times I've had this problem, solved it, and forgotten how until the next time it occurs. I have been revisiting this post SO many times by now.. This time, the following solved my problem:
Delete .Rhistory and .RData as suggested by #Marcus LCC and restart the computer.
I then tried to install my package again, with the error code:
devtools::install_github("hrbrmstr/nominatim")
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) :
there is no package called ‘rlang’
So I installed rlang (despite having it installed already), and installed my desired package again. (Note that I never deleted rlang, only .rHistory and .rData)
Worked for me, hope it helps someone! :)

change permission to all the folders in
'/Library/Frameworks/R.framework/Versions/3.2/Resources/library'
run the install.packages
once installed restore permissions

Related

Installing sleuth using Bioconductor, error where the path is not writable?

I'm having trouble installing sleuth into R studio. I have already tried troubleshooting using the suggested methods in a stockoverflow titled "installation path not writable R, unable to update packages" (link: installation path not writable R, unable to update packages). I keep getting the following error.
Error Message:
Bioconductor version 3.10 (BiocManager 1.30.10), R 3.6.1 (2019-07-05)
Installation path not writeable, unable to update packages: boot, class, foreign, KernSmooth, lattice,
MASS, Matrix, mgcv, nlme, nnet
Old packages: 'isoband', 'purrr', 'RcppArmadillo', 'RCurl', 'reshape2', 'survival'
Update all/some/none? [a/s/n]:
BiocManager::install("devtools") # only if devtools not yet installed
Update all/some/none? [a/s/n]:
BiocManager::install("pachterlab/sleuth")
Update all/some/none? [a/s/n]:
When I type in this code:
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install()
BiocManager::install("devtools") # only if devtools not yet installed
BiocManager::install("pachterlab/sleuth")
Thank you for your help!
This answer might be helpful.
It looks like the offending packages (boot, class, foreign, etc.) are installed in a location where you do not have writing permissions for.
In principle, the sleuth package should have been installed, despite the error messages (you can check this by running library(sleuth) in an R console). However, to avoid having these errors each time you try installing a (BioConductor) package, I would recommend to re-install those offending packages in a directory where you have writing access to. Depending on your OS this can be in different locations.
I found this guide particularly useful to maintain R packages.
In short (all commands are run in R):
Remove the packages mentioned in the error message with remove.packages(), if sleuth was installed, remove it as well, we will install it again later in a more maintainable way
In R, check the output of Sys.getenv("R_LIBS_USER"), this should normally be a directory path under your home directory
It's likely that the R_LIBS_USER directory does not yet exist, create it with dir.create(Sys.getenv("R_LIBS_USER"))
Restart R (in RStudio, you can do Session → Restart R
Check the output of .libPaths(), the first element should now be the directory you just created (i.e. the same as Sys.getenv("R_LIBS_USER")), this is now the default directory where R packages will be installed
Install sleuth again with BiocManager::install("pachterlab/sleuth"), this should normally also install all dependencies. If not you might have to install them separately.
Hope this helps!

Error in install.packages : cannot remove prior installation of package ‘DBI’

I attempted install.packages("RMySQL") and it runs for a second and then says:
Error in install.packages : cannot remove prior installation of package ‘DBI’
I did remove.packages("DBI") and it says:
remove.packages("DBI")
Removing package from ‘/Library/Frameworks/R.framework/Versions/3.2/Resources/library’
(as ‘lib’ is unspecified)
Then I retry install.packages("RMySQL") and it says:
Error in install.packages : cannot remove prior installation of package ‘DBI’
The DBI dependency is not actually getting removed or something. Or possibly I removed it from the wrong area? I'm confused.
Try starting R without any startup files to prevent DBI from being loaded (however that happens on your box).
You should then be able to just delete the unattached package.
Alternatively, just remove the DBI/ directory from library directory.
It should look similar to this:
$ R --vanilla ## prevent inits at load
R version 3.4.1 (2017-06-30) -- "Single Candle"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> ls() ## no variables
character(0)
> search() ## no packages besides Base
[1] ".GlobalEnv" "package:stats" "package:graphics"
[4] "package:grDevices" "package:utils" "package:datasets"
[7] "package:methods" "Autoloads" "package:base"
>
I had that same problem with package 'rlang', while trying to reinstall it would present me the error: "Error in install.packages : cannot remove prior installation of package ‘rlang’ "
The problem here is that the old installation is not totally gone.
When that happens just look for the location where you have your packages installed (use the command .libPaths() in R console) , then look for a directory that has a name that is not a regular name included in installed packages, that is the folder that needs to be removed.
After removing that old installation folder you are ready to reinstall the package again, it worked for me, hope it helps!
Go to the R library that has all the packages folder and delete the particular package folder that you want to uninstall and then again try installing. Mere removing the packages with remove.package is not going to work.
Check your task manager for R related programs running in the background, I had files open from "microsoft.r.host.broker.exe" preventing me from accessing or deleting them. Closing it fixed the problem.
I've met the same problem. And I tried to restart R or RStudio, it just happened again.
Then I realized when I restarted the RStudio, it loaded previous data all the time because I saved the .Rdata and .Rhistory file in my workspace (working directory).
I deleted the two files and tried again to restart and reinstall, it worked. Maybe it's helpful if you are in the same situation.
first clear the environment so that the packages are not loaded:-
go to session in nav bar -> clear workspace.
go to session in nav bar -> restart R
then the update.packages() worked.
If you are working in RStudio:
First update all your installed packages using RStudio toolbar -> Tools -> Check for package updates.
Try installing your package again.
For me this resolved the warning 'cannot remove prior installation of package...' when trying to update (=reinstall) a previously installed R package from a GitHub repository.
I also had this issue, and no matter how many times I restarted R or terminated R, tried to delete the package file, or terminated R processes, I could not install the package (in my case, openxlsx).
Finally, I ended up restarting my PC, creating a new project that would serve as a "throwaway", called install.packages(), and that did the trick.
I reinstalled R 4.0.1 and packages again after removing R and its libraries completely
sudo rm -rf /Library/Frameworks/R.framework /Applications/R.app \
/usr/bin/R /usr/bin/Rscript
It worked
On Windows 10 and R version 3.6.3
I don't know how many times I've had this problem, solved it, and forgotten how until the next time it occurs. I have been revisiting this post SO many times by now.. This time, the following solved my problem:
Delete .Rhistory and .RData as suggested by #Marcus LCC and restart the computer.
I then tried to install my package again, with the error code:
devtools::install_github("hrbrmstr/nominatim")
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) :
there is no package called ‘rlang’
So I installed rlang (despite having it installed already), and installed my desired package again. (Note that I never deleted rlang, only .rHistory and .rData)
Worked for me, hope it helps someone! :)
change permission to all the folders in
'/Library/Frameworks/R.framework/Versions/3.2/Resources/library'
run the install.packages
once installed restore permissions

installation path not writable R, unable to update packages

I am trying to install Bioconductor into R, using the code on their website. When I type in the code (see bellow) I get an error message saying that some packages can't be updated, the installation path is unwriteable.
> ## try http:// if https:// URLs are not supported
> source("https://bioconductor.org/biocLite.R")
Bioconductor version 3.4 (BiocInstaller 1.24.0), ?biocLite for help
> biocLite()
BioC_mirror: https://bioconductor.org
Using Bioconductor 3.4 (BiocInstaller 1.24.0), R 3.3.2 (2016-10-31).
installation path not writeable, unable to update packages: Matrix, mgcv,
survival
I can install these package by going to packages/install packages.
> utils:::menuInstallPkgs()
trying URL 'https://www.stats.bris.ac.uk/R/bin/windows/contrib/3.3/Matrix_1.2-8.zip'
Content type 'application/zip' length 2775038 bytes (2.6 MB)
downloaded 2.6 MB
trying URL 'https://www.stats.bris.ac.uk/R/bin/windows/contrib/3.3/mgcv_1.8- 16.zip'
Content type 'application/zip' length 2346257 bytes (2.2 MB)
downloaded 2.2 MB
trying URL 'https://www.stats.bris.ac.uk/R/bin/windows/contrib/3.3/survival_2.40-1.zip'
Content type 'application/zip' length 5109948 bytes (4.9 MB)
downloaded 4.9 MB
package ‘Matrix’ successfully unpacked and MD5 sums checked
package ‘mgcv’ successfully unpacked and MD5 sums checked
package ‘survival’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\stxeb8\AppData\Local\Temp\Rtmp2tQZ4v\downloaded_packages
I can then go to packages/ load packages and load them succesfully and search and see that the packages are there.
> local({pkg <- select.list(sort(.packages(all.available = TRUE)),graphics=TRUE)
+ if(nchar(pkg)) library(pkg, character.only=TRUE)})
Loading required package: nlme
This is mgcv 1.8-16. For overview type 'help("mgcv-package")'.
> local({pkg <- select.list(sort(.packages(all.available = TRUE)),graphics=TRUE)
+ if(nchar(pkg)) library(pkg, character.only=TRUE)})
> local({pkg <- select.list(sort(.packages(all.available = TRUE)),graphics=TRUE)
+ if(nchar(pkg)) library(pkg, character.only=TRUE)})
> local({pkg <- select.list(sort(.packages(all.available = TRUE)),graphics=TRUE)
+ if(nchar(pkg)) library(pkg, character.only=TRUE)})
> search()
[1] ".GlobalEnv" "package:survival" "package:mgcv"
[4] "package:nlme" "package:Matrix" "package:BiocInstaller"
[7] "package:stats" "package:graphics" "package:grDevices"
[10] "package:utils" "package:datasets" "package:methods"
[13] "Autoloads" "package:base"
But then when I go to install bioconductor it gives me the same error message that Matrix, mgcv and survival aren't able to be updated.
> ## try http:// if https:// URLs are not supported
> source("https://bioconductor.org/biocLite.R")
Bioconductor version 3.4 (BiocInstaller 1.24.0), ?biocLite for help
> biocLite()
BioC_mirror: https://bioconductor.org
Using Bioconductor 3.4 (BiocInstaller 1.24.0), R 3.3.2 (2016-10-31).
installation path not writeable, unable to update packages: Matrix, mgcv,
survival
What can I do to be able to update these packages so I can install bioconductor?
In general, I would advise against changing the permission in system folders, because R should work without additional administrative rights.
Thus I would likewise advise against installing packages using administrative rights, as you would be required to do so in the future every time you have to update these packages!
To backtrace this issue and prevent to minimize it in future updates, you should perform the following steps:
Note which packages that fail to update (already shown in the error message).
Locate the folders where all R packages are installed using .libPaths(). This should provide two results, a destination in your home folder and a system folder.
"/home/USER/R/x86_64-pc-linux-gnu-library/X.X" "/usr/lib/R/library"
Install the packages with install.packages(c("PKG1", "PKG2", "PKG3")) or BiocManager::install(c("PKG1", "PKG2", "PKG3"))*
Only If you have administrator rights: Manually remove the older package folders from the system folder ("/usr/lib/R/library"), using administrator rights (sudo) OR enter R with administrator rights One last time and run remove.packages(c("PKG1", "PKG2", "PKG3"), lib = "/usr/lib/R/library").
*If there are issues with the installation path, add the argument , lib = "/home/USER/R/x86_64-pc-linux-gnu-library/X.X" to either of the install functions in step 3. This argument explicitly states to install in your home folder.
There is a single issue with this approach, at least with the official R repository on Arch Linux: Whenever R is updated, the updated version still includes packages in the system folder, that can't be updated without administrative rights. Therefore for each R update, this procedure must be repeated. I'm especially looking at you survival!!!
*Edit: It is important to note that biocLite is no longer the recommended tool for installing BioConductor packages. You should instead use BiocManager, which is in the official CRAN repository (install.packages("BiocManager")).
**Second edit: As this answer still receives votes, I have updated and cleaned up the answer.
It was a permission issue for me. First, I identified where the packages were installed using installed.packages()[, c("Package", "LibPath")]. This outputs a long 2 column matrix with the names and locations of the packages. Then you will see where the offending packages are. In my case, they were at /usr/lib/R/site-library and /usr/lib/R/library. Then I changed the permission of these folders by chmod (I used chmod -R 777 on the main R folder, this is my personal computer, so security is not a big concern here I think).
If you are running R/Rstudio on Windows, then just open R/Rstudio as administrator. Right click on the icon then run as administrator
It looks like several 'recommended' packages are installed in two places -- maybe by an administrator account in a directory you don't have write access to, and then by RStudio in a directory where you do have write access. biocLite() is complaining about the former.
Unless biocLite() complains about a Bioconductor package that cannot be installed (different from cannot be updated), there is no problem and basic Bioconductor packages have been successfully installed. Check out https://support.bioconductor.org for future Bioconductor-related support.
One solution is to open a terminal and load R using admin rights
sudo R
update.packages()
source("https://bioconductor.org/biocLite.R")
biocLite()
Then you can update. But careful. This can create packages by the admin in a directory supposed to be owned by a user.
In this case, instead of loading R as root (which is solving the problem until the next update), check the .libPaths(). You will have a list of directories.
.libPaths()
"/home/it_s_me/R/x86_64-pc-linux-gnu-library/3.4" "/usr/lib/R/library"
In my case, all packages in "/usr/lib/R/library" are owned by root, and all except one are owned by a normal user (not root) at "/home/itsame/R/x86_64-pc-linux-gnu-library/3.4".
If you have admin rights, an easy solution may be to run chown in all the places: For example, I had trouble updating the curl package. I used:
sudo chown -R it_s_me /home/it_s_me/R/x86_64-pc-linux-gnu-library/3.4/curl/
I had a similar problem with other Bioconductor packages and the solution was simpler than I thought. I had to install some OS packages pointed out on my log: libcurl-dev, libcurl4-openssl-dev, libssl-dev.
My advice is to check your R log and look for "Configuration failed because [PACKAGE] was not found. Try installing".
Since with the version of R 3.6.1 the script http://bioconductor.org/biocLite.R returns this message " Error: With R version 3.5 or greater, install Bioconductor packages using BiocManager; see https://bioconductor.org/install " I solved the problem with the following steps:
Get the list of directories used by R to install libraries and
choose the one with write permissions using: .libPaths()
Installing the "BiocManager" library using: install.packages("BiocManager")
Installation of the library "bioconductor" by forcing the directory
with write permissions using: BiocManager::install("Rgraphviz", lib = "C:/Users/tizbet/Documents/R/win-library/3.6")
I worked on the following R installation:
platform x86_64-w64-mingw32, arch x86_64, os mingw32, system x86_64, mingw32, version.string R version 3.6.1 (2019-07-05)
I was inspired by the Kasper Thystrup Karstensen answer
Whenever you want to work on an R-code where packages need to be installed: -
Windows: Open R-studio as 'Run as administrator' [Right click on the icon]
Linux: Run as sudo user; else get writing rights from the administrator
Ubuntu:
sudo apt install libcurl-dev
Fedora:
sudo dnf install libcurl-devel

Error: "Package not available" for a package that should be current [duplicate]

I have been using the "stats" package in R 3.0.1 without any problems . But today i deleted the "stats" folder from the R library location and now I can't install it any more. I tried doing the same thing with other packages but I could install everything except "stats"
install.packages("stats", dep = TRUE, repos="http://cran.cs.wwu.edu")
## Installing package into ‘%Default R Lib Installation Path%’
## (as ‘lib’ is unspecified)
## Warning in install.packages : package ‘stats’ is not available (for R version 3.0.1)
I also tried downloading it from other sources like "http://cran.ma.imperial.ac.uk/" but nothing works . Any ideas?
(Since SO is nagging me not to continue the comment thread, I will post an answer.)
I believe that this problem will apply to any base package (but not to those installed from repositories, and probably not to Recommended packages): I am deeply skeptical that stats is the only package. It should occur for any of the packages in this list:
rownames(subset(as.data.frame(installed.packages()),Priority=="base"))
I'm sure it's theoretically possible to re-install a base package from scratch, but it will be much easier to re-install R. At a guess, it would take me about 15 minutes to re-install R, and I would feel lucky if I figured out how to re-install a base package on its own in less than an hour.
I'm pretty sure that re-installing R will not affect previously-installed packages: see e.g. http://cran.r-project.org/bin/windows/base/rw-FAQ.html#How-do-I-UNinstall-R_003f; that link is about uninstalling rather than re-installing, but this seems relevant:
Uninstalling R only removes files from the initial installation, not (for example) packages you have installed or updated.
Other choices would be
create another identical installation of R (on another machine, or in a non-default location); locate the relevant directories in your new installation and copy them over to your existing installation.
restore the relevant directories from your backup.
PS: obviously if you are doing this on a client's machine it would be a good idea to test my advice first ...
What worked for me was to copy the entire package folder from another to my R.home() directory.

How do I reinstall a base-R package (e.g., stats, graphics, utils, etc.)?

I have been using the "stats" package in R 3.0.1 without any problems . But today i deleted the "stats" folder from the R library location and now I can't install it any more. I tried doing the same thing with other packages but I could install everything except "stats"
install.packages("stats", dep = TRUE, repos="http://cran.cs.wwu.edu")
## Installing package into ‘%Default R Lib Installation Path%’
## (as ‘lib’ is unspecified)
## Warning in install.packages : package ‘stats’ is not available (for R version 3.0.1)
I also tried downloading it from other sources like "http://cran.ma.imperial.ac.uk/" but nothing works . Any ideas?
(Since SO is nagging me not to continue the comment thread, I will post an answer.)
I believe that this problem will apply to any base package (but not to those installed from repositories, and probably not to Recommended packages): I am deeply skeptical that stats is the only package. It should occur for any of the packages in this list:
rownames(subset(as.data.frame(installed.packages()),Priority=="base"))
I'm sure it's theoretically possible to re-install a base package from scratch, but it will be much easier to re-install R. At a guess, it would take me about 15 minutes to re-install R, and I would feel lucky if I figured out how to re-install a base package on its own in less than an hour.
I'm pretty sure that re-installing R will not affect previously-installed packages: see e.g. http://cran.r-project.org/bin/windows/base/rw-FAQ.html#How-do-I-UNinstall-R_003f; that link is about uninstalling rather than re-installing, but this seems relevant:
Uninstalling R only removes files from the initial installation, not (for example) packages you have installed or updated.
Other choices would be
create another identical installation of R (on another machine, or in a non-default location); locate the relevant directories in your new installation and copy them over to your existing installation.
restore the relevant directories from your backup.
PS: obviously if you are doing this on a client's machine it would be a good idea to test my advice first ...
What worked for me was to copy the entire package folder from another to my R.home() directory.

Resources