updating package in R: `update.packages` vs. `install.packages` - r

I've tried to load the party library and got the following error:
Loading required package: zoo
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) :
namespace ‘lattice’ 0.20-24 is already loaded, but >= 0.20.27 is required
Error: package ‘zoo’ could not be loaded
So I decided to update all packages within the same session (detach all packages while working in R), including lattice, hoping that zoo and then party would then load correctly once lattice is updated:
pkgs <- names( sessionInfo()$otherPkgs )
pkgs <- paste('package:', pkgs, sep = "")
lapply( pkgs , detach, character.only = TRUE, unload = TRUE)
update.packages(checkBuilt=TRUE, ask=FALSE,
repos="http://r-forge.r-project.org",
oldPkgs=c("lattice","zoo","party")
)
It didn't work (within the same session and after re-starting without preloading .RData):
> library(party)
Loading required package: zoo
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) :
namespace ‘lattice’ 0.20-24 is already loaded, but >= 0.20.27 is required
Error: package ‘zoo’ could not be loaded
According to How to update R2jags in R? it's best to simply run install.packages on those packages I want to update, then restart. And indeed it did the trick.
So here's the question: when is update.packages called for, given that updating within a running session is fragile to say the least, and install.package will do the trick at the cost of restarting the session? What bit of R package management voodoo am I missing? Thanks.

Dirk offers a more general strategy to avoid this issue. However, if you are in an interactive session that you don't feel like rebooting, and you want to unload a package that needs updating (which neither detach(.)-ing orupdate.packages(.)-ing effectively accomplishes), then there is a function, unloadNamespace that usually works for me. There are warnings in its help page that say it's not entirely safe, but I have not had difficulties. Try:
unloadNamespace("lattice") # or lapply()-ing as you attempted with `detach`
update.packages("lattice")
require(lattice) # or library()

This is yet another reason why I prefer to launch both the "install" and "update" operations outside of my current, working R session.
By using the command-line, I get fresh R sessions without loaded packages, and the issue you experienced here does not arise. And as a shortcut, I define scripts update.r and install.r using littler (and included in the examples/ directory of that package) but you can of course do the same via Rscript.

Related

Why does my script work in RStudio but not when scheduled?

I wrote some scripts, which I want to run periodically in the background. They work if I start them manually in RStudio. With scheduled scripts, I get an error, if I have a one, which loads some packages. With lubridate, it works fine. With readr or dplyr I get an error:
Fehler: package or namespace load failed for 'readr' in
loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck =
vI[[j]]): es gibt kein Paket namens 'fansi' Ausf?hrung angehalten
The same script works from scheduler and in RStudio, if I don't load library(readr), without any more modifications. Fansi is installed.
I also saw an error for tzdb. I think, some packages uses some other basic packages (fansi, tzdb) in an incorrect manner.
The problem is, that R uses other library-pathes than RStudio. I saw it, with print(.libPaths()) in scheduler environment and in dektop environment. I didn´t find the place to change the path in R itself. But I choosed one of the library-paths from R in RStudio. Now everything works. Maybe, someone has a better solution.

Tidy Verse Issue

I am using tidyverse for the first time and I am very new to R and Rstudio.
I installed the tidyverse package in Rstudio and continued with my code.
My code until now is just this:
library (tidyverse)
damsel <- read.csv("mecistogaster_in_bromeliads_multiyear.csv")
damsel %>% filter (genus == "Weruhaia")
When I piped, it gave me the error saying that object 'genus' is not found.
When I looked into my packages tidyverse was not ticked-- So I tried to again reload it by typing library (tidyverse).
It showed me the following error 'Error: package or namespace load failed for ‘dplyr’ in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]): there is no package called ‘pillar’'
I have tried to restart R three times since this issue and all the three times I get the same error.
How do I get this to work?
Did you run
Install.packages(“tidyverse”)
cderv wrote on community.studio.com:
install.packages("tidyverse", dependencies = TRUE) to install everything tidyverse depends on.
Once installed.
library(tidyverse)
This may be the blind leading the blind, but... Have you successfully installed any packages? If not, there could be an issue of where your packages are installing.

R tells me I'm missing a dependency package when I'm not?

