rrapache

Using plots within rApache


I'm working with rApache for displaying encapsulated plots that were created in R. Right now there's only one problem that I have to face. If there's only nest R code within the document, the HTML file get's rendered as some kind of single png image I think.

However, I want that it gets renderd as a document that contains graphical plots. So when I add HTML content before or within the <% ... %> Tags, I get an broken image sign as an output.

How can I make it happen, that I can use the plot command within an HTML document?

<h1> Plot Content </h1> // adding this causes a broken image

<%
setContentType("image/png")
 t  <- tempfile() 
png(t,type="cairo") 

rndDistribution <- rnorm(100)

 plot(rndDistribution) 

 dev.off() 
sendBin(readBin(t,'raw',n=file.info(t)$size)) 
unlink(t)
%>

My apache.conf:

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>


<Directory /var/www/html/R>
    SetHandler r-script
    RHandler brew::brew
</Directory>

Solution

  • After reading a bit about file creation in R, I came to the following solution as a very simple workaround:

    // 1. creating the image of the plotted diagramm:
    <%
    setwd("/var/www/html/images/R")
    getwd()
    png(filename="plot.png")
    
    rndDistribution <- rnorm(100)
    
    plot(rndDistribution) 
    
    dev.off() 
    %>
    
    // 2. display graphic:
    <h1> Plot Content </h1>
    <img src="/images/R/plot.png">
    

    I guess the first code example I've tried was made for a R-Handler for documents, rather than the r-script option within an specific directory.