0

I have this code in the .php file:

$nb1 = 1;
$nb2 = 2;
exec("C:/xampp/phpR/plotR.r $nb1 $nb2", $response);
echo $response;

And I have in the .r file:

args <- commandArgs(TRUE)
x<-args[1]+args[2]
print(x)

The php code prints "array" on browser. It should print "3". It prints "array". Where are the problem? Thanks

ABA
  • 435
  • 1
  • 5
  • 13
  • 2
    Not even sure what R is so can't comment on what it should return, but in your PHP try print_r($response) instead of echo $response and it'll print the array structure so you can see what it is. – LP Papillon Apr 17 '14 at 15:53
  • hello Papillon, nop, the code receive a empty array. In fact i've use print_r() and var_dump() – ABA Apr 17 '14 at 16:23

2 Answers2

0

PHP is interpreting the response from your R script as an array, whether the R script is actually returning an array I'm not sure. Regardless, if you do var_dump($response) you'll likely get something like

array (size=1)
  0 => int 3

I think you should be able to substitute echo $response[0]; for echo $response; and get the results you need.

WinterMute
  • 519
  • 3
  • 9
0

If anyone else happens upon this unanswered question, I had the same problem, and it turned out to be a permissions issue. The user that my webserver was running as didn't have permission to run my R script.

This can be diagnosed by following the excellent directions at: How to retrieve PHP exec() error responses? and viewing the error.

To get things to work nicely, I also changed the output of my R script from print(x) to write(x, stdout()). While print(x) would give you [1] 3 as an output, write(x) would return 3.

afszcz
  • 1
  • 1