b-rodrigues / rix

Reproducible development environments for R with Nix
https://b-rodrigues.github.io/rix/
GNU General Public License v3.0
102 stars 11 forks source link

`Error in available_r() : object 'r_nix_revs' not found` if the package is not loaded #200

Open eliocamp opened 5 days ago

eliocamp commented 5 days ago

rix::rix:() seems to fail if the package is not loaded:

path_default_nix <- tempdir()

rix::rix(r_ver = "latest",
    r_pkgs = c("dplyr", "ggplot2"),
    system_pkgs = NULL,
    git_pkgs = NULL,
    ide = "code",
    project_path = path_default_nix,
    overwrite = TRUE,
    print = TRUE)
#> Error in available_r(): object 'r_nix_revs' not found

library(rix)
rix::rix(r_ver = "latest",
         r_pkgs = c("dplyr", "ggplot2"),
         system_pkgs = NULL,
         git_pkgs = NULL,
         ide = "code",
         project_path = path_default_nix,
         overwrite = TRUE,
         print = TRUE)
#> # This file was generated by the {rix} R package v0.7.1 on 2024-06-24
#> # with following call:
#> # >rix::rix(r_ver = "9693852a2070b398ee123a329e68f0dab5526681",
#> #  > r_pkgs = c("dplyr",
#> #  > "ggplot2"),
#> #  > system_pkgs = NULL,
#> #  > git_pkgs = NULL,
#> #  > ide = "code",
#> #  > project_path = path_default_nix,
#> #  > overwrite = TRUE,
#> #  > print = TRUE)
#> # It uses nixpkgs' revision 9693852a2070b398ee123a329e68f0dab5526681 for reproducibility purposes
#> # which will install R version latest.
#> # Report any issues to https://github.com/b-rodrigues/rix
#> let
#>  pkgs = import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/9693852a2070b398ee123a329e68f0dab5526681.tar.gz") {};
#>  
#>   rpkgs = builtins.attrValues {
#>     inherit (pkgs.rPackages) 
#>       dplyr
#>       ggplot2
#>       languageserver;
#>   };
#>     
#>   system_packages = builtins.attrValues {
#>     inherit (pkgs) 
#>       R
#>       glibcLocales
#>       nix;
#>   };
#>   
#> in
#> 
#> pkgs.mkShell {
#>   LOCALE_ARCHIVE = if pkgs.system == "x86_64-linux" then  "${pkgs.glibcLocales}/lib/locale/locale-archive" else "";
#>   LANG = "en_US.UTF-8";
#>    LC_ALL = "en_US.UTF-8";
#>    LC_TIME = "en_US.UTF-8";
#>    LC_MONETARY = "en_US.UTF-8";
#>    LC_PAPER = "en_US.UTF-8";
#>    LC_MEASUREMENT = "en_US.UTF-8";
#> 
#>   buildInputs = [  rpkgs  system_packages   ];
#>   
#> }

This seems to be an issue with rix:::available_r(). https://github.com/b-rodrigues/rix/blob/86fac584c4cf16f1d6cc89745caf81088b8ae7f8/R/available_r.R#L14

philipp-baumann commented 5 days ago

Thanks for reporting @eliocamp . Can confirm the bug.

@b-rodrigues I think we best put it in R/sysdata.rda.

From ?data

"...Use of data within a function without an envir argument has the almost always undesirable side-effect of putting an object in the user's workspace (and indeed, of replacing any object of that name already there). It would almost always be better to put the object in the current evaluation environment by data(..., envir = environment()). However, two alternatives are usually preferable, both described in the ‘Writing R Extensions’ manual.

For sets of data, set up a package to use lazy-loading of data.

For objects which are system data, for example lookup tables used in calculations within the function, use a file ‘R/sysdata.rda’ in the package sources or create the objects by R code at package installation time.

A sometimes important distinction is that the second approach places objects in the namespace but the first does not. So if it is important that the function sees mytable as an object from the package, it is system data and the second approach should be used. In the unusual case that a package uses a lazy-loaded dataset as a default argument to a function, that needs to be specified by ::, e.g., survival::survexp.us. "

b-rodrigues commented 5 days ago

Can you test with latest master?

eliocamp commented 4 days ago

With the latest master, I get a different object that is not found 😅

rix::rix(r_ver = "latest",
    r_pkgs = NULL,
    system_pkgs = NULL,
    git_pkgs = NULL,
    ide = "rstudio",
    project_path = "../nixtest2/",
    overwrite = TRUE,
    print = TRUE)
#> Error in available_r(): object 'sysdata' not found

Created on 2024-06-24 with reprex v2.1.0

philipp-baumann commented 4 days ago

Bruno has just pushed a fix:

devtools::install_github("https://github.com/b-rodrigues, ref = "simplify_available_r")

This should work.

b-rodrigues commented 4 days ago

not quite yet

b-rodrigues commented 4 days ago

should be ok now

philipp-baumann commented 4 days ago

https://github.com/b-rodrigues/rix/pull/220