0
    //poorman.php
    <?php
    echo "<form action='poorman.php' method='get'>";
    echo "Number values to generate: <input type='text' name='N' />";
    echo "<input type='submit' />";
    echo "</form>";

    if(isset($_GET['N']))
    {
      $N = $_GET['N'];

      // execute R script from shell
      // this will save a plot at temp.png to the filesystem
      exec(".\R\R-3.1.3\bin\Rscript .\R\R-3.1.3\bin\my_rscript.R $N");

      // return image tag
      $nocache = rand();
      echo("<img src='temp.png?$nocache' />");
    }
    ?>
    //my_rscript.R
    args <- commandArgs(TRUE)

    N <- args[1]
    x <- rnorm(N,0,1)

    png(filename="temp.png", width=500, height=500)
    hist(x, col="lightblue")
    dev.off()

This is a simple programe in which r script is included in php and run in localhost.But i wanted to run this code in linux server so how it is possible can anyone explain me please..?

1 Answers1

0

Debian or Ubuntu Install R first:

sudo apt-get update
sudo apt-get install r-base

Then you got R in your server.

Write your R script called new.r

Then execute shell_exec('Rscript new.r');

What about passing parameter to R?

Refer to How can I read command line parameters from an R script?

Content of new.r:

options(echo=FALSE)
args <- commandArgs(trailingOnly = TRUE)
print(args)

shell_exec('Rscript new.r Hello'); will return:

[1] "Hello"
Community
  • 1
  • 1
Shiji.J
  • 1,561
  • 2
  • 17
  • 31