1# R {#r} 2 3## Installation 4 5Define an environment for R that contains all the libraries that you'd like to 6use by adding the following snippet to your $HOME/.config/nixpkgs/config.nix file: 7 8```nix 9{ 10 packageOverrides = super: let self = super.pkgs; in 11 { 12 13 rEnv = super.rWrapper.override { 14 packages = with self.rPackages; [ 15 devtools 16 ggplot2 17 reshape2 18 yaml 19 optparse 20 ]; 21 }; 22 }; 23} 24``` 25 26Then you can use `nix-env -f "<nixpkgs>" -iA rEnv` to install it into your user 27profile. The set of available libraries can be discovered by running the 28command `nix-env -f "<nixpkgs>" -qaP -A rPackages`. The first column from that 29output is the name that has to be passed to rWrapper in the code snipped above. 30 31However, if you'd like to add a file to your project source to make the 32environment available for other contributors, you can create a `default.nix` 33file like so: 34```nix 35with import <nixpkgs> {}; 36{ 37 myProject = stdenv.mkDerivation { 38 name = "myProject"; 39 version = "1"; 40 src = if lib.inNixShell then null else nix; 41 42 buildInputs = with rPackages; [ 43 R 44 ggplot2 45 knitr 46 ]; 47 }; 48} 49``` 50and then run `nix-shell .` to be dropped into a shell with those packages 51available. 52 53## RStudio 54 55RStudio uses a standard set of packages and ignores any custom R 56environments or installed packages you may have. To create a custom 57environment, see `rstudioWrapper`, which functions similarly to 58`rWrapper`: 59 60```nix 61{ 62 packageOverrides = super: let self = super.pkgs; in 63 { 64 65 rstudioEnv = super.rstudioWrapper.override { 66 packages = with self.rPackages; [ 67 dplyr 68 ggplot2 69 reshape2 70 ]; 71 }; 72 }; 73} 74``` 75 76Then like above, `nix-env -f "<nixpkgs>" -iA rstudioEnv` will install 77this into your user profile. 78 79Alternatively, you can create a self-contained `shell.nix` without the need to 80modify any configuration files: 81 82```nix 83{ pkgs ? import <nixpkgs> {} 84}: 85 86pkgs.rstudioWrapper.override { 87 packages = with pkgs.rPackages; [ dplyr ggplot2 reshape2 ]; 88} 89 90``` 91 92Executing `nix-shell` will then drop you into an environment equivalent to the 93one above. If you need additional packages just add them to the list and 94re-enter the shell. 95 96## Updating the package set 97 98```bash 99nix-shell generate-shell.nix 100 101Rscript generate-r-packages.R cran > cran-packages.nix.new 102mv cran-packages.nix.new cran-packages.nix 103 104Rscript generate-r-packages.R bioc > bioc-packages.nix.new 105mv bioc-packages.nix.new bioc-packages.nix 106 107Rscript generate-r-packages.R bioc-annotation > bioc-annotation-packages.nix.new 108mv bioc-annotation-packages.nix.new bioc-annotation-packages.nix 109 110Rscript generate-r-packages.R bioc-experiment > bioc-experiment-packages.nix.new 111mv bioc-experiment-packages.nix.new bioc-experiment-packages.nix 112``` 113 114`generate-r-packages.R <repo>` reads `<repo>-packages.nix`, therefor the renaming. 115 116 117## Testing if the Nix-expression could be evaluated 118 119```bash 120nix-build test-evaluation.nix --dry-run 121``` 122 123If this exits fine, the expression is ok. If not, you have to edit `default.nix`