如何强制Knitr在所有其他代码块之后评估\ Sexpr

编程入门 行业动态 更新时间:2024-10-23 23:29:24
本文介绍了如何强制Knitr在所有其他代码块之后评估\ Sexpr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试为动态文档编写摘要,但是我的\Sexpr{}调用无法正常工作.

I am trying to write an abstract for a dynamic document, but my \Sexpr{} calls are not working.

基本上,我要做的就是从一个具有\Sexpr{value}生成的p值的摘要开始文档,其中的值被确定为文档中的下游".例如

Essentially all I am trying to do is start the document off with an abstract that has p-values generated from \Sexpr{value} where value is determined "downstream" in the document. For example

这有效:

\begin{document} <<foo>>= value = 10 @ Today I bought \Sexpr{value} Salamanders \end{document}

这不起作用(以及我要完成的工作)

This does not work (and what I am trying to accomplish)

\begin{document} Today I bought \Sexpr{value} Salamanders <<foo>>= value = 10 @

推荐答案

我没有看到直接的解决方案来在评估代码块之后推迟对\Sexpr的评估,但是将\Sexp与值结合使用仍然很容易例如,稍后在摘要中定义:使用单独的文件(myabstract.Rnw)作为摘要,在应该包含摘要的位置添加\input{myabstract},并在主文档的最后添加knit myabstract.Rnw :

I don't see a straightforward solution to postpone evaluation of \Sexpr after evaluation of code chunks, but it is still easy to use \Sexp with values defined later in, for example, an abstract: Use a separate file (myabstract.Rnw) for the abstract, add \input{myabstract} where the abstract is supposed to be included and knit myabstract.Rnw at the very end of the main document:

document.Rnw:

document.Rnw:

\documentclass{article} \begin{document} \begin{abstract} \input{myabstract} \end{abstract} Main text. <<>>= answer <- 42 @ \end{document} <<include = FALSE>>= knit("myabstract.Rnw") @

myabstract.Rnw:

myabstract.Rnw:

The answer is \Sexpr{answer}.

了解其工作原理的关键是要认识到knitr在LaTeX之前先处理文档.因此,LaTeX命令\input{myabstract}包含myabstract.tex"before"(不是指时间,而是指行号)并不重要,knit("myabstract.Rnw")会生成myabstract.tex.

Key to understanding how this works is to realize that knitr processes the document before LaTeX does. Therefore, it doesn't matter that the LaTeX command \input{myabstract} includes myabstract.tex "before" (not referring to time but referring to the line number), knit("myabstract.Rnw") generates myabstract.tex.

对于更复杂的场景,可以将评估和输出分开:在所有早期计算中进行所有计算,并将结果打印在其所属的位置.要显示源代码,请重用块(设置eval = FALSE).使用上面的示例,这意味着:

For more complex scenarios, evaluation and output could be separated: Do all the calculations in early chunks and print the results where they belong. To show source code, reuse chunks (setting eval = FALSE). Using the example from above, that means:

\documentclass{article} \begin{document} <<calculation, include = FALSE>>= answer <- 42 @ \begin{abstract} The answer is \Sexpr{answer}. \end{abstract} Main text. <<calculation, eval = FALSE>>= @ \end{document}

更多推荐

如何强制Knitr在所有其他代码块之后评估\ Sexpr

本文发布于:2023-10-14 11:59:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1490999.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   Knitr   Sexpr

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!