Unloading and removing a loaded package withouth restarting R - r

Actual question
Can I unload and remove a package without having to restart an R process that has already required/loaded the package?
I've had a look at this question/answer, but it only deals with part of the problem (unloading)
Background
The reason I want to do this is that I am working on a project-based framework for package development in which I'm also using project-based package libraries in order to keep the standard library "clean" and allow for systematic testing of package dependencies.
The only problem with this is that it's really hard for the user to remember the path to a project-based package library before initializing the project (which also loads all specified package dependencies via require() and makes the project-based library the first one in .libPaths())
Practically, this means that a user currently can't really uninstall a package dependency from the project-based package library in a comfortable way (after initializing the project, that is) and I'm looking for ways to change this.
What I tried
install.packages("forecast")
require("forecast")
detach("package:forecast", unload=TRUE)
remove.packages("forecast")
> install.packages("forecast")
Warning: unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/2.15
trying URL 'http://cran.rstudio.com/bin/windows/contrib/2.15/forecast_4.03.zip'
Content type 'application/zip' length 1124001 bytes (1.1 Mb)
opened URL
downloaded 1.1 Mb
package 'forecast' successfully unpacked and MD5 sums checked
Warning: cannot remove prior installation of package 'forecast'
The downloaded binary packages are in
C:\Users\wwa418\AppData\Local\Temp\RtmpYPry89\downloaded_packages
The problem seems to be /forecast/libs/i386/forecast.dll which probably gets registered upon loading and seems to be a tough one to kill ;-)
Any ideas?

Related

Trouble installing and opening “MAMI” package in R

