简体   繁体   English

knitr的spin():不能在标题中嵌入内联R表达式

[英]knitr's spin(): cannot embed inline R expression in a heading

I'm using knitr 's spin() function to render an R script to HTML (using RStudio's "Compile Notebook" command). 我正在使用knitrspin()函数将R脚本呈现为HTML(使用RStudio的“Compile Notebook”命令)。

While it's easy to include a dynamic R expression in plain text, this apparently does not work for a heading. 虽然在纯文本中包含动态R表达式很容易,但这显然不适用于标题。

For example, this first example correctly embeds an inline expression into plain text: 例如,第一个示例正确地将内联表达式嵌入到纯文本中:

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.

But this second example, when rendering HTML, only applies the heading style to the first line ("Crosstabulation of"), and then wraps the line and reverts back to plain text: 但是第二个例子,在呈现HTML时,只将标题样式应用于第一行(“交叉表”),然后包装该行并恢复为纯文本:

library(knitr)
var0 <- "Sex"
var1 <- "Education"

#' # Crosstabulation of
{{var0}}
#' # by
{{var1}}
#' # .

How can I have the heading style applied to all the lines of the chunk? 如何将标题样式应用于块的所有行? Is there any alternative syntax I have to use? 有什么替代语法我必须使用? What am I missing? 我错过了什么?

I think it's a limitation of spin() . 我认为这是spin()的限制。 You could pre-process the file with brew , or get R to output the header in markdown, 您可以使用brew预处理文件,或者让R在markdown中输出标题,

`%#%` <- 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)}}

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

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