tags:

views:

46

answers:

3

At the moment I am using a BATCH file that comprises of the following line of code:

c:\R\bin\Rcmd.exe BATCH "<filepath>/shares.R" 

It opens and runs the following R code:

library(ggplot2)
library (XML)
test.df <- xmlToDataFrame(file.choose())
test.df
sapply(test.df, class) 
test.df$timeStamp <- strptime(as.character(test.df$timeStamp), "%H:%M:%OS")
test.df$Price <- as.numeric(as.character(test.df$Price))
sapply(test.df, class)
options("digits.secs"=3)
summary (test.df)
with(test.df, plot(timeStamp, Price))
sd (test.df$Price)
mean(test.df$timeStamp)
test.df$timeStamp <- test.df[1,"timeStamp"] + cumsum(runif(7)*60)
summary(test.df)
qplot(timeStamp,Price,data=test.df,geom=c("point","line"))
Price <- summary(test.df$Price)
print (Price)
dput(test.df)

Is it possible to use a PHP form to first select the file and pass it as an argument into the r script there by replacing the file.choose() command, I will be running this from a localhost?

A: 

Maybe you want to start with the R FAQ on Web interfaces which describes a number of such web-to-R approaches.

Dirk Eddelbuettel
A: 

I have never used R before, however I can shed some light on the PHP side of things...

Short answer: no, not with a file picker.

Long answer: Though you can use a file picker (<input type="file">) to graphically select a file, it is impossible to actually retrieve the full filepath. You can only know the filename and extension (programmatically), due to security reasons.

You then have two options:

1) You can only select files from a pre-known selection of R scripts, using something like

<?php $available_r_scripts = array('shares.R','foo.R','bar.R'); ?>
Choose a script:
<select name="r_script">
<?php foreach ($available_r_scripts as $script) { ?>
    <option value="<?php echo $script ?>"><?php echo $script ?></option>
<? } ?>
</select>

2) You actually upload the R script. You would use a file-uploading form like normal, grab the uploaded filename on the server, then feed that to however you need to use it in your R script:

form.html

<form action="run_script.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="r_script">
    <input type="submit value="Run Script">
</form>

run_script.php

<?php
$filename = $_FILES['r_script']['tmp_name'];
//$filename now contains the full name and path of the script you just uploaded.

//call r script here
Austin Hyde
A: 

Is it possible to use a PHP form to first select the file and pass it as an argument into the r script there by replacing the file.choose() command

Yes - but you need to re-write your R script to use an argument instead. Given the simplicity of the code, I'd just generate a temporary script:

<?php

if (file_exists(BASE_DIR . $_GET['filename'])) {
   $out=tempnam();
   $in=file_get_cotents("<filepath>/shares.R");
   $in=str_replace("file.choose()", '"' . BASE_DIR . $_GET['filename'] . '"');
   file_put_contents($out, $in);
   print (passthru("c:/R/bin/Rcmd.exe BATCH \"$out\"");
   unlink($out);
} else {
   print htmlentities($_GET['filename']) . " was not found.";
} 
symcbean