I have a handful of R scripts that I routinely run every day so all of the necessary packages have been running just fine for months. Yesterday I installed the RInno package and I believe this has somehow affected other packages because my routine scripts no longer run due to the inability to load my normal packages.
When troubleshooting I started with the odbc package. When trying to load it gives me:
Error: package or namespace load failed for ‘odbc’ in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):
there is no package called ‘Rcpp’
I then ran .libPaths(), checked both of the returned paths, and the Rcpp package existed in both.
I've even tried re-installing both odbc and Rcpp with no luck. Does anyone know what could be going on here?
Update1: While trying to re-install Rcpp did not work, re-installing dplyr did manage to fix the missing rlang issue. So I decided to go download Rcpp directly from https://cran.r-project.org/web/packages/Rcpp/index.html and then paste it manually into both of my library folders. This seems to have fixed the Rcpp issue but I am now met with the following new error when trying to load odbc or run devtools::session_info():
Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : object 'vI' not found
Update2: Ultimately I decided to do a restore on my two library folders to a time before I installed RInno and it seems to have worked. I still have no idea what RInno did but I won't be trying that again.

"Rcmdr" error load failed for ‘car’ in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):

I'm attempting to run 'Rcmdr' in R. I had it fully functional and accidentally exited the program. Now i can not get back in.
I've tried a number of things;
uninstall and reinstalling R
uninstall and reinstalling 'Rcmdr'
I tried manually installing
Trying different 'CRAN's
and all of the suggestions i saw from previous posts. (i.e.(install.packages("car",dependencies=TRUE)
Below is the error I'm receiving.
The downloaded binary packages are in C:\Users\william\AppData\Local\Temp\Rtmpuazyss\downloaded_packages
> library(Rcmdr)
Loading required package: RcmdrMisc
Loading required package: car
Error: package or namespace load failed for ‘car’ in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):
there is no package called ‘openxlsx’
Error: package ‘car’ could not be loaded
A common cause of errors like this is that you are working on a system where you don't have write permission on the R package library. You install a package (likely openxlsx in this case), and since it can't write to the system library, it creates a private library for you somewhere where you do have write permission.
But then when you start a new session, it doesn't look in that location, so it thinks openxlsx is not installed, and you get an error like the one you saw.
A short term fix is to just re-install the missing package. But then you'll get the same error in the next session if you restart R.
A better fix is to tell R to look in your private library. You can find where it is after re-installing openxlsx by running
.libPaths()
The first entry in the result will probably be your private library, the second entry will be the standard system one. (You could have more than 2, but that's not likely unless you've already asked for more.) To make sure that your private library always shows up, you need to put the line
.libPaths("whatever was in the first entry")
into a file called .Rprofile in your home directory. For example, I see
> .libPaths()
[1] "/Users/me/R/contrib"
[2] "/Library/Frameworks/R.framework/Versions/3.5/Resources/library"
so I should have
.libPaths("/Users/me/R/contrib")
in my .Rprofile. You'll see some other directory there, use it.

Deleted httr, now can't reinstall, or load devtools

I was trying to install twitteR, but the install failed, saying I needed httr >0.6 and I only had 0.3.
I tried to update httr. However, I seemed to have to 2 versions simultaneously, and when I used library(twitteR) it still said I had httr 0.3 and needed 0.6 (sorry I don't have the exact error messages anymore, and can't replicate due to the problems below.)
So, I deleted httr with remove.packages('httr') (this was a mistake).
Now I cannot load devtools
> library(devtools)
Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) :
there is no package called ‘httr’
Error: package or namespace load failed for ‘devtools’
or install packages
install.packages('httr')
'''Some lines skipped'''
Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) :
there is no package called ‘httr’
Error: package or namespace load failed for ‘devtools’
Execution halted
I get the same error message as above when trying to install httr from a tarball with R CMD INSTALL.
I'm pretty stuck. Is it time for a clean R install?
And for future reference, how do I update httr?
My best guess -- you have something in your ~/.Rprofile that's attempting to load httr (or a package that uses httr) -- since surely, nothing in base R relies on httr.
I believe install.packages() launches an R sub-process (using the same arguments on load as the current session), which in turn will source that ~/.Rprofile, and cause the errors you are seeing.
If I am correct, one solution would be to move / comment out your ~/.Rprofile and restore it after re-installing httr. Alternatively, launching R with the --vanilla flag (to suppress any site / profile loading) should also help in alleviating the problem.
Running R as R --vanilla fixed the problem.
I could then install httr with
install.packages('httr')
And then loading devtools works and installing twitteR then works as well.

Resources