R: Difference between revisions

imported>Idontgetoutmuch
No edit summary
imported>Dmvianna
direct users of Jupyter to use jupyterWith from tweag
Line 26: Line 26:
== Jupyter Notebook ==
== Jupyter Notebook ==


There is currently no package in nixpkgs to set up Jupyter Notebook with additional kernels. (Keep an eye on this [https://github.com/NixOS/nixpkgs/pull/33673 this pull request] though).
Use [https://github.com/tweag/jupyterWith jupyterWith]
 
For now, you can save the following into a file and run it with nix-shell:
<syntaxhighlight lang="nix>
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 pythonPackages.jupyter ];
  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')"
  '';
}
</syntaxhighlight>
 
== How does <code>rWrapper</code> work? ==
 
<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:
 
'''shell.nix'''
<syntaxhighlight lang = "nix">
let
  pkgs = import <nixpkgs> {};
  stdenv = pkgs.stdenv;
in with pkgs; {
  myProject = stdenv.mkDerivation {
    name = "Speedracer";
    version = "1";
 
    buildInputs =  [
      R
      rPackages.knitr
      rPackages.rmarkdown
    ];
  };
}
</syntaxhighlight>


== R with Lorri ==
== R with Lorri ==