I would like to read the .csv file into R. I have been able to send the absolute path of the file to R and it will return it via the $test variable in the PHP code below. The $r->evalString("data<-read.csv(filetim,header=TRUE)"); returns an error that I have listed below. Any ideas as to what is causing the error?
<?php
require_once 'rconfig.php';
require 'rConnection.php';
try {
# Connect to R via Rserve
echo '<p>Connecting to Rserve '.RSERVE_HOST;
$r = new Rserve_Connection(RSERVE_HOST);
echo ' Connection OK</p>';
# Associate filename with the file's absolute path
$r->evalString("filetim=('folder/folder/folder/file.csv')");
$r->evalString("filebob=('folder/folder/folder/file2.csv')");
# Check if the filename is being stored
$test = $r->evalString("filebob");
echo $test;
# Read the .csv file into variable "data"
$r->evalString("data<-read.csv(filetim,header=TRUE)");
$r->close();
} catch(Exception $e) {
echo $e;
}
?>
Error:
'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(17): Rserve_Connection->evalString('data<-read.csv(f...') #1 {main}
This solution assumes you're on a Linux system. For Windows (if you got Rserve to work...) the story is the same.
The path you gave to
read.csvis not an absolute, but a relative path. R starts from the working directory (by default often the user home directory on Linux systems, indicated in R by~)In your case,the absolute path would be
To find out what R considers the working directory, you can use
getwd(). Usingtry(), you can catch error messages inside R to get a better idea of what went wrong, as explained on the page of Rserve here : http://rforge.net/Rserve/faq.html#errorsYou can also use a
tryCatch()in R to get the R error message or the warning message. In the specific case ofread.csv()the warning message might be more useful than the error (which simply says "cannot open the connection")eg:
to catch the error, or
to catch the warning. This should give you an idea about where R is looking for your file. Note that you might have to wrap the whole call to tryCatch in
as.character(). I'm not sure if Rserve in combination with php can handle simpleWarning objects.