Having lots of trouble with R development

I’m really enjoying NixOS; thanks to all who have made it possible.

But, I’m having some problems when I work on R code development projects, and I hope someone can help me. Even though I’ve been using NixOS for a couple of years, I still only know the basics and don’t even know where to start.

Basically, as soon as I start editing an R file in vim, I see (in the top command output) that my CPU usage goes to 100%, because of (one or more) systemd-coredum processes. When I quit vim, these go away. Also, if I just start R and load the devtools package, then try to build() or install() the package I am working on, I get an error, like this:


> library(devtools)
Loading required package: usethis
> build()
   *** buffer overflow detected ***: terminated
   /nix/store/r3pf5fwxmvbkjksba0kic2l1i9sf91ni-R-4.0.4/lib/R/bin/build: line 10: 476437 Done                    echo 'tools:::.build_packages()'
        476438 Aborted                 (core dumped) | R_DEFAULT_PACKAGES= LC_COLLATE=C "${R_HOME}/bin/R" --no-restore --no-echo --args ${args}
Error in (function (command = NULL, args = character(), error_on_status = TRUE,  :
  System command 'R' failed, exit status: 134, stdout + stderr:
E> *** buffer overflow detected ***: terminated
E> /nix/store/r3pf5fwxmvbkjksba0kic2l1i9sf91ni-R-4.0.4/lib/R/bin/build: line 10: 476437 Done                    echo 'tools:::.build_packages()'
E>      476438 Aborted                 (core dumped) | R_DEFAULT_PACKAGES= LC_COLLATE=C "${R_HOME}/bin/R" --no-restore --no-echo --args ${args}
Type .Last.error.trace to see where the error occured

Now, to install R, I basically followed the instructions in the Nixpkgs Manual (section 15.21, Nixpkgs 23.11 manual | Nix & NixOS). I created the following configuration file in .config/nixpkgs/config.nix:

{
  allowUnfree = true;

  packageOverrides = super: let self = super.pkgs; in
  {

    rEnv = super.rWrapper.override {
      packages = with self.rPackages; [
        languageserver
        spelling
        knitr
        lintr
        yaml
        devtools
        tidyverse
        shiny
        data_table
        flextable
        kableExtra
        mvtnorm
        Formula
        MatchIt
      ];
    };
  };
}

I’m sorry if this isn’t a really clear or helpful description of what is going on, but I’d be happy to provide any other information requested. Many thanks in advance.

UPDATE: I was able to diagnose the problem with opening R files in vim a bit. I traced it back to the coc.nvim plugin, which loads the R language server. I guess this is what was crashing. Still, any help fixing this, or the problem with devtools, is appreciated.

devtools::build seems to be a fancy way to run R CMD build but it looks like it gets confused by the wrapped R created by nix. Other functions like load_all() work. Can you use system("R CMD build .") to build your package?

Hi @alexv, thanks for responding. I somehow feel that you are on the right track. Indeed, devtools::load_all() works fine, as does devtools::document() and so on.

In fact, R CMD build --no-build-vignettes . works fine from the command line (I need to add the --no-build-vignettes flag because the vignette building fails, which is another mystery since it builds fine on other platforms). It also works fine if I use system("R CMD build --no-build-vignettes .") from the R console (no difference here). Does that seem strange? What do you think?

@mrbrich I don’t remember the details on building vignettes, but doesn’t R need some additional tools for that? Like TeX to produce pdfs? In that case you’d need to add them to you nix R environment.

One word of caution, don’t build R with MKL support in nixpkgs (see R built with MKL computes incorrect results · Issue #104026 · NixOS/nixpkgs · GitHub, that issue also has a workaround I use).

@alexv Hmm… thanks. I don’t really know what MKL is, so hopefully that doesn’t apply to me :slight_smile:

To answer your question: yes, R needs tools to build the vignettes (pandoc, pdflatex, …) but that’s not the issue because I have 2 vignettes in my package, and one builds fine while the other doesn’t. It’s just some weird issue, but no worries.

What bugs me is that this should be easy, and WOULD be easy if I were NOT using NixOS. But I really don’t want to switch - I like NixOS a lot. Also, I’m pretty sure that this issue wansn’t always there - I used to be able to use devtools without problems on the same system a while ago. So it should be possible to resolve it.