Maître de Conférences en Informatique à l'Université d'Angers
Ce site est en cours de reconstruction certains liens peuvent ne pas fonctionner ou certaines images peuvent ne pas s'afficher.
The simplest command to output data is print() but if you want to print many things inside print you will need to use the paste() command.
> print("hello world")
[1] "hello world"
> x <- 1
> print(paste("hello world, x=", x))
[1] "hello world, x= 1"
# example for which print is not recommended
> x <- c(1:3)
> print(paste("hello world, x=", x))
[1] "hello world, x= 1" "hello world, x= 2" "hello world, x= 3"
Use write.table() or write.csv() to save a data frame.
mydata <- data.frame(col1 = c(1:3), col2 = c(4:6))
> mydata
col1 col2
1 1 4
2 2 5
3 3 6
> write.table("outputfile.RData")
richer@nano:~/dev/R$ cat outputfile.RData
"col1" "col2"
"1" 1 4
"2" 2 5
"3" 3 6
> mydata <- data.frame(col1 = rnorm(3), col2 = rnorm(3))
> mydata
col1 col2
1 0.1251068 0.3132729
2 1.3635986 1.0170974
3 -0.2699093 1.0902407
> write.fwf(mydata, width=c(12,12))
col1 col2
0.1251068 0.3132729
1.3635986 1.0170974
-0.2699093 1.0902407
write.table(mydata, "mydata.Rdata", sep="\t")
# or save in CSV format
write.csv(mydata, "mydata.csv")
Use the gridExtra library.
> install.packages("gridExtra")
> library(gridExtra)
> pdf("mydata.pdf", height=11, width=8.5)
> grid.table(mydata)
> dev.off()
The result is the following: mydata.pdf
Download PHPR and install it in the directory where you will call R your public_html directory.
Install requirements using composer, you should have the composer.json file of PHPR in this directory.
richer@nano:~/public_html\$ composer install
### example #####
public_html
├── call_R.php
├── composer.json
├── src <-- directory in which you will have Kachkaev/PHPR
└── vendor <-- directory in which you will have autoload.php
Then write the following code but modify the path to R depending on your system:
<?php
require_once 'vendor/autoload.php';
use Kachkaev\PHPR\RCore;
use Kachkaev\PHPR\Engine\CommandLineREngine;
$r = new RCore(new CommandLineREngine('/usr/bin/R'));
$result = $r->run(<<<EOF
x = c(1,2,3)
y = c(4,5,6)
z = x + y
z
EOF
);
echo "<pre>$result</pre>";
?>
You should obtain the following result (although I have noticed on my computer that in some occasions you won't get the last two lines):
