R: Difference between revisions

From NixOS Wiki
imported>Jabranham
Show example of nix-shell usage
imported>Josephsdavid
Added template default.nix and shell.nix, as well as links to non-RStudio editors
Line 58: Line 58:


<code>rWrapper</code> simply replaces the R command in your path by a script that sets the environment variable <code>R_LIBS_SITE</code> and then runs R. [https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/r-modules/wrapper.nix]
<code>rWrapper</code> simply replaces the R command in your path by a script that sets the environment variable <code>R_LIBS_SITE</code> and then runs R. [https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/r-modules/wrapper.nix]
== With nix-shell ==
Below is an example of a nix-shell build to run R with knitr for R markdown:
'''default.nix'''
<syntaxhighlight lang = "nix">
let
  pkgs = import <nixpkgs> {};
  stdenv = pkgs.stdenv;
in with pkgs; {
  myProject = stdenv.mkDerivation {
    name = "Speedracer";
    version = "1";
    src = if pkgs.lib.inNixShell then null else nix;
    buildInputs =  [
      R
      rPackages.knitr
      rPackages.rmarkdown
      rPackages.knitr
    ];
  };
}
</syntaxhighlight>
== R with Lorri ==
An example of a '''shell.nix''' for usage with [https://github.com/target/lorri lorri] is shown below:
<syntaxhighlight lang = "nix">
let
  pkgs = import <nixpkgs> {};
in
  pkgs.mkShell {
    buildInputs = with pkgs; [
      R
      rPackages.rmarkdown
      rPackages.knitr
    ];
  }
</syntaxhighlight>
== Other Editors ==
'''with vim''' -  [https://nixos.wiki/wiki/Nvim-r nvim-r]
Note - if you are building an environment and plan on using nvim-r, it cannot also include rstudio, it will break your packages!
'''with emacs''' - [http://ess.r-project.org/ emacs speaks statistics]


== External Documentation ==
== External Documentation ==


* [https://nixos.org/nixpkgs/manual/#r-packages R user guide in nixpkgs manual]
* [https://nixos.org/nixpkgs/manual/#r-packages R user guide in nixpkgs manual]

Revision as of 17:31, 18 April 2019

R comes with a very large number of packages, many of which available through nixpkgs. In particular, any package available through CRAN.

Similarly to Python, your packages must be declared when installing R. Commands such as install.packages("ggplot2") will not work.

To install R with a collection of packages, a new nix package must be defined, for instance

with pkgs;
let
  R-with-my-packages = rWrapper.override{ packages = with rPackages; [ ggplot2 dplyr xts ]; };
in ...

and then you can put R-with-my-packages into your environment.systemPackages for a system-wide installation, for instance.

If you with to use `nix-shell` to generate an on-the-fly environment with some R packages, the command is similar:

nix-shell --packages 'rWrapper.override{packages = [ rPackages.ggplot2 ];}'

RStudio

RStudio uses a standard set of packages and ignores any custom R environments, like the one set up above. To install it you can use rstudioWrapper just as we used rWrapper earlier.

RStudio-with-my-packages = rstudioWrapper.override{ packages = with rPackages; [ ggplot2 dplyr xts ]; };

Jupyter Notebook

There is currently no package in nixpkgs to set up Jupyter Notebook with additional kernels. (Keep an eye on this this pull request though).

For now, you can save the following into a file and run it with nix-shell:

with import <nixpkgs> {};
let 
my-R-packages = with rPackages; [ ggplot2 dplyr xts ];
R-with-my-packages = rWrapper.override{ packages = with rPackages; my-R-packages ++ [ JuniperKernel ]; };
jupyter-R-kernel = stdenv.mkDerivation {
  name = "jupyter-R-kernel";
  buildInputs = [ pythonPackages.notebook R-with-my-packages ];
  unpackPhase = ":";
  installPhase = ''
    export HOME=$TMP
    ${R-with-my-packages}/bin/R --slave -e "JuniperKernel::installJuniper(prefix='$out')"
  '';
};
in
mkShell rec {
  name = "jupyter-with-R-kernel";
  buildInputs = [ jupyter-R-kernel ];
  shellHook = ''
    export JUPYTER_PATH=${jupyter-R-kernel}/share/jupyter
    # see https://github.com/NixOS/nixpkgs/issues/38733
    ${R-with-my-packages}/bin/R --slave -e "system2('jupyter', 'notebook')"
  '';
}

How does rWrapper work?

rWrapper simply replaces the R command in your path by a script that sets the environment variable R_LIBS_SITE and then runs R. [1]

With nix-shell

Below is an example of a nix-shell build to run R with knitr for R markdown:

default.nix

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

    buildInputs =  [
      R
      rPackages.knitr
      rPackages.rmarkdown
      rPackages.knitr
    ];
  };
}

R with Lorri

An example of a shell.nix for usage with lorri is shown below:

let
  pkgs = import <nixpkgs> {};
in
  pkgs.mkShell {
    buildInputs = with pkgs; [
      R
      rPackages.rmarkdown
      rPackages.knitr
    ];
  }


Other Editors

with vim - nvim-r

Note - if you are building an environment and plan on using nvim-r, it cannot also include rstudio, it will break your packages!

with emacs - emacs speaks statistics

External Documentation