gpt4 book ai didi

r - 使用 knitr 从 R 中编写 html

转载 作者:行者123 更新时间:2023-12-04 10:20:13 26 4
gpt4 key购买 nike

也许我错过了显而易见的事情,但我一直在努力寻找以下示例:我想使用 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 - 使用 knitr 从 R 中编写 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10367293/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com