How force R to load package, `library()` fails due to dependency? - r

How force R to load package when some (not crucial) packages it depends on aren't installed ?
Motivation : sometimes I have to use R in places, where I can't automatically install required packages. Doing so manually is very time consuming and in most cases I need only a vary small part of functions contained in installed package.
Typical message error in this case is :
> library(packageX)
Loading required package: packageY
Error: package ‘packageY’ could not be loaded
In addition: Warning messages:
In library(pkg, character.only = TRUE, logical.return = TRUE, lib.loc = lib.loc) : there is no package called ‘packageY’
Mayby 'devtools' package will be helpful. But I haven't investigate it.

If you cannot install the dependencies, I think your only option is to remove those packages from the Depends field of DESCRIPTION file of packageX and try to rebuild packageX

You can look at the source of the package to figure out which functions you need and what dependencies they need. You may want to collaborate with the author/maintainer of the package on this part. You could create a private version of the package that does not have the dependencies and the other functionality that you don't need.
If this is only for your personal use and the license for the package allows it (gpl and similar) then you don't need the authors permission to extract those pieces you want. If you want to link your package to this for distribution then you should work with the original author. I know a couple of package authors wanted just a couple of functions from one of my packages and I agreed that loading my entire package (and dependencies) was overkill for what they wanted to do, so I worked with them and they have copies of the functions in their package without needing to depend on mine. When I update one of the functions I send a copy to them to update their copy as well.

Related

Facing trouble in loading packages in R studio [duplicate]

I updated R using installr and asked to copy my library. (Even though I've never had this work, I keep hoping maybe it will someday as the package gets updated.) Opened up RStudio and, as usual, it can't find any of the packages that were in my old library. I next copied all the files from my old library into my new library, and several critical ones still don't work. For example, when I tried to load tidyverse, the message I receive is:
Error: package or namespace load failed for ‘tidyverse’: package ‘lazyeval’ was installed by an R version with different internals; it needs to be reinstalled for use with this R version.
I tried remove.packages("tidyverse") and then install.packages("tidyverse", dependencies = TRUE), and I still get the same message about the package lazyeval. I tried installing lazyeval, but then, when I go through those same steps again to try to use tidyverse, now it's lubridate that was installed by an R version with different internals.
How can I avoid this incredibly irritating and time-sucking iterative process of trying to install a package, seeing that the internals are wrong for some dependent package, installing whatever package was the problem, installing the package I actually care about, and then figuring out what other dependencies are problems?
The tidyverse package hardly contains anything: its main purpose is to get a bunch of other packages loaded. Removing it and updating it won't help update the other packages. You could do what #phago29 suggested in their comment, but an easier way is just to update everything. Run this command in an R session:
update.packages(ask = FALSE, checkBuilt = TRUE)
A few notes:
If you have admin capabilities, run as admin, and it will update your main library. If you don't, it'll install new copies in your user account.
Even though you're saying ask = FALSE, it may ask some questions about whether you can build packages from source. Answer "Yes" if your system is set up to do that, "No" if not. If you're not sure, try "Yes", and if you get install failures, run it again and say "No".)
This is likely to run for a while, so go away and have a coffee or something.

Updated R, copied library, still can't find packages, and can't seem to install all dependencies

