RStudio: Optionally allow packages from custom R environment
https://nixos.org/nixpkgs/manual/#r-packages contains a method for setting up an R environment with a specific set of libraries, and it creates an R wrapper which points R to those libraries. The package RStudio relies on the standard R package, which then cannot access any of the libraries specified in a custom R environment. While one may easily use pkgs.rstudio.override to change rstudio's R dependency to the custom R environment, this accomplishes nothing because while RStudio runs the correct R wrapper it clears out the environment variable R_LIBS_SITE - and so it is still unable to use any of those packages. In order to work around this problem, these changes allow the user to optionally modify rstudio's wrapper to set environment variable R_PROFILE_USER to an R script which sets R's .libPaths(..) to point to the same libraries; that script is generated from R_LIBS_SITE in the R wrapper. By default, this change has no effect. If R is overridden to something else, and if useRPackages is changed from its default of false, then the change described above is made; for instance: { packageOverrides = pkgs: let self = pkgs.pkgs; in rec { rEnv = pkgs.rWrapper.override { packages = with self.rPackages; [ dplyr ggplot2 e1071 rpart reshape ]; }; rstudioEnv = pkgs.rstudio.override { R = rEnv; useRPackages = true; }; }; }
This commit is contained in:
@@ -53,6 +53,37 @@ in with pkgs; {
|
||||
and then run `nix-shell .` to be dropped into a shell with those packages
|
||||
available.
|
||||
|
||||
## RStudio
|
||||
|
||||
RStudio by default will not use the libraries installed like above.
|
||||
You must override its R version with your custom R environment, and
|
||||
set `useRPackages` to `true`, like below:
|
||||
|
||||
```nix
|
||||
{
|
||||
packageOverrides = super: let self = super.pkgs; in
|
||||
{
|
||||
|
||||
rEnv = super.rWrapper.override {
|
||||
packages = with self.rPackages; [
|
||||
devtools
|
||||
ggplot2
|
||||
reshape2
|
||||
yaml
|
||||
optparse
|
||||
];
|
||||
};
|
||||
rstudioEnv = super.rstudio.override {
|
||||
R = rEnv;
|
||||
useRPackages = true;
|
||||
};
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
Then like above, `nix-env -f "<nixpkgs>" -iA rstudioEnv` will install
|
||||
this into your user profile.
|
||||
|
||||
## Updating the package set
|
||||
|
||||
```bash
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
{ stdenv, R, makeWrapper, recommendedPackages, packages }:
|
||||
|
||||
stdenv.mkDerivation {
|
||||
stdenv.mkDerivation rec {
|
||||
name = R.name + "-wrapper";
|
||||
|
||||
buildInputs = [makeWrapper R] ++ recommendedPackages ++ packages;
|
||||
|
||||
unpackPhase = ":";
|
||||
|
||||
# This filename is used in 'installPhase', but needs to be
|
||||
# referenced elsewhere. This will be relative to this package's
|
||||
# path.
|
||||
passthru = {
|
||||
fixLibsR = "fix_libs.R";
|
||||
};
|
||||
|
||||
installPhase = ''
|
||||
mkdir -p $out/bin
|
||||
cd ${R}/bin
|
||||
@@ -14,6 +21,17 @@ stdenv.mkDerivation {
|
||||
makeWrapper ${R}/bin/$exe $out/bin/$exe \
|
||||
--prefix "R_LIBS_SITE" ":" "$R_LIBS_SITE"
|
||||
done
|
||||
# RStudio (and perhaps other packages) overrides the R_LIBS_SITE
|
||||
# which the wrapper above applies, and as a result packages
|
||||
# installed in the wrapper (as in the method described in
|
||||
# https://nixos.org/nixpkgs/manual/#r-packages) aren't visible.
|
||||
# The below turns R_LIBS_SITE into some R startup code which can
|
||||
# correct this.
|
||||
echo "# Autogenerated by wrapper.nix from R_LIBS_SITE" > $out/${passthru.fixLibsR}
|
||||
echo -n ".libPaths(c(.libPaths(), \"" >> $out/${passthru.fixLibsR}
|
||||
echo -n $R_LIBS_SITE | sed -e 's/:/", "/g' >> $out/${passthru.fixLibsR}
|
||||
echo -n "\"))" >> $out/${passthru.fixLibsR}
|
||||
echo >> $out/${passthru.fixLibsR}
|
||||
'';
|
||||
|
||||
meta = {
|
||||
|
||||
Reference in New Issue
Block a user