Example 1 -- PHP communicate with R

This page is for Linux only!!
  1. PHP code (ex_1.php)

        
    <?
    
      $cmd = "echo 'argv <- \"ex_1.r\"; source(argv)' | " .
             "/usr/bin/R --vanilla --slave";
      $ret = system($cmd);
      echo $ret;
    
    ?>
    

    This PHP file use shell command echo pipe arguments to R and accept the return from R and show it on web page.

  2. R script (ex_1.r)

         print(argv)

    This R script just print the arguments.

  3. Browse
    Put the "ex_1.php" and "ex_1.r" into the same directory and open web browser to link "ex_1.php".
    Click here to see the output return from R script "ex_1.r".