简体   繁体   中英

Passing an array from php via command line to R

I have a problem, e am executing a R script from php via command line, and i need to give it two arrays for calculation.

I call the script by running:

Rscript nls.R ??? ???

??? and ??? are my arrays that i need to "give" to R, in order for it to calculate certain values.

Anyone knows how to do this? It is not limited to php, because it is command line - i just need to know if an array can be passed to R via command line and how.

How would R catch it, with what command?

Thanks a lot.

Regards

The command you are looking for is commandArgs() .

Now, if you have for example a list if integers separated by commas in a string, you can get the integers

s = '1,2,3,4,5'
your_list = lapply(strsplit(s, ','), as.numeric)[[1]]

There might be more straight-forward ways achieving this.

EDIT:

better example (should also work with Rscript)

$ R "1,2,3,4,5"
...

>lapply(strsplit(commandArgs()[[2]], ','), as.numeric)[[1]]
[1] 1 2 3 4 5

Assuming you want to run R in a process of serving a web page, there are some better ways of invoking R than command line; the problem is that R interpreter starts very slow and you are wasting lots of time and CPU power to start it over and over again.

You may for instance make a small R server with triggr and talk to it using a client made with PHP sockets.
The simpler yet heavier idea is to make an rApache app and either talk to it with CURL or use it directly as AJAX or even HTML provider.

You'll have to adapt your R script and include a call to commandArgs() . When used with the option trailingOnly=TRUE , it will return a character vector with the space-separated arguments after the call. This will allow you to further manipulate the arguments.

Given a script myscript.r:

#My script
x <- commandArgs(trailingOnly=TRUE)
print(x)
print(str(x))

You can call from the command line Rscript myscript.r 1 2 3 and get

Loading required package: ...
...
[1] "1" "2" "3"
 chr[1:3] "1" "2" "3"
NULL

F:\Temp

This gives you the possibility to pass names of text files with your arrays to the Rscript. If I combine different languages, I usually use text files to save intermediate results, it makes things go a bit more smooth.

Beware, if you use any of the options of Rscript (eg --no-save), you have to put them before the script, eg Rscript --no-save myscript.r

You can't pass such things (well, not easily) to Rscript . Rscript has the -e option, of which there can be more than one, which are R expressions. So you could get PHP to produce a character string that is an R expression creating your arrays, and pass each expression to create an array via separate -e arguments.

You can also pass in command line arguments that the R function commandArgs() can grab and make available for you. See an example here , but you may have to play around with how the arguments get pass in and evaluated by R.

Depending on the size of the arrays, the above more than likely won't be useful. In which case you will have to look at other ways of communicating with R than via Rscript .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM