rrstudiorcpp

How can I re-install a R package which uses Rcpp?


In brief: I never faced the problem that I can install a package, but I am not able to re-install the unchanged package. How can I solve that problem? It only affects a package which uses Rcpp - all other packages are fine. Thanks a lot.

I followed the steps outlined at the RStudio website (basically just create a new package using -> New project -> New directory -> R package using Rcpp)

enter image description here

and everything worked. (I just try to make a package using Rcpp work, see this other question.). So

library(testRcpp5)
rcpp_hello_world()
[[1]]
[1] "foo" "bar"

[[2]]
[1] 0 1

However, if I do a second "Clean and Install" or "Install package", the following error occurs:

==> Rcpp::compileAttributes()

  • Updated R/RcppExports.R

==> Rcmd.exe INSTALL --no-multiarch --with-keep.source testRcpp5

  • installing to library 'C:/R/R-4.2.2/library'
  • installing source package 'testRcpp5' ... ERROR: cannot remove earlier installation, is it in use?
  • removing 'C:/R/R-4.2.2/library/testRcpp5'
  • restoring previous 'C:/R/R-4.2.2/library/testRcpp5' Warnung in file.copy(lp, dirname(pkgdir), recursive = TRUE, copy.date = TRUE)
    Problem C:\R\R-4.2.2\library\00LOCK-testRcpp5\testRcpp5\libs\x64\testRcpp5.dll nach C:\R\R-4.2.2\library\testRcpp5\libs\x64\testRcpp5.dll zu kopieren: Permission denied

Exited with status 1.

I just realized, that the whole project folder is read-only after Rcmd.exe INSTALL --preclean --no-multiarch --with-keep.source testRcpp

I can change access rights under Windows but when I run Rcmd.exe INSTALL --preclean --no-multiarch --with-keep.source testRcpp again, the project is again read-only. This never happend with projects not using Rcpp...


Output devtools::check()

── R CMD check results ─────────────────────────────────────── testRcpp 1.0 ────
Duration: 1m 35.9s
0 errors ✔ | 0 warnings ✔ | 0 notes ✔
R CMD check succeeded

(But subsequent "build" fails again.)

Build options

enter image description here


Solution

  • The following works for me:

    1. Restarting R - Session > Restart R
    2. Reinstalling the package - Build > Install Package

    I don't know why. The only "reason" I can think of is as follows: When loading the package, R opens a connection to the *.dll file in the package. Somehow that connections prevents from removing the file, hence:

    ERROR: cannot remove earlier installation, is it in use?
    

    Restarting R releases that connection.


    I don't understand why unloading the package before reinstalling does not have the same effect, though.