nixpkgs/pkgs/development/r-modules
Chris Hodapp 7638578342 RStudio: Optionally allow packages from custom R environment
https://nixos.org/nixpkgs/manual/#r-packages contains a method for
setting up an R environment with a specific set of libraries, and it
creates an R wrapper which points R to those libraries.

The package RStudio relies on the standard R package, which then
cannot access any of the libraries specified in a custom R
environment.  While one may easily use pkgs.rstudio.override to change
rstudio's R dependency to the custom R environment, this accomplishes
nothing because while RStudio runs the correct R wrapper it clears out
the environment variable R_LIBS_SITE - and so it is still unable to
use any of those packages.

In order to work around this problem, these changes allow the user to
optionally modify rstudio's wrapper to set environment variable
R_PROFILE_USER to an R script which sets R's .libPaths(..) to point to
the same libraries; that script is generated from R_LIBS_SITE in the R
wrapper.

By default, this change has no effect.  If R is overridden to
something else, and if useRPackages is changed from its default of
false, then the change described above is made; for instance:

{
  packageOverrides = pkgs: let self = pkgs.pkgs; in
  rec {
    rEnv = pkgs.rWrapper.override {
      packages = with self.rPackages; [
        dplyr ggplot2 e1071 rpart reshape
      ];
    };
    rstudioEnv = pkgs.rstudio.override { R = rEnv; useRPackages = true; };
  };
}
2017-01-27 18:54:50 -05:00
..
patches Remove no longer (or never) referenced patches 2016-01-24 02:02:21 +01:00
bioc-annotation-packages.nix r-modules: update CRAN, add Bioconductor data pkgs 2016-03-22 22:50:54 -07:00
bioc-experiment-packages.nix r-modules: update CRAN, add Bioconductor data pkgs 2016-03-22 22:50:54 -07:00
bioc-packages.nix work on fixing up bioconductor packages 2016-03-31 16:16:34 -07:00
cran-packages.nix work on fixing up bioconductor packages 2016-03-31 16:16:34 -07:00
default.nix r-modules: fix snpStats package 2017-01-06 21:07:25 +01:00
generate-r-packages.R r-modules: update CRAN, add Bioconductor data pkgs 2016-03-22 22:50:54 -07:00
generate-shell.nix r-modules: use HTTPS, allow passing args from generated set, use MRAN 2015-11-25 11:39:28 +01:00
generic-builder.nix darwin: R: provide gettext and gfortran as buildInputs on Darwin. 2016-07-22 13:31:40 -04:00
irkernel-packages.nix r-modules: regenerate CRAN, BIOC and IRKernel 2015-11-25 11:39:28 +01:00
README.md RStudio: Optionally allow packages from custom R environment 2017-01-27 18:54:50 -05:00
test-evaluation.nix R: improve support for CRAN and Bioconductor package sets 2015-06-17 23:19:56 +02:00
wrapper.nix RStudio: Optionally allow packages from custom R environment 2017-01-27 18:54:50 -05:00

R packages

Installation

Define an environment for R that contains all the libraries that you'd like to use by adding the following snippet to your $HOME/.nixpkgs/config.nix file:

{
    packageOverrides = super: let self = super.pkgs; in
    {

        rEnv = super.rWrapper.override {
            packages = with self.rPackages; [ 
                devtools
                ggplot2
                reshape2
                yaml
                optparse
                ];
        };
    };
}

Then you can use nix-env -f "<nixpkgs>" -iA rEnv to install it into your user profile. The set of available libraries can be discovered by running the command nix-env -f "<nixpkgs>" -qaP -A rPackages. The first column from that output is the name that has to be passed to rWrapper in the code snipped above.

However, if you'd like to add a file to your project source to make the environment available for other contributors, you can create a default.nix file like so:

let
  pkgs = import <nixpkgs> {};
  stdenv = pkgs.stdenv;
in with pkgs; {
  myProject = stdenv.mkDerivation {
    name = "myProject";
    version = "1";
    src = if pkgs.lib.inNixShell then null else nix;

    buildInputs = with rPackages; [
      R
      ggplot2
      knitr
    ];
  };
}

and then run nix-shell . to be dropped into a shell with those packages available.

RStudio

RStudio by default will not use the libraries installed like above. You must override its R version with your custom R environment, and set useRPackages to true, like below:

{
    packageOverrides = super: let self = super.pkgs; in
    {

        rEnv = super.rWrapper.override {
            packages = with self.rPackages; [ 
                devtools
                ggplot2
                reshape2
                yaml
                optparse
                ];
        };
        rstudioEnv = super.rstudio.override {
            R = rEnv;
            useRPackages = true;
        };
    };
}

Then like above, nix-env -f "<nixpkgs>" -iA rstudioEnv will install this into your user profile.

Updating the package set

Rscript generate-r-packages.R cran  > cran-packages.nix.new
mv cran-packages.nix.new cran-packages.nix

Rscript generate-r-packages.R bioc  > bioc-packages.nix.new
mv bioc-packages.nix.new bioc-packages.nix

Rscript generate-r-packages.R irkernel  > irkernel-packages.nix.new
mv irkernel-packages.nix.new irkernel-packages.nix

generate-r-packages.R <repo> reads <repo>-packages.nix, therefor the renaming.

Testing if the Nix-expression could be evaluated

nix-build test-evaluation.nix --dry-run

If this exits fine, the expression is ok. If not, you have to edit default.nix