rrb*_*est 13 r knitr r-markdown
在代码本身计算这些值之前,是否有标准方法在编写的knitr报告中尽早包含变量的计算值?目的是在报告的顶部创建一个执行摘要.
例如,像这样的东西,其中variable1和variable2直到后来才定义:
---
title: "Untitled"
output: html_document
---
# Summary
The values from the analysis are `r variable1` and `r variable2`
## Section 1
In this section we compute some values. We find that the value of variable 1 is `r variable1`
```{r first code block}
variable1 <- cars[4, 2]
```
## Section 2
In this section we compute some more values. In this section we compute some values. We find that the value of variable 2 is `r variable2`
```{r second code block}
variable2 <- cars[5, 2]
```
Run Code Online (Sandbox Code Playgroud)
一个简单的解决方案是knit()从新的Rgui会话中简单地两次文档.
第一次,内联R代码将触发一些关于无法找到的变量的抱怨,但是将评估块,并且它们返回的变量将保留在全局工作空间中.第二次,内联R代码将找到这些变量,并在没有投诉的情况下替换它们的值:
knit("eg.Rmd")
knit2html("eg.Rmd")
## RStudio users will need to explicitly set knit's environment, like so:
# knit("eg.Rmd", envir=.GlobalEnv)
# knit2html("eg.Rmd", envir=.GlobalEnv)
Run Code Online (Sandbox Code Playgroud)

注1:在这个答案的早期版本中,我曾建议过knit(purl("eg.Rmd")); knit2html("eg.Rmd").这具有(次要)优点,即第一次不运行内联R代码,但具有错过knitr缓存功能的(可能是主要的)缺点.
注2(对于Rstudio用户): RStudio需要显式,envir=.GlobalEnv因为,如此处所述,它默认knit()在单独的进程和环境中运行.它的默认行为旨在避免触及全局环境中的任何内容,这意味着第一次运行不会在第二次运行可以找到它们的任何地方留下所需的变量.
这是另一种方法,它使用brew+ knit。这个想法是knitr先对文档进行一次传递,然后运行它brew。您可以通过将该brew步骤引入为文档挂钩(在knitr完成后运行)来自动化此工作流程。请注意,您必须使用brew 标记<%= variable %>来就地打印值。
---
title: "Untitled"
output: html_document
---
# Summary
The values from the analysis are <%= variable1 %> and
<%= variable2 %>
## Section 1
In this section we compute some values. We find that the value of variable 1
is <%= variable1 %>
```{r first code block}
variable1 = cars[6, 2]
```
## Section 2
In this section we compute some more values. In this section we compute
some values. We find that the value of variable 2 is <%= variable2 %>
```{r second code block}
variable2 = cars[5, 2]
```
```{r cache = F}
require(knitr)
knit_hooks$set(document = function(x){
x1 = paste(x, collapse = '\n')
paste(capture.output(brew::brew(text = x1)), collapse = '\n')
})
```
Run Code Online (Sandbox Code Playgroud)
使用 chunk 选项这变得非常容易ref.label。见下文:
---
title: Report
output: html_document
---
```{r}
library(pixiedust)
options(pixiedust_print_method = "html")
```
### Executive Summary
```{r exec-summary, echo = FALSE, ref.label = c("model", "table")}
```
Now I can make reference to `fit` here, even though it isn't yet defined in the script. For example, a can get the slope for the `qsec` variable by calling `round(coef(fit)[2], 2)`, which yields 0.93.
Next, I want to show the full table of results. This is stored in the `fittab` object created in the `"table"` chunk.
```{r, echo = FALSE}
fittab
```
### Results
Then I need a chunk named `"model"` in which I define a model of some kind.
```{r model}
fit <- lm(mpg ~ qsec + wt, data = mtcars)
```
And lastly, I create the `"table"` chunk to generate `fittab`.
```{r table}
fittab <-
dust(fit) %>%
medley_model() %>%
medley_bw() %>%
sprinkle(pad = 4,
bg_pattern_by = "rows")
```
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1333 次 |
| 最近记录: |