Can `filename` strings be sent to R via Rserve with PHP?

455 views Asked by At

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

There are 1 answers

5
SethB On

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