Skip to content

Instantly share code, notes, and snippets.

@abelcallejo
Last active May 8, 2019 02:39
Show Gist options
  • Save abelcallejo/301f156713f51cceb527f50565f262e6 to your computer and use it in GitHub Desktop.
Save abelcallejo/301f156713f51cceb527f50565f262e6 to your computer and use it in GitHub Desktop.
This is a great alternative on building web services that display chart/graph images rendered in PNG.
<?php
// Use R to generate the graph and store it to "/tmp" directory
exec("Rscript /path/to/hello.r");
// Use PHP to display the generated graph
header('Content-Type: image/jpg');
$im = imagecreatefromjpeg('/tmp/rplot.jpg');
imagejpeg($im);
imagedestroy($im);
?>
jpeg('/tmp/rplot.jpg')
cars <- c(1, 3, 6, 4, 9)
plot(cars)

Generating graph/chart using R and PHP

php r

Google Image Charts API is dead! This is a great alternative on building web services that display chart/graph images rendered in PNG.

Files

  • hello.r - generates the graph
  • hello.php - displays the graph
  • rplot.jpg - temporary file output that gets generated by R of which later is read by PHP

Instructions

  1. Open your web browser; and
  2. In the address bar, visit to http://localhost/path/to/hello.php
  3. It should display the look of the graph/chart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment