![](/img/trans.png)
[英]Using knitr (from .Rhtml to html): how to embed a link in an R Figure?
[英]Writing html from within R using knitr
也许我错过了显而易见的事情,但我一直在努力寻找以下示例:我想使用knitr
包将R中的分析报告写入html文件。 我已经找到了stitch()
函数,但是如果能够更好地控制将哪些结果和图表写入html以及哪些不是。 原则上我希望能够编写以下代码:
# some dummy code
library(ggplot)
data <- read.table('/Users/mydata', header=TRUE)
model <- lm(Y~X*Y, data)
# write this result to html:
summary(model)
我想我不明白你到底缺少什么,但这里有一个我做过的最小例子。 要运行它
library(knitr)
knit("r-report.html")
而这个例子。
<HTML>
<HEAD>
<TITLE>Analyzing Diamonds!</TITLE>
</HEAD>
<BODY>
<H1>Diamonds are everywhere!</H1>
<!--begin.rcode echo=FALSE
## Load libraries, but do not show this
library(ggplot2)
library(plyr)
testData <- rnorm(1)
end.rcode-->
This is an analysis of diamonds, load the data.<p>
<!--begin.rcode echo=TRUE, fig.keep="all"
# Load the data
data(diamonds)
# Preview
head(diamonds)
end.rcode-->
Generate a figure, don't show code <p>
<!--begin.rcode echo=FALSE, fig.align="center", dev="png"
# This code is not shown, but figure will output
qplot(diamonds$color, fill=diamonds$color) +
opts(title="A plot title")
end.rcode-->
Show some code, don't output the figure<p>
<!--begin.rcode echo=TRUE, fig.keep="none"
# Show the code for this one, but don't write out the figure
ggplot(diamonds, aes(carat, price, colour=cut)) +
geom_point(aes(alpha=0.9))
end.rcode-->
And the value testData: <!--rinline testData --> inside a text block.
</BODY>
</HTML>
在R中编写HTML比在编写模板和knit()
时更加费力(@dready给出了一个不错的例子)。 代码会相当丑陋,你会看到许多“猫”跳来跳去。 你最终会得到这样的东西:
sink('test.html') # redirect output to test.html
cat('<h1>First level header</h1>\n')
cat('<pre>')
summary(mtcars)
cat('</pre>\n')
sink()
browseURL('test.html')
无论如何,还有另一个包R2HTML
,在这种情况下可能更合适。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.