R: Difference between revisions

imported>Brentscott
Adds a section specifying how to setup R + packages using Flakes and nix-direnv
fix links
 
(3 intermediate revisions by 3 users not shown)
Line 77: Line 77:
== R with Flakes and nix-direnv==
== R with Flakes and nix-direnv==


R and accompanying R-packages can be installed using a [https://nixos.wiki/wiki/Flakes Flake] and then managed/activated with [https://github.com/nix-community/nix-direnv nix-direnv] to create a reproducible development environment. After the initial setup of nix-direnv (instructions provided on the GitHub README), there is a [https://github.com/nix-community/nix-direnv/blob/master/templates/flake/flake.nix flake template] provided by the nix-direnv maintainers to get started. Run <code>nix flake new -t github:nix-community/nix-direnv .</code> to initialize the flake template in your current directory. This will create a `flake.nix` file that can be edited to setup the R-environment:  
R and accompanying R-packages can be installed using a [[Flakes | Flake]] and then managed/activated with [https://github.com/nix-community/nix-direnv nix-direnv] to create a reproducible development environment. After the initial setup of nix-direnv (instructions provided on the GitHub README), there is a [https://github.com/nix-community/nix-direnv/blob/master/templates/flake/flake.nix flake template] provided by the nix-direnv maintainers to get started. Run <code>nix flake new -t github:nix-community/nix-direnv .</code> to initialize the flake template in your current directory. This will create a `flake.nix` file that can be edited to setup the R-environment:  


<syntaxhighlight lang = "nix" >
<syntaxhighlight lang = "nix" >
Line 101: Line 101:
[https://github.com/wbolster/emacs-direnv Emacs has support for direnv] which can be setup to use R with ESS. Direnv functionality can also be set in Doom Emacs under :tools in the `init.el` file in `.doom.d` folder.  
[https://github.com/wbolster/emacs-direnv Emacs has support for direnv] which can be setup to use R with ESS. Direnv functionality can also be set in Doom Emacs under :tools in the `init.el` file in `.doom.d` folder.  


==Install an R-package from GitHub==


The R-packages available in <nixpkgs> are generated from a recent snapshot of CRAN. You may find certain packages a version behind or want to install a package not on CRAN/Bioconducter. R-packages can be installed from GitHub using `buildRPackage` and `fetchFromGitHub`. An example of installing {rmarkdown} from GitHub using a Flake:
<syntaxhighlight lang = "nix" >
{
  description = "A basic flake with a shell";
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
  inputs.flake-utils.url = "github:numtide/flake-utils";
  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system: let
      pkgs = nixpkgs.legacyPackages.${system};
      rmark = pkgs.rPackages.buildRPackage {
        name = "rmarkdown";
        src = pkgs.fetchFromGitHub{
          owner = "rstudio";
          repo = "rmarkdown";
          rev = "b87ca50c8c4d5a5876333b598aed4eb84de925a3";
          sha256 = "12mhmmibizbxgmsns80c8h97rr7rclv9hz98zpgsl26hw3s4l0vm";
        };
  propagatedBuildInputs = with pkgs.rPackages; [bslib evaluate jsonlite knitr stringr tinytex yaml xfun];
      };
    in {
      devShells.default = pkgs.mkShell {
        nativeBuildInputs = [ pkgs.bashInteractive ];
        buildInputs = with pkgs; [ R rmark pandoc ];
      };
    });
}
</syntaxhighlight>
You will need to obtain the `rev` and `sha256` for the package on github [https://search.nixos.org/packages?channel=22.05&show=nix-prefetch-git&from=0&size=50&sort=relevance&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.
== A note on knitr ==
== A note on knitr ==


Line 108: Line 142:
== Other Editors ==
== Other Editors ==


'''with vim''' -  [https://nixos.wiki/wiki/Nvim-r nvim-r]
'''with vim''' -  [[Nvim-r | nvim-r]]




Line 115: Line 149:
== External Documentation ==
== External Documentation ==


* [https://nixos.org/nixpkgs/manual/#r-packages R user guide in nixpkgs manual]
* [https://nixos.org/manual/nixpkgs/stable/#r R user guide in nixpkgs manual]


== Officer package ==
== Officer package ==
Line 132: Line 166:
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
https://churchman.nl/tag/r/
https://churchman.nl/tag/r/
[[Category:Languages]]