I updated R using installr and asked to copy my library. (Even though I've never had this work, I keep hoping maybe it will someday as the package gets updated.) Opened up RStudio and, as usual, it can't find any of the packages that were in my old library. I next copied all the files from my old library into my new library, and several critical ones still don't work. For example, when I tried to load tidyverse, the message I receive is:
Error: package or namespace load failed for ‘tidyverse’: package ‘lazyeval’ was installed by an R version with different internals; it needs to be reinstalled for use with this R version.
I tried remove.packages("tidyverse") and then install.packages("tidyverse", dependencies = TRUE), and I still get the same message about the package lazyeval. I tried installing lazyeval, but then, when I go through those same steps again to try to use tidyverse, now it's lubridate that was installed by an R version with different internals.
How can I avoid this incredibly irritating and time-sucking iterative process of trying to install a package, seeing that the internals are wrong for some dependent package, installing whatever package was the problem, installing the package I actually care about, and then figuring out what other dependencies are problems?
The tidyverse package hardly contains anything: its main purpose is to get a bunch of other packages loaded. Removing it and updating it won't help update the other packages. You could do what #phago29 suggested in their comment, but an easier way is just to update everything. Run this command in an R session:
update.packages(ask = FALSE, checkBuilt = TRUE)
A few notes:
If you have admin capabilities, run as admin, and it will update your main library. If you don't, it'll install new copies in your user account.
Even though you're saying ask = FALSE, it may ask some questions about whether you can build packages from source. Answer "Yes" if your system is set up to do that, "No" if not. If you're not sure, try "Yes", and if you get install failures, run it again and say "No".)
This is likely to run for a while, so go away and have a coffee or something.

Why do I get Error when calling library (rdd)

I have installed the rdd package, when calling library(rdd), I get error an message:
Error: package ‘car’ required by ‘AER’ could not be found"
I have used functions from car (like anova) so I know I have it.
I have MAC OS 10.15.1, R 3.6.1 , I reinstalled R and RStudio.
install.packages("rdd")
library(rdd)
Loading required package: AER
Error: package ‘car’ required by ‘AER’ could not be found
Packages may have dependencies (i.e. other packages) and those dependent package may have dependencies of their very own. Appears that rdd requires AER which in turn requires car. So you need all three (and possibly even more). At the very least you need to now install pkg:car.
(I know this may seem a duplicate but I'm not sure the usual duplicate nominees have seen a multi-level dependency situation such as this. )
Suggest you execute both of this lines:
install.packages("AER", dependencies=TRUE) # should pick up car
# maybe also need install.packages("car", dependencies=TRUE)
install.packages("rdd", dependencies=TRUE)
If you get further errors, pay attention to the first error message and use install package recursively until you establish a full set of dependencies and dependencies of dependencies.

Addressing the "cannot remove prior installation of package" error when updating multiple packages at once

This is a problem that comes up repeatedly, e.g.
Error in install.packages : cannot remove prior installation of package ‘DBI’
Warning in install.packages : cannot remove prior installation of package ‘data.table’
Unable to install package forecast in R
Install error of package plyr
Unable to install package zoo
I have come across this issue multiple times. Most annoying is when it occurs with a bunch of packages; e.g. I do something like
update.packages(ask = FALSE)
and get a number of these errors. The error is misleading though, because R tends to actually remove the old package version---but then doesn't install the new version. Once this happens, you can't fix it with update.packages; you have to reinstall it from scratch.
The most frustrating aspect of this problem is that it often occurs when installing dependencies, so e.g. I might successfully update dplyr, but in the process Rcpp is removed. Now I can't load dplyr because Rcpp is missing, so I need to reinstall Rcpp.
My current workaround (when the number of uninstalled packages is large) is to close all of my R sessions, open a new one with the base R GUI (i.e. not RStudio or RTVS) and do
lapply(dir(.libPaths()), install.packages)
which reinstalls my entire library. This is overkill.
Is there a better way to check which folders in .libPaths() actually contain packages, so that I only reinstall the missing packages?
Alternatively: Is there a better way to check for missing package depedencies?
One strategy is to do
setdiff(dir(.libPaths()), .packages(all = TRUE))
To get a list of directories in the library that don't seem to actually contain packages. Now I have a much smaller list of packages to reinstall.
I'm not sure if it's foolproof, but it's better than reinstalling my entire library.

Including libraries in a package or main Script

I am building a package for the first time.
The package needs couple of libraries to work. Should I include those libraries in the package for every function? or should I include them in my main Script?
In the DESCRIPTION file in your package you can list the packages your package depends on. This will allow you to use the code from those packages to be used anywhere in your package. So, there is no need for explicit use of library or require. When your package is loaded, the other packages will also be loaded. In addition, when setting dependencies = TRUE in install.packages the packages your package depends on will also be installed (if available on CRAN).

Resources