javarjrirenjin

Renjin - how to use values generated in java


I'm using renjin and I'm trying to use values I generated in the java code with the r-code, for instance:

int x = 7;

try
{
   engine.eval("tmp<-c(x, 4)");
   engine.eval("print(tmp)");
}
catch (ScriptException ex) 
{
   ;
}

However, this does not work, as the engine apparently cannot work with x. Is there an easy way to solve this?


Solution

  • You can concatenate the variable into the string as a literal, as I posted in the comment:

    engine.eval("tmp<-c(" + x + ", 4)");
    

    This works because (I'm assuming) the engine needs to evaluate literal expressions (with number values instead of variable), and the above expression essentially passes tmp<-c(7, 4) through concatenation (combination) of the strings and integer value. I would try also first running a command to store a variable and then reference it, ie:

    engine.eval(x <- 7);
    

    Then try your original expression. I'm not familiar with Renjin, though, so it's a bit of a shot in the dark.