10

I have problem in installing plyr R package, and got the following error:

Installing package into '/home/mousavian/R/x86_64-pc-linux-gnu-library/3.2'
(as 'lib' is unspecified)
* installing *source* package 'plyr' ...
** package 'plyr' successfully unpacked and MD5 sums checked
** libs
g++ -I/share/apps/R/lib64/R/include -DNDEBUG  -I/usr/local/include -I"/home/mousavian/R/x86_64-pc-linux-gnu-library/3.2/Rcpp/include"   -fpic  -g -O2  -c RcppExports.cpp -o RcppExports.o
gcc -I/share/apps/R/lib64/R/include -DNDEBUG  -I/usr/local/include -I"/home/mousavian/R/x86_64-pc-linux-gnu-library/3.2/Rcpp/include"   -fpic  -g -O2  -c loop_apply.c -o loop_apply.o
loop_apply.c: In function 'loop_apply':
loop_apply.c:15:3: error: 'for' loop initial declarations are only allowed in C99 or C11 mode
   for(int i = 0; i < n1; i++) {
   ^
loop_apply.c:15:3: note: use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code
make: *** [loop_apply.o] Error 1
ERROR: compilation failed for package 'plyr'
* removing '/home/mousavian/R/x86_64-pc-linux-gnu-library/3.2/plyr'
Warning message:
In install.packages("R packages/plyr_1.8.3.tar.gz", repos = NULL) :
  installation of package 'R packages/plyr_1.8.3.tar.gz' had non-zero exit status

How can I use -std=c99 option when try to install R packages from the source by install.packages command?

Thanks

Zaynab
  • 233
  • 3
  • 16

4 Answers4

14

Try C11 First

The compiler error tells you to choose either C99 or C11. So unless you're knowingly compiling legacy code, try C11 first. You can always then try the legacy C99 if that doesn't work.

Use withr::with_makevars

Editing individual package Makevars or coordinating global changes to your include/lib directories for every package that doesn't directly compile from source is not a happy strategy for the future. Don't do that! A great alternative to editing your R's make configuration is to use withr::with_makevars to manipulate the Makevars only for the installation command:

library(withr)

with_makevars(c(PKG_CFLAGS = "-std=c11"), 
              install.packages("plyr", repos = NULL, type = "source"), 
              assignment = "+=")

Plus, you likely already have withr installed since it's a devtools dependency.

merv
  • 67,214
  • 13
  • 180
  • 245
  • what does the assignment parameter do? – Richard DiSalvo Sep 29 '19 at 20:32
  • 1
    @RichardDiSalvo the `+=` is append to any existing value that might be assigned to the `PKG_CFLAGS` variable in the actual `MAKEVARS` file in the package. The alternative is to overwrite it with `assignment="="`. I'm not exactly sure what the other options (`":="`, `"?="`) do. – merv Sep 29 '19 at 23:05
  • I'm getting the same error messages as the OP installing the `cubature` package. However when I run your code I get the error: `Warning: invalid package ‘cubature’` followed by `Error: ERROR: no packages specified`. – RobertF May 05 '21 at 20:26
  • 1
    @RobertF did you see this, i.e., try `"-std=gnu99"` instead?: https://github.com/bnaras/cubature/issues/30#issuecomment-794497275 – merv May 06 '21 at 03:37
10

If not already existing, create a directory in your $HOME (/home/mousavian/.R in your case). Inside, create a Makevars file (no extension). Edit this file with your favorite editor and write:

CC = gcc -std=c99

Then, save it and after starting R, simply run

install.packages("plyr", dependencies = TRUE)

It should compile with gcc -std=c99.

  • I couldn't create .R folder, do you mean folder with name "R" or ".R"? Makevars file is a text file with the name of "Makevars.txt"? – Zaynab Feb 04 '16 at 10:44
  • I said `.R` and `Makevars`. `mkdir .R`, `cd .R`, `nano Makevars` or `vi Makevars`. –  Feb 04 '16 at 10:44
  • mkdir: cannot create directory `.R': File exists – Zaynab Feb 04 '16 at 10:46
  • Really! Then use it! Waste of time' –  Feb 04 '16 at 10:48
  • I have these folders in /home/mousavian/: – Zaynab Feb 04 '16 at 10:48
  • bio R R packages Thesis – Zaynab Feb 04 '16 at 10:49
  • `cd .R`, `nano Makevars` or `vi Makevars` –  Feb 04 '16 at 10:49
  • Yes, Thank you so much. – Zaynab Feb 04 '16 at 11:58
  • I noticed you never accepted any useful answer to any of your question. It is considered as good manner on this site to do so. –  Feb 05 '16 at 05:34
  • I'm downvoting this because it's quite bad advice. It advocates users *globally* change their R configuration to interpret all C source code to be **C99**, for all packages, in perpetuity. If all you have is a hammer...you should probably shop around for other tools. – merv Sep 01 '18 at 17:35
1

The default /etc/R/Makeconf includes CC = gcc -std=gnu99 (R 3.3.2 and probably most or all other recent versions, although I think the in-progress development version jumps up into the 21st century)

The accepted answer may well work, but is not the R default. Setting CC=gcc with no other options would drop this default, and this another reason for getting this error. Best to leave defaults alone unless you know what you're doing.

Jack Wasey
  • 3,360
  • 24
  • 43
0

As an addendum, to possibly save someone a few minutes: I ran into a similar issue after an R update to 3.3.2 on an Rstudio server (that I have no control over), but with g++. Compiler options for g++ may be added to Makevars like this:

CXX1XSTD = -std=gnu++11

(the package I needed to compile required gnu++11)