简体   繁体   中英

how can i run php+rscript on linux server?is it possible?And if possible then how?

    //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..?

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM