yihui / xfun

Miscellaneous R functions
https://yihui.org/xfun/
Other
135 stars 28 forks source link

xfun does not install when already loaded #58

Closed bersbersbers closed 2 years ago

bersbersbers commented 2 years ago

By filing an issue to this repo, I promise that

I understand that my issue may be closed if I don't fulfill my promises.

Here we go.

My .Rprofile:

.version <- paste0(version$major, ".", version$minor)
.libPaths(paste0("/data2/bers/opt/R/", substr(.version, 1, 3), "/library"))
options(repos = c(CRAN = "https://cloud.r-project.org/"))
knitr::opts_knit$set(progress = FALSE)

Then, R -e 'install.packages("xfun")' fails:

> install.packages("xfun")
Installing package into ‘/data2/bers/opt/R/4.1/library’
(as ‘lib’ is unspecified)
trying URL 'https://cloud.r-project.org/src/contrib/xfun_0.27.tar.gz'
Content type 'application/x-gzip' length 118918 bytes (116 KB)
==================================================
downloaded 116 KB

* installing *source* package ‘xfun’ ...
** package ‘xfun’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
gcc -I"/usr/lib64/R/include" -DNDEBUG   -I/usr/local/include   -fpic  -fmessage-length=0 -grecord-gcc-switches -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection  -c base64.c -o base64.o
gcc -I"/usr/lib64/R/include" -DNDEBUG   -I/usr/local/include   -fpic  -fmessage-length=0 -grecord-gcc-switches -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection  -c init.c -o init.o
g++ -std=gnu++11 -shared -L/usr/lib64/R/lib -L/usr/local/lib64 -o xfun.so base64.o init.o -L/usr/lib64/R/lib -lR
installing to /data2/bers/opt/R/4.1/library/00LOCK-xfun/00new/xfun/libs
** R
** inst
** byte-compile and prepare package for lazy loading
Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) : 
  there is no package called ‘xfun’
Calls: loadNamespace ... loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart
Execution halted
ERROR: lazy loading failed for package ‘xfun’
* removing ‘/data2/bers/opt/R/4.1/library/xfun’
* restoring previous ‘/data2/bers/opt/R/4.1/library/xfun’

The downloaded source packages are in
        ‘/tmp/RtmpvkZEql/downloaded_packages’
Warning message:
In install.packages("xfun") :
  installation of package ‘xfun’ had non-zero exit status
> 
> 

Commenting out knitr::opts_knit$set(progress = FALSE) fixed the issue.

cderv commented 2 years ago

If you set such code in your .Rprofile

knitr::opts_knit$set(progress = FALSE)

It will load knitr package in each R Session, which means that its dependencies will also be loaded. and xfun is one of them. When a package is loaded, it could cause issue when installing it or its dependency in the same session.

I would suggest not doing this - it can definitely cause issue like this one when updating in a non vanilla session.

If you want to set a knitr option globally for all your session, know that there is another way described in ?knitr::opts_knit: You can use option. So doing

options(knitr.package.progress = FALSE)

would work.

By doing this, knitr won't be loaded.

Hope it helps

bersbersbers commented 2 years ago

Thanks @cderv, that helps a lot on many levels. Here's a few more details:

I am not sure if there is anything xfun can and wants to do - if not, feel free to close this.

cderv commented 2 years ago

Yes I know it is not something specific to .Rprofile - it just amplifies. You will have the issue as soon as you are trying to install a package that is already loaded in the session. So it is best to always start in a fresh session to install packages. Usually that is what you do.

In gets pretty weird on windows too.

The solution to help with this is the pak package https://github.com/r-lib/pak It will try to be clever about loading and unloading package, and also detect the potential conflicts.

Anyway, it is not something to solve on a package basis. It is just the way R works - Good practice is to install / update only in a fresh session. But pak helps me a lot now with all this