knitr&x27;s spin():无法在标题中嵌入内联R表达式

knitr&x27;s spin():无法在标题中嵌入内联R表达式,r,knitr,r-markdown,R,Knitr,R Markdown,我使用的函数将R脚本呈现为HTML(使用RStudio的“compilenotebook”命令) 虽然在纯文本中包含动态R表达式很容易,但这显然不适用于标题 例如,第一个示例将内联表达式正确嵌入到纯文本中: library(knitr) #' You can use the special syntax {{code}} to embed inline expressions, e.g. {{mean(x) + 2}} #' is the mean of x plus 2. library(

我使用的函数将R脚本呈现为HTML(使用RStudio的“compilenotebook”命令)

虽然在纯文本中包含动态R表达式很容易,但这显然不适用于标题

例如,第一个示例将内联表达式正确嵌入到纯文本中:

library(knitr)

#' You can use the special syntax {{code}} to embed inline expressions, e.g.
{{mean(x) + 2}}
#' is the mean of x plus 2.
library(knitr)
var0 <- "Sex"
var1 <- "Education"

#' # Crosstabulation of
{{var0}}
#' # by
{{var1}}
#' # .
但第二个示例在呈现HTML时,仅将标题样式应用于第一行(“的交叉列表”),然后将该行换行并恢复为纯文本:

library(knitr)

#' You can use the special syntax {{code}} to embed inline expressions, e.g.
{{mean(x) + 2}}
#' is the mean of x plus 2.
library(knitr)
var0 <- "Sex"
var1 <- "Education"

#' # Crosstabulation of
{{var0}}
#' # by
{{var1}}
#' # .
库(knitr)

var0我认为这是
spin()
的一个限制。您可以使用
brew
预处理该文件,或让R以标记方式输出标题

`%#%` <- function(tpl, vals) if(interactive()) vals else do.call(sprintf, c(fmt=tpl, vals)) 

var0 <- "Sex"
var1 <- "Education"

{{"# Crosstabulation of %s by %s " %#% list(var0, var1)}}

`%\%`谢谢。当然,它不是完全等价的(当作为纯R脚本运行时,{expr}}语法将被执行,而不是忽略;但它肯定解决了我的问题。顺便说一句:
library(knitr)
,最简单的例子可以归结为
{paste0(#crosstablation Of**),var0,**by**,var1,**}
我为interactive()添加了一个测试,以便在标准的R会话中不显示标题