0

Hi I'm running an Rscript with PHP exec and it is behaving strangely.......It launches R but throws an error at the following line:-

filein = filein[,c("id","bank","trans_date","description","description_2",
                 "description_3","description_4","description_5" ,"type",
                 "debit","credit","statement_balance", "cleared_balance",
                 "debit_int_rate","credit_int_rate","category")]

This simple rearranges the columns in a data-set.

It throws the following error:-

Error in

[.data.frame`(filein, , c("id", "bank", "trans_date", "description", 
:  undefined columns selected

However I run the same script from command-line it runs without any error.

(I'm running the Rscript on a ubuntu 14.04 machine with PHP5......also when I run the same script on a windows machine from PHP it also runs perfectly)

Anybody have any ideas why this is?

Andrie
  • 176,377
  • 47
  • 447
  • 496
user2987739
  • 713
  • 2
  • 7
  • 9
  • Are you sure that the "_" character does not somehow get read in a different way when you import the dataframe using PHP5? -- check how the columns are named by adding a "print colnames(filein)". I think that it might be a text-parsing problem which alters the name of the columns. – selenocysteine Feb 12 '15 at 09:59
  • PHP runs under different rights than Rscript, so it may not have access to the file. – LauriK Feb 12 '15 at 10:21
  • @LauriK More precisely, *the web server* runs under different rights than *the user* (in other words, if the PHP script is manually run by the user on the console, `php` will run with exactly the same rights as `Rscript`). But the gist is the same. – Konrad Rudolph Feb 12 '15 at 10:27
  • @KonradRudolph Thanks! My web server sysadmin career lasted all of two months, so I have some basic knowledge and can debug permission and dependency problems, but I'm far from systematic understanding. – LauriK Feb 12 '15 at 10:32
  • 1
    Agree that it's likely a file permissions issue. Try adding a check when you read in the file to see if it read in anything... if(nrow(dataframe) == 0)stop("dataframe not read in properly") – cory Feb 12 '15 at 13:30
  • Thanks for all of your help....So it wasn't a permissions issue.......the issue was:----the command run from php exec was ( Rscript Myfile.r "http://localhost/categorisation/public/1423746975.json" ) However the argument that was received by R was "http://localhost/categorisation/public/1423746975.json2" For some reason the ubuntu/php combination added a 2 to the end of the argument string....when I stripped the 2 in the R script it ran correctly – user2987739 Feb 12 '15 at 15:00

1 Answers1

0

Thanks for all of your help....So it wasn't a permissions issue(I had fixed those previously).......the issue was:----The command passed to commandline by PHP exec was sudo /Rscript /home/xin/Documents/ClassificationApp/ClassificationAllInOne.R "http://localhost/categorisation/public/classification/data/1423746975.json"

However the argument that was received by R was "localhost/categorisation/public/1423746975.json2";

For some reason the Ubuntu/PHP combination added a 2 to the end of the argument string....I added a line in ClassificationAllInOne.R to strip the 2 and it worked perfectly

user2987739
  • 713
  • 2
  • 7
  • 9