RStudio IDE package 'Check' can't find installed packages

Hello!

I am developing a package using the RStudio IDE. I have used the Build tab to assist the pkg dev process for years, but I am running into an odd issue recently. When I use the IDE buttons to Check or Test the package, I get a note that two package are not installed when they are indeed installed.

Here's the note I get when trying to check the package.

Below you can see that I have two library paths available and both packages are installed into both libraries. I have no idea how these packages are not being found??? ¯\_(ツ)_/¯

Any ideas?! Thank you!

.libPaths()
#> [1] "C:/Users/SjobergD/R-dev"            "C:/Program Files/R/R-4.2.2/library"
installed.packages() |> 
  dplyr::as_tibble() |> 
  dplyr::filter(Package %in% c("gtsummary", "tidyr")) |> 
  dplyr::select(Package, LibPath, Version)
#> # A tibble: 4 × 3
#>   Package   LibPath                            Version
#>   <chr>     <chr>                              <chr>  
#> 1 gtsummary C:/Users/SjobergD/R-dev            1.7.0  
#> 2 tidyr     C:/Users/SjobergD/R-dev            1.3.0  
#> 3 gtsummary C:/Program Files/R/R-4.2.2/library 1.7.0  
#> 4 tidyr     C:/Program Files/R/R-4.2.2/library 1.3.0

Created on 2023-01-28 with reprex v2.0.2

Where do you set .libPaths()? Maybe you set it in the .Rprofile and the profile is not executed by RStudio when checking a package.

Seems like you are using a user package library. IMO the best way to do that is to use the location at Sys.getenv("R_LIBS_USER"):

❯ Sys.getenv("R_LIBS_USER")
[1] "/Users/gaborcsardi/Library/R/arm64/4.2/library/"

If you create this directory, then R will pick it up automatically. It also has the benefit that libraries for different R versions do not interfere.

Thank you @Gabor for the response! Much appreciated!

Somehow my default lib is different from the environmental variable location.

.libPaths()
#> [1] "C:/Program Files/R/R-4.2.2/library"
Sys.getenv("R_LIBS_USER")
#> [1] "C:\\Users\\SjobergD\\AppData\\Local/R/win-library/4.2"

Created on 2023-01-30 with reprex v2.0.2
Additionally, I do add another lib in my project .Rprofile for package dev work (to keep my primary libs clean of dev versions of packages) using devtools::dev_mode(on = TRUE).

I created the Sys.getenv("R_LIBS_USER") folder as you suggested. Now when I open R, the new folder is included in the default libraries. I also copied all packages into the new folder.

.libPaths()
#> [1] "C:/Users/SjobergD/AppData/Local/R/win-library/4.2"
#> [2] "C:/Program Files/R/R-4.2.2/library"

Created on 2023-01-30 with reprex v2.0.2
BUT, when I open this Rproj, I have the additional dev folder. and I still get the error that gtsummary and tidyr are not found despite being installed in every lib listed in .libPaths().

.libPaths()
#> [1] "C:/Users/SjobergD/R-dev"                          
#> [2] "C:/Users/SjobergD/AppData/Local/R/win-library/4.2"
#> [3] "C:/Program Files/R/R-4.2.2/library"

Created on 2023-01-30 with reprex v2.0.2

installed.packages() |> 
  dplyr::as_tibble() |> 
  dplyr::filter(Package %in% c("gtsummary", "tidyr")) |> 
  dplyr::select(Package, LibPath, Version)
#> # A tibble: 6 × 3
#>   Package   LibPath                                           Version
#>   <chr>     <chr>                                             <chr>  
#> 1 gtsummary C:/Users/SjobergD/R-dev                           1.7.0  
#> 2 tidyr     C:/Users/SjobergD/R-dev                           1.3.0  
#> 3 gtsummary C:/Users/SjobergD/AppData/Local/R/win-library/4.2 1.7.0  
#> 4 tidyr     C:/Users/SjobergD/AppData/Local/R/win-library/4.2 1.3.0  
#> 5 gtsummary C:/Program Files/R/R-4.2.2/library                1.7.0  
#> 6 tidyr     C:/Program Files/R/R-4.2.2/library                1.3.0

Created on 2023-01-30 with reprex v2.0.2

If the packages are installed in every lib on my machine, how can the IDE call to devtools::check() not find them?

Yes, you call dev_mode() in your profile I guess, and it adds C:/Users/SjobergD/R-dev to the library path. For some reason the profile is not used when running the check in RStudio.

Is the dev_mode() call conditional in your profile? Or you always call it?

It's run every time the Rproj is opened. This is what my .Rprofile file looks like.

# this sets the dev folder in the libPath
tryCatch(
  devtools::dev_mode(on = TRUE),
  error = function(e) invisible()
)

Even if the .Rprofile is not run for IDE submissions of devtools::check(), I don't understand which libraries are active during the IDE check? Since the pkgs are installed in all the libraries, I assume it should find them. (Although, my preference is that the IDE would run .Rprofile so my local env would match the env the IDE-submitted checks are run in.)

Just to rule out that this is an RStudio thing, can you try running devtools::check() from a Windows terminal or cmd window?

Also, try removing the tryCatch() from the profile, I think you would probably want to know about it if the library setup fails.

Just to rule out that this is an RStudio thing, can you try running devtools::check() from a Windows terminal or cmd window?

There are no issues whendevtools::check() is run interactively in the console. Also no error if it's run from a terminal window with Rscript filename.R where filename.R is a single line with devtools::check().

Try removing the tryCatch() from the profile, I think you would probably want to know about it if the library setup fails.

Issue persists after the tryCatch() is removed. (You're correct that I want to know when devtools::dev_mode() doesn't work, but I keep it because many others fork the repo and don't necessarily have dev mode setup on their machine.)

Thank you again for reviewing these details with me!

Yeah, it seems like RStudio calls devtools::check() with --vanilla, so your profile is not used: rstudio/SessionBuild.cpp at 3d5918dca30a51e6c5b2805b7d2d73169b62d6fd · rstudio/rstudio · GitHub
(Might be a different call in this file, but I am pretty sure that this is what happens.)

So unfortunately this is not going to work with RStudio. You can search the RStudio issues at Issues · rstudio/rstudio · GitHub and report it if it hasn't been reported yet.

A workaround is to modify your library/base/R/Rprofile file within your R installation to add the library to the library path. This file is always loaded.

1 Like

OK, thank you so much for the helpful comments.

Since these packages are installed in all the libraries, it's so strange that the packages are not found. I'll file an issue in the GH repo if I don't see a previous posting about it.

Thanks!

Some package must be missing from the system library, I think, or maybe it is broken and cannot be loaded. The system library is always available. (Well, unless you use renv.)

This topic was automatically closed after 45 days. New replies are no longer allowed.


If you have a query related to it or one of the replies, start a new topic and refer back with a link.