Reputation: 1
I'm building a Web site using apache, PHP and R (a statistic programing language) to build the Logic Server. This program is on the same sarver as the apache. The client user use the Web site in order to use the App that we build with R. This is possible by using R_modul that was created for this purpose (so R can communicate with apache).
Here is a link of the R_modul http://rapache.net/
The problem:
We can use PHP to send an text in form of an R code and get in return the R App answer in text form. That is what the R_modul does.
But I don't know how to bring back plots (Graphs photos). Some functions in R provide plos as an output. That is what I want to show the user. But I don't know how to do that...
I guess I need to find were is the cache memory directory that hold the plot and return it as a pic . But I'm not sure...and if so, the plos are created per command and are endless in look.
Upvotes: 0
Views: 2741
Reputation: 28632
Simply save the plot to a (temp) file from R on the server and serve that image to the client.
You might be interested in the following threads on Rapache mailing list:
A basic example (using brew):
<html>
<body>
<%
filename <- paste(tempfile(tmpdir='/to/some/dir'), '.png', sep='')
png(filename)
plot(runif(10), runif(10))
dev.off()
%>
<img src="<%=filename%>"/>
</body>
</html>
Change '/to/some/dir'
In the above example to a directory which is readable by the web server and also writable by R.
Upvotes: 1
Reputation: 33782
You can do this without using PHP or writing a file on the server by creating an R tempfile() object to hold the plot, then using sendBin() to display the plot in the browser. Here's some sample R code:
setContentType("image/png")
t <- tempfile()
png(t,type="cairo")
plot(rnorm(100), col = "blue")
dev.off()
sendBin(readBin(t,'raw',n=file.info(t)$size))
unlink(t)
DONE
You can save that as e.g. "plot.R" in /var/www/R. Your Apache config (e.g. /etc/apache2/conf.d/rapache.conf) should look something like this:
LoadModule R_module /usr/lib/apache2/modules/mod_R.so
<Location "/R">
ROutputErrors
SetHandler r-script
RHandler sys.source
</Location>
Upvotes: 3
Reputation: 36080
@Yoni, please don't cross-post! We already answered similar question on RApache mailing list. You should also try to provide an accurate description of your problem.
As far as I understood, you need to retrieve an image generated with R on the server side in order to display it on the client side (i.e. in a browser). If so, here goes:
P.S.
If you decide to use brew
, don't set Apache <Directory>
directive to the folder where the images are stored, since RApache will try to process all files in a directory with brew
function. Use <FilesMatch>
Apache directive with regular expressions, since it's flexible. Name your files systematically (put a prefix or smth) and use AJAX. You don't really need brew
, since the job can be done with sys.source
. Try to refrain from HTML + R/brew
mash-up code. Return JSON and unserialise it with JavaScript.
Upvotes: 3