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 = 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
35```nix
36with import <nixpkgs> {};
37{
38 myProject = stdenv.mkDerivation {
39 name = "myProject";
40 version = "1";
41 src = if lib.inNixShell then null else nix;
42
43 buildInputs = with rPackages; [
44 R
45 ggplot2
46 knitr
47 ];
48 };
49}
50```
51and then run `nix-shell .` to be dropped into a shell with those packages
52available.
53
54## RStudio {#rstudio}
55
56RStudio uses a standard set of packages and ignores any custom R
57environments or installed packages you may have. To create a custom
58environment, see `rstudioWrapper`, which functions similarly to
59`rWrapper`:
60
61```nix
62{
63 packageOverrides = super: let self = super.pkgs; in
64 {
65
66 rstudioEnv = super.rstudioWrapper.override {
67 packages = with self.rPackages; [
68 dplyr
69 ggplot2
70 reshape2
71 ];
72 };
73 };
74}
75```
76
77Then like above, `nix-env -f "<nixpkgs>" -iA rstudioEnv` will install
78this into your user profile.
79
80Alternatively, you can create a self-contained `shell.nix` without the need to
81modify any configuration files:
82
83```nix
84{ pkgs ? import <nixpkgs> {}
85}:
86
87pkgs.rstudioWrapper.override {
88 packages = with pkgs.rPackages; [ dplyr ggplot2 reshape2 ];
89}
90
91```
92
93Executing `nix-shell` will then drop you into an environment equivalent to the
94one above. If you need additional packages just add them to the list and
95re-enter the shell.
96
97## Updating the package set {#updating-the-package-set}
98
99There is a script and associated environment for regenerating the package
100sets and synchronising the rPackages tree to the current CRAN and matching
101BIOC release. These scripts are found in the `pkgs/development/r-modules`
102directory and executed as follows:
103
104```bash
105nix-shell generate-shell.nix
106
107Rscript generate-r-packages.R cran > cran-packages.nix.new
108mv cran-packages.nix.new cran-packages.nix
109
110Rscript generate-r-packages.R bioc > bioc-packages.nix.new
111mv bioc-packages.nix.new bioc-packages.nix
112
113Rscript generate-r-packages.R bioc-annotation > bioc-annotation-packages.nix.new
114mv bioc-annotation-packages.nix.new bioc-annotation-packages.nix
115
116Rscript generate-r-packages.R bioc-experiment > bioc-experiment-packages.nix.new
117mv bioc-experiment-packages.nix.new bioc-experiment-packages.nix
118```
119
120`generate-r-packages.R <repo>` reads `<repo>-packages.nix`, therefore
121the renaming.
122
123Some packages require overrides to specify external dependencies or other
124patches and special requirements. These overrides are specified in the
125`pkgs/development/r-modules/default.nix` file. As the `*-packages.nix`
126contents are automatically generated it should not be edited and broken
127builds should be addressed using overrides.