Pigs (talk | contribs)
Install an R-package from GitHub: Use current channel for search.nixos.org link
Tags: Mobile edit Mobile web edit Advanced mobile edit
DHCP (talk | contribs)
m prefix shell command with "$"; fix formatting
 
(2 intermediate revisions by 2 users not shown)
Line 14: Line 14:
If you with to use `nix-shell` to generate an on-the-fly environment with some R packages, the command is similar:
If you with to use `nix-shell` to generate an on-the-fly environment with some R packages, the command is similar:


<syntaxhighlight lang="sh">
<syntaxhighlight lang="console">
nix-shell --packages 'rWrapper.override{packages = [ rPackages.ggplot2 ];}'
$ nix-shell --packages 'rWrapper.override{packages = [ rPackages.ggplot2 ];}'
</syntaxhighlight>
</syntaxhighlight>


Line 159: Line 159:


You will need to obtain the `rev` and `sha256` for the package on github [https://search.nixos.org/packages?show=nix-prefetch-git&type=packages&query=nix-prefetch-git which can be found by using the `nix-prefetch-git` command line tool.] For the above example, running <code>nix-prefetch-git https://github.com/rstudio/rmarkdown</code> from a terminal will generate the information. You may need to manually specify other R-package/system dependencies for the specific package in the `propagatedBuildInputs`. This information can be found in the `DESCRIPTION` file of the R-package source directory.
You will need to obtain the `rev` and `sha256` for the package on github [https://search.nixos.org/packages?show=nix-prefetch-git&type=packages&query=nix-prefetch-git which can be found by using the `nix-prefetch-git` command line tool.] For the above example, running <code>nix-prefetch-git https://github.com/rstudio/rmarkdown</code> from a terminal will generate the information. You may need to manually specify other R-package/system dependencies for the specific package in the `propagatedBuildInputs`. This information can be found in the `DESCRIPTION` file of the R-package source directory.
== Install an R package from source ==
If you need a specific version of a package or find one that is not available in Nixpkgs, you can use `rPackages.buildRPackage` to build from source. The example below builds S4Arrays from BioConductor.<syntaxhighlight lang="nix">
let
  pkgs = import <nixpkgs> { };
  customPackage = pkgs.rPackages.buildRPackage {
    name = "S4Arrays";
    src = pkgs.fetchurl {
      url = "https://www.bioconductor.org/packages/release/bioc/src/contrib/S4Arrays_1.8.1.tar.gz";
      hash = "sha256-8f2oA0xgwI6CucXbMU1yJC4W6tiVMcDAk8D3Ur3zxw8=";
    };
    buildInputs = with pkgs.rPackages; [
      pkgs.R
      Matrix
      abind
      BiocGenerics
      S4Vectors
      IRanges
      crayon
    ];
  };
in
pkgs.mkShell {
  packages = with pkgs.rPackages; [
    pkgs.R
    Matrix
    abind
    BiocGenerics
    S4Vectors
    IRanges
    crayon
    customPackage # the package we built from source.
  ];
}
</syntaxhighlight>


== A note on knitr ==
== A note on knitr ==
Line 164: Line 199:
To knit a .Rmd file to a pdf (or .Rnw), you need to have included in your envronment <code> pkgs.texlive.combined.scheme-full</code>as well as <code> pandoc </code> or it will fail to knit. None of the other texlive packages contain the proper "frame" package. Note there are likely other workarounds but this requires the least effort.
To knit a .Rmd file to a pdf (or .Rnw), you need to have included in your envronment <code> pkgs.texlive.combined.scheme-full</code>as well as <code> pandoc </code> or it will fail to knit. None of the other texlive packages contain the proper "frame" package. Note there are likely other workarounds but this requires the least effort.


If <code>R</code> is included (using wrapper) but <code>pandoc</code> is not wanted in the user environment, one can expose it just for the R with
<syntaxhighlight lang="nix">
(rWrapper.override{
  packages = with rPackages; [
    markdown
    # other plugins go also here
  ];
}).overrideAttrs (old: {
  buildInputs = (old.buildInputs or []) ++ [
    pkgs.pandoc
  ];
});
</syntaxhighlight>
== Other Editors ==
== Other Editors ==


Line 179: Line 227:
When the R package "officer" has been installed from the Nix package manager, "save_as_docx" does not work:
When the R package "officer" has been installed from the Nix package manager, "save_as_docx" does not work:


<pre>
Error in write_xml.xml_document(private$doc, file = private$filename) :  
Error in write_xml.xml_document(private$doc, file = private$filename) :  
Error closing file
Error closing file
Line 187: Line 236:
2: In write_xml.xml_document(private$doc, file = private$filename) :
2: In write_xml.xml_document(private$doc, file = private$filename) :
Permission denie [1501]
Permission denie [1501]
</pre>


However officer does work if installed using the conventional install.packages() which can be enabled as discussed in
However officer does work if installed using the conventional install.packages() which can be enabled as discussed in