8

I've created and evaluated a call to expand.grid by using eval(parse(...)).

In short:

len <- 36
Text <- paste("pos <- expand.grid(",
  paste(rep("c(TRUE,FALSE)", len), collapse=","), ")", sep="")
eval(parse(text = Text))

Gives me

Error in rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) : 
invalid 'times' value
In addition: Warning message:
In rep.int(rep.int(seq_len(nx), rep.int(rep.fac, nx)), orep) :
NAs introduced by coercion

as opposed to len number of combinations of TRUE/FALSE in the variable pos.

I am overlooking something simple, or maybe not...?

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
brews
  • 661
  • 9
  • 23

1 Answers1

11

As I said in my comment, this has nothing to do with eval or parse. The error is in expand.grid. The problem is that R's maximum vector length is 2^31-1 and rep.int is trying to create a 2^36 length vector.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • What is an alternative to `expand.grid` if I absolutely had to create such a long vector? – David Moore Jan 18 '20 at 17:56
  • Something like: x <- rep(c(TRUE, FALSE), 2^36)... except that on most desktop machines you'd run into memory problems. If you really need a vector this long, try breaking it into chunks if that works for your use of the values in the vector. – Eden Jun 25 '21 at 03:03