I’ve downloaded the “MAMI” package in R but even though it’s downloaded, when I go to open it from my library, it tells me “Error in library(MAMI): there is no package called ‘MAMI.’” I had trouble downloading it as well - when I tried to download it with just “install.packages(“MAMI”)” R kept telling me “package ‘MAMI’ is not available for this version of R” so I went to the developer’s website (https://mami.r-forge.r-project.org/) and just downloaded it using the installation code there. Can someone tell me how to either redownload it in a way that will work or how to open it from my library?

problems with installing r packages on windows 10

I'm having problems installing r packages using R3.4.1 on windows 10.
E.g. I get the following message when trying to install a package:
package ‘ctv’ successfully unpacked and MD5 sums checked
Warning in install.packages :
unable to move temporary installation ‘C:\Program Files\R\R-3.4.1\library\file34470c9f23\ctv’ to ‘C:\Program Files\R\R-3.4.1\library\ctv’
I would really appreciate any assistance to resolve this issue.
Normally one puts the library in C:\user\yourname\Documents\R\win-library. Each version of R will have its library in a separate folder within that folder. In that case you won't get this error. If you reinstall R and accept the default for the library location then it will put the library there and you won't get this error.
If you prefer the configuration you have now because, for example, there are multiple users on your PC that need to share the library, then you will need to be Administrator each time you install a package; however, note that your current configuration is less secure than the default.

"Error in library(rjson): There is no package called rjson"

My rjson package randomly doesn't work. As in, it works fine sometimes, sometimes it fails to load. Not sure why.
I get this error.
Error in library("rjson") : there is no package called ‘rjson’
To try and alleviate this, despite knowing its installed, I added an install line in my script.
install.packages("rjson", repos="http://cran.rstudio.com/")
library(rjson)
Now I get....
Installing package(s) into ‘C:/Users/Tom/Documents/R/win-library/2.15’
(as ‘lib’ is unspecified)
trying URL 'http://cran.rstudio.com/bin/windows/contrib/2.15/rjson_0.2.13.zip'
Content type 'application/zip' length 491848 bytes (480 Kb)
opened URL
downloaded 480 Kb
package ‘rjson’ successfully unpacked and MD5 sums checked
Warning: cannot remove prior installation of package ‘rjson’
The downloaded binary packages are in
C:\Users\Tom\AppData\Local\Temp\RtmpiOfTqK\downloaded_packages
In R, when I go to "Packages --> Load" for some reason rjson is NOT there. It never has been, even when it worked.
I've naviaged to...
C:\Users\Tom\Documents\R\win-library\2.15
I can confirm the folder for rjson is there.
No idea what to do.
This has happened to me quite a few times. It usually happens when you try to install a newer version of an already installed package (although it can happen in other more rare occasions).
The solution I have found so far is to go back to your library path i.e. the location on your machine where the package is installed (C:\Users\user_name\Documents\R\win-library\R_version is the default path on Windows) delete the corresponding package folder and then re-install the package as usual using:
install.packages('rjson')
And this way it should work.
Or you could even do it programmatically as per #Thomas 's comment:
#get list of installed packages
inst_packages <- installed.packages()
if ("rjson" %in% inst_packages[, 1]) {
#uninstalls package
remove.packages("rjson")
#re-installs package
install.packages("rjson")
}
or even better just use:
if ("rjson" %in% inst_packages[, 1]) update.packages("rjson")

R not finding package even after package installation

I have always worked with the zoo package, that I have installed a long time ago. Today, I created a new R script, and ran library(zoo) and got the following error:
> library(zoo)
Error in library(zoo) : there is no package called ‘zoo’
Odd.. Still, I tried reinstalling the package using install.packages("zoo"), and get the following:
> install.packages("zoo")
Installing package(s) into ‘C:/Users/U122337.BOSTONADVISORS/Documents/R/win-library/2.15’
(as ‘lib’ is unspecified)
--- Please select a CRAN mirror for use in this session ---
trying URL 'http://cran.cnr.Berkeley.edu/bin/windows/contrib/2.15/zoo_1.7-10.zip'
Content type 'application/zip' length 874474 bytes (853 Kb)
opened URL
downloaded 853 Kb
package ‘zoo’ successfully unpacked and MD5 sums checked
Warning: cannot remove prior installation of package ‘zoo’
The downloaded binary packages are in
C:\Users\U122337.BOSTONADVISORS\AppData\Local\Temp\Rtmp404t8Y\downloaded_packages
When I try uploading the package, I get the error again that the zoo package doesn't exist.
I have no idea what's happening. I exited the GUI and restarted it, same problem. I have always worked with this package, and I have no idea why this is happening now.
Do .libPaths(), close every R runing, check in the first directory, remove the zoo package restart R and install zoo again. Of course you need to have sufficient rights.
When you run
install.packages("whatever")
you got message that your binaries are downloaded into temporary location (e.g. The downloaded binary packages are in
C:\Users\User_name\AppData\Local\Temp\RtmpC6Y8Yv\downloaded_packages ). Go there. Take binaries (zip file). Copy paste into location which you get from running the code:
.libPaths()
If libPaths shows 2 locations, then paste into second one. Load library:
library(whatever)
Fixed.
I had this problem and the issue was that I had the package loaded in another R instance. Simply closing all R instances and installing on a fresh instance allowed for the package to be installed.
Generally, you can also install if every remaining instance has never loaded the package as well (even if it installed an old version).
So the package will be downloaded in a temp folder C:\Users\U122337.BOSTONADVISORS\AppData\Local\Temp\Rtmp404t8Y\downloaded_packages from where it will be installed into your library folder, e.g. C:\R\library\zoo
What you have to do once install command is done: Open Packages menu -> Load package...
You will see your package on the list. You can automate this:
How to load packages in R automatically?
go to this folder C:\Users\hp\Documents\R\win-library\4.0 in your PC.
And delete '00LOCK' named folder.
then It will work properly, THis folder is being locked all incoming file.IF your delete this , then it will work.

Possible to override the blocking of a package's (re-)installation after it has been required/loaded?

Actual question
Is it possible to override the blocking of a package's (re-)installation after it has been required/loaded?
I understand that blocking a "true" re-installation to the same library that the package was loaded from makes perfect sense once the package is in use. But my use case is a bit different
Background
I like the idea of having a "sandbox library" to test own packages. Besides the package(s) to test, those sandbox libraries contain all base packages of a clean base R installation plus some contrib packages necessary in order for my whole package building and testing framework to work (e.g. digest, stringr etc.). However, my framework loads the latter packages from the standard lib and then needs to install them to the sandbox lib - which is blocked. Hence my question if it is possible to override this as I'm not really doing what people had in mind when implementing the block.
Illustration
Specify paths to the two libraries
lib <- file.path(R.home(), "library")
lib.sandbox <- file.path(tempdir(), "library")
Create sandbox library
dir.create(lib.sandbox, showWarnings=FALSE)
Install a package to the standard library and load it
install.packages("digest", lib=lib)
require("digest", lib.loc=lib)
Then also install it to the sandbox library
> install.packages("digest", lib=lib.sandbox)
Warning: package 'digest' is in use and will not be installed
My framework figures certain stuff out after certain packages that should also be installed to the sandbox lib are loaded, so I can't put the "install-to-sandbox-lib" step before the initial loading step.
Here's a general version of what you suggest in your answer. This will unload the package before installing and reload the package from the same location afterward.
install.packages.sandbox <- function(pkgs, lib, repos=getOption("repos"), ...) {
if (is.null(repos))
stop("Can't install from source. Need package name.")
pkg.pos <- grep(pkgs, search())
pkg.path <- searchpaths()[grep(pkgs, searchpaths())]
in.use <- length(pkg.pos) > 0
# detach
if (in.use) do.call(detach,
list(pkg.pos),
envir=.GlobalEnv)
# install
utils::install.packages(pkgs, lib, repos, ...)
# re-attach from original library location
if (in.use) library(pkgs,
character.only=TRUE,
lib.loc=.libPaths()[sapply(.libPaths(), grepl, pkg.path)])
}
Not what I was after conceptually, but a pragmatic workaround: detaching the package right before installing it
Specify paths to the two libraries
lib <- file.path(R.home(), "library")
lib.sandbox <- file.path(tempdir(), "library")
Create sandbox library
dir.create(lib.sandbox, showWarnings=FALSE)
Install a package to the standard library and load it
install.packages("digest", lib=lib)
require("digest", lib.loc=lib)
Detach package
detach(package:digest)
Then also install it to the sandbox library
> install.packages("digest", lib=lib.sandbox)
trying URL 'http://cran.at.r-project.org/bin/windows/contrib/2.15/digest_0.5.2.zip'
Content type 'application/zip' length 79053 bytes (77 Kb)
opened URL
downloaded 77 Kb
package 'digest' successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\wwa418\AppData\Local\Temp\Rtmp6XSVYq\downloaded_packages
On top of the already mentioned search path altering approach, it may be worthwhile noting that this warning and behavior comes (at least as of R 3.6.2) from utils:::.install.winbinary, where the check against attached packages is made.
What we therefore can do alternatively to circumvent this issue is to instead of using the binary installation method, install the package from source:
library(digest)
utils::install.packages("digest", lib = tempdir(), type = "source")
This of course comes with some potential caveats if the package requires compilation of C/C++/Fortran code, as written in help("install.packages") for Windows:
Using packages with type = "source" always works provided the package
contains no C/C++/Fortran code that needs compilation. Otherwise, you
will need to have installed the Rtools collection as described in the
‘R for Windows FAQ’ and you must have the PATH environment variable
set up as required by Rtools.
...

Resources