PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/RCallerPhp/RCode.php

https://code.google.com/p/rcaller/
PHP | 117 lines | 61 code | 21 blank | 35 comment | 0 complexity | 8ab1f97cd402adbd7052da0272631e96 MD5 | raw file
  1. <?php
  2. /*
  3. *
  4. RCallerPhp, A solution for calling R from Php
  5. Copyright (C) 2010,2011 Mehmet Hakan Satman
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. *
  18. * Mehmet Hakan Satman - mhsatman@yahoo.com
  19. * http://www.mhsatman.com
  20. * Google code projec: http://code.google.com/p/rcaller/
  21. *
  22. */
  23. include_once ("Globals.php");
  24. class RCode {
  25. private $code;
  26. function setCode($sb) {
  27. $this->code = $sb;
  28. }
  29. public function getCode() {
  30. return($this->code);
  31. }
  32. function __construct($sb) {
  33. $this->code = $sb;
  34. $this->clear();
  35. }
  36. public function clear() {
  37. $this->code = "";
  38. $this->addRCode("packageExist<-require(Runiversal)");
  39. $this->addRCode("if(!packageExist){");
  40. $this->addRCode("install.packages(\"Runiversal\", repos=\" " . Globals::$cranRepos . "\")");
  41. $this->addRCode("}\n");
  42. }
  43. public function prependRCode($code) {
  44. $this->code = $code . $this->code . "\n";
  45. }
  46. public function addRCode($code) {
  47. $this->code .= $code . "\n";
  48. }
  49. public function addStringArray($name, $arr) {
  50. //CodeUtils.addStringArray(code, name, arr, false);
  51. CodeUtils::addNumericArray($this->code, $name, $arr, false);
  52. }
  53. public function addNumericArray($name, $arr) {
  54. CodeUtils::addNumericArray($this->code, $name, $arr, false);
  55. }
  56. public function startPlot() {
  57. $f = CodeUtils::createTempFile("RcallerPlot");
  58. //addRCode("png(\"" + f.toString().replace("\\", "/") + "\")");
  59. /*
  60. * File path seperator may be buggy in Windows. Look here again
  61. */
  62. $this->addRCode("png(\"" . $f . "\")");
  63. return ($f);
  64. }
  65. public function endPlot() {
  66. $this->addRCode("dev.off()");
  67. }
  68. public function getPlot($f) {
  69. //data:image/png;base64
  70. $content = file_get_contents($f);
  71. $b = base64_encode($content);
  72. $html_text = "<img src=\"data:image/png;base64, ".$b."\"/>";
  73. return $html_text;
  74. }
  75. public function showPlot($f) {
  76. /*
  77. ImageIcon plot = getPlot(f);
  78. RPlotViewer plotter = new RPlotViewer(plot);
  79. plotter.setVisible(true);
  80. */
  81. throw new Exception("This is not implemented yet", "RCode.php", "showPlot()");
  82. }
  83. public function R_require($pkg) {
  84. $this->code = "require(" . $pkg . ")\n" . $this->code;
  85. }
  86. public function R_source($sourceFile) {
  87. $this->addRCode("source(\"" + $sourceFile + "\")\n");
  88. }
  89. public function toString() {
  90. return $this->code;
  91. }
  92. }
  93. ?>