在 rApache 中使用绘图

Using plots within rApache

我正在使用 rApache 显示在 R 中创建的封装图。现在我必须面对的只有一个问题。 如果文档中只有嵌套 R 代码,我认为 HTML 文件会呈现为某种单一的 png 图像。

但是,我希望将其呈现为包含 图形绘图的文档。因此,当我在 <% ... %> 标签之前或之内添加 HTML 内容时,我得到一个损坏的图像标志作为输出。

我怎样才能做到这一点,我可以在 HTML 文档中使用 plot 命令?

<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)
%>

我的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>

在阅读了一些关于在 R 中创建文件之后,我找到了以下解决方案作为一个非常简单的解决方法:

// 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">

我想我试过的第一个代码示例是为文档的 R-Handler 编写的,而不是特定目录中的 r-script 选项。