简体   繁体   English

RMarkdown文档-如何延迟对内联代码段的编织器评估,直到以后的代码块处理完毕?

[英]RMarkdown document - How to delay knitr evaluation of an inline code piece until later chunks have processed?

I've created an rmarkdown report, with a bunch of code chunks. 我创建了一个带有大量代码块的rmarkdown报告。 I'm now creating a summary front page for it, and would like to include an inline calculation like 我现在正在为此创建一个摘要首页,并希望包括一个内联计算,例如

Blah blah blah summary stuff.... We found the mean to be `r mean(some_object_from_the_report)`. Blah blah blah more summary stuff.

Being at the start of the RMD file, some_object_from_the_report doesn't exist yet. 在RMD文件的开头,some_object_from_the_report不存在。 Is there any way to tell knitr to hold off evaluating that code snippet until after the later items have all been computed? 有什么方法可以让knitr推迟评估该代码段,直到计算完所有后续项为止?

Thanks for any tips! 感谢您的提示!

EDIT: 编辑:

Suggestion as comment is to set echo=false in knitr options. 建议将注释设置为knitr选项中的echo = false。 Either I'm doing it wrong, or it doesn't help my situation. 我做错了,或者对我的处境没有帮助。 The following short example illustrates this. 以下简短示例说明了这一点。

---
title: "Minimal test of delayed evaluation"
author: "sff"
date: "December 13, 2017"
output: html_document
---

```{r setup, include=TRUE}
knitr::opts_chunk$set(echo = FALSE)
```

## Summary

Summary of blahblahblah. Also here's a mean from the report: `r mean(testobj)`.


## Report

```{r report_stuff}
testobj <- c(1, 2, 3)
```

Knitr throws an object not found error. 编织器引发未找到对象错误。 Am I implementing the suggestion wrongly, or does the suggestion not achieve what I'm looking for? 我是错误地实施了建议,还是建议没有实现我想要的?

Here is a simple working example, you set a first chunk before the abstract, it can be set at the very beginning of the document if you need to adjust other thigs like \\graphicspath{} . 这是一个简单的工作示例,您在摘要之前设置了第一个块,如果需要调整\\graphicspath{}类的其他\\graphicspath{} ,则可以在文档的开头设置它。

In this chunk you create a list if the Rdata file containing the list does not exist. 如果包含该列表的Rdata文件不存在,则在此块中创建一个列表。 You need to populate it with the values called in the text. 您需要使用文本中调用的值填充它。

When first running the example you get 第一次运行示例时,您会得到 首轮

On the second run you get 在第二次运行中,您得到

第二次

Note that this way, you can avoid running long calculation and simply save their results. 请注意,通过这种方式,您可以避免进行长时间的计算,而只需保存其结果即可。

\documentclass{article}    
\title{Testing how to save results for the abstract}
\author{}
\begin{document}
% this chunk comes just after begin{document}
<< init, echo=FALSE, eval=TRUE,results="hide" >>=
require(knitr)
summary_list_path <-paste0(getwd(),"/data/summary_list.Rdata")
if (!file.exists(summary_list_path)){
  summary_list<-list()
  summary_list[["A"]]<-list()
  summary_list[["B"]]<-list()
  summary_list[["A"]][["N"]]<-NA
  summary_list[["A"]][["p"]]<-NA
  summary_list[["B"]][["text"]]<-"extremely sad"
} else {
  load(summary_list_path)
}
@
\maketitle
\begin{abstract}
My population is \Sexpr{summary_list[["A"]][["N"]]} and the p value was \Sexpr{summary_list[["A"]][["p"]]} as a result I am \Sexpr{summary_list[["B"]][["text"]]}
\end{abstract}

<<chunk_1, echo=FALSE, eval=TRUE,results="hide" >>=
summary_list[["A"]][["N"]]<-20
summary_list[["A"]][["p"]]<-0.05
# save your list at the end of each chunk, that was you can also avoid 
# processing everyting.
save(summary_list,file=summary_list_path)
@

<<chunk_2, echo=FALSE, eval=TRUE,results="hide" >>=
summary_list[["B"]][["text"]]<-"happy"
save(summary_list,file=summary_list_path)
@

\end{document}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM