-1

I'm using R (http://www.r-project.org/) on a LAMP system with Rserve-php (https://code.google.com/p/rserve-php/). I would like to send a .csv file to R. Is this possible?

I tried using evalString(), but it did not work and returned the error below. The connection to R and the required files are okay. The output returns Hello World! as expected.

evalString() from rcodetest.php:

<?php
require_once 'rconfig.php';
require 'rConnection.php';
//$test_cases = Rserve_Tests_Definition::$native_tests;
try {
    echo '<p>Connecting to Rserve '.RSERVE_HOST;
    $r = new Rserve_Connection(RSERVE_HOST);
    echo ' OK</p>';
    $result = $r->evalString('x ="Hello World!"; x');
    echo $result;
    $filex = $r->evalString("filename=('folder\\folder\\folder\\file.csv')");
    $r->close();
} catch(Exception $e) {
    echo $e;
}
?>

Error:

exception 'Rserve_Exception' with message 'Unexpected packet Data type (expect DT_SEXP)' in /path/rConnection.php:201 Stack trace: #0 /path/rConnection.php(237): Rserve_Connection->parseResponse(NULL, 0) #1 /path/rcodetest.php(14): Rserve_Connection- >evalString('filename=('fold...') #2 {main}

1 Answers1

0

Try using read.table. This is if you are trying to process the .csv file.

$filex = $r->evalString("read.table('folder/folder/folder/file.csv', sep = ',')");



<?php
require_once 'rconfig.php';
require 'rConnection.php';
//$test_cases = Rserve_Tests_Definition::$native_tests;
try {
    echo '<p>Connecting to Rserve '.RSERVE_HOST;
    $r = new Rserve_Connection(RSERVE_HOST);
    echo ' OK</p>';
    $result = $r->evalString('x ="Hello World!"; x');
    echo $result;
    $filex = $r->evalString("read.table('folder/folder/folder/file.csv', sep = ',')");
    $r->close();
} catch(Exception $e) {
    echo $e;
}
?>

Using

filename=('folder/folder/folder/file.csv')

... is only assigning the string value to filename.

> filename=("~/config.yml")
> filename
[1] "~/config.yml"
> typeof(filename)
[1] "character"

Also, the preferred assignment operator in R is <-.

SethB
  • 2,060
  • 1
  • 14
  • 18
  • Tried `read.table`. Unfortunately, it still returns the same error. – user2932733 Dec 19 '13 at 20:35
  • That solved the "Unexpected Packet Data Type" error. Now it's just "unable to evaluate". `exception 'Rserve_Exception' with message 'unable to evaluate' in /folder/folder/folder/folder/folder/rConnection.php:239 Stack trace: #0 /folder/folder/folder/folder/folder/rcodetest.php(13): Rserve_Connection->evalString('read.table('fol...') #1 {main}` – user2932733 Dec 19 '13 at 21:01
  • Are you able to execute: `"x = read.table('folder/folder/folder/file.csv', sep = ',')"` – SethB Dec 19 '13 at 21:07
  • What are the results of `$filex = $r->evalString("file('folder/folder/folder/file.csv')")` – SethB Dec 19 '13 at 21:32
  • Returns the "unable to evaluate" error. With the trouble it's currently having with a .csv, is there a way that I can just read the table in from MySql? – user2932733 Dec 20 '13 at 13:11