1# R {#r}
2
3## Installation {#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 =
11 super:
12 let
13 self = super.pkgs;
14 in
15 {
16
17 rEnv = super.rWrapper.override {
18 packages = with self.rPackages; [
19 devtools
20 ggplot2
21 reshape2
22 yaml
23 optparse
24 ];
25 };
26 };
27}
28```
29
30Then you can use `nix-env -f "<nixpkgs>" -iA rEnv` to install it into your user
31profile. The set of available libraries can be discovered by running the
32command `nix-env -f "<nixpkgs>" -qaP -A rPackages`. The first column from that
33output is the name that has to be passed to rWrapper in the code snipped above.
34
35However, if you'd like to add a file to your project source to make the
36environment available for other contributors, you can create a `default.nix`
37file like so:
38
39```nix
40with import <nixpkgs> { };
41{
42 myProject = stdenv.mkDerivation {
43 name = "myProject";
44 version = "1";
45 src = if lib.inNixShell then null else nix;
46
47 buildInputs = with rPackages; [
48 R
49 ggplot2
50 knitr
51 ];
52 };
53}
54```
55and then run `nix-shell .` to be dropped into a shell with those packages
56available.
57
58## RStudio {#rstudio}
59
60RStudio uses a standard set of packages and ignores any custom R
61environments or installed packages you may have. To create a custom
62environment, see `rstudioWrapper`, which functions similarly to
63`rWrapper`:
64
65```nix
66{
67 packageOverrides =
68 super:
69 let
70 self = super.pkgs;
71 in
72 {
73
74 rstudioEnv = super.rstudioWrapper.override {
75 packages = with self.rPackages; [
76 dplyr
77 ggplot2
78 reshape2
79 ];
80 };
81 };
82}
83```
84
85Then like above, `nix-env -f "<nixpkgs>" -iA rstudioEnv` will install
86this into your user profile.
87
88Alternatively, you can create a self-contained `shell.nix` without the need to
89modify any configuration files:
90
91```nix
92{
93 pkgs ? import <nixpkgs> { },
94}:
95
96pkgs.rstudioWrapper.override {
97 packages = with pkgs.rPackages; [
98 dplyr
99 ggplot2
100 reshape2
101 ];
102}
103```
104
105Executing `nix-shell` will then drop you into an environment equivalent to the
106one above. If you need additional packages just add them to the list and
107re-enter the shell.
108
109## Updating the package set {#updating-the-package-set}
110
111There is a script and associated environment for regenerating the package
112sets and synchronising the rPackages tree to the current CRAN and matching
113BIOC release. These scripts are found in the `pkgs/development/r-modules`
114directory and executed as follows:
115
116```bash
117nix-shell generate-shell.nix
118
119Rscript generate-r-packages.R cran > cran-packages.json.new
120mv cran-packages.json.new cran-packages.json
121
122Rscript generate-r-packages.R bioc > bioc-packages.json.new
123mv bioc-packages.json.new bioc-packages.json
124
125Rscript generate-r-packages.R bioc-annotation > bioc-annotation-packages.json.new
126mv bioc-annotation-packages.json.new bioc-annotation-packages.json
127
128Rscript generate-r-packages.R bioc-experiment > bioc-experiment-packages.json.new
129mv bioc-experiment-packages.json.new bioc-experiment-packages.json
130```
131
132`generate-r-packages.R <repo>` reads `<repo>-packages.json`, therefore
133the renaming.
134
135The contents of a generated `*-packages.json` file will be used to
136create a package derivation for each R package listed in the file.
137
138Some packages require overrides to specify external dependencies or other
139patches and special requirements. These overrides are specified in the
140`pkgs/development/r-modules/default.nix` file. As the `*-packages.json`
141contents are automatically generated it should not be edited and broken
142builds should be addressed using overrides.