R: Difference between revisions

imported>MikiVanousek
The lorri repo has been moved
imported>Brentscott
Adds a section specifying how to setup R + packages using Flakes and nix-direnv
Line 74: Line 74:


</syntaxhighlight>
</syntaxhighlight>
== 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:
<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};
    in {
      devShells.default = pkgs.mkShell {
        nativeBuildInputs = [ pkgs.bashInteractive ];
        buildInputs = with pkgs; [ R rPackages.pagedown chromium pandoc ];
      };
    });
}
</syntaxhighlight>
Saving the file and then running <code>direnv allow</code> in the terminal of the project directory will execute the `flake.lock` and build the shell. This will install the current version of R and the R-package {pagedown} that is in <nixpkgs>. Note additional system dependencies may need installed for certain packages to work such as pandoc for document conversion with {rmarkdown}. Here, chromium is installed in the `buildInputs` so the <code>chrome_print</code> function can be used from {pagedown}. 
[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.


== A note on knitr ==
== A note on knitr ==