tc / RMongo

R client to interface with MongoDB
102 stars 34 forks source link

RMongo messes the R script output stream #47

Open rootkea opened 7 years ago

rootkea commented 7 years ago

my_script.R:

#!/usr/bin/env Rscript

i <- as.numeric(commandArgs(TRUE))
print(i:10)

index.php

<?php
    $n = 3;
    exec("Rscript my_script.R $n", $out);
    var_dump($out);
<?

Output:

array(1) { [0]=> string(27) "[1] 3 4 5 6 7 8 9 10" } 

Shell Output:

$ Rscript my_script.R 3
Loading required package: rJava
Loading required package: methods
[1]  3  4  5  6  7  8  9 10

Now I need to have the database connection and other processing in my_script.R before I print the result. So Modified my_script.R:

#!/usr/bin/env Rscript

i <- as.numeric(commandArgs(TRUE))
rootkea <- mongoDbConnect('rootkea')
print(i:10)

Output:

array(0) { }

Shell Output:

$ Rscript my_script.R 3
Loading required package: rJava
Loading required package: methods
[1]  3  4  5  6  7  8  9 10

Though I get the shell output as expected, RMongo messes something with php. How do I make RMongo not mess up with the value returned in $out argument?

tc commented 7 years ago

I can't think of a good way to solve this.