knitr 的 spin():无法在标题中嵌入内联 R 表达式
knitr's spin(): cannot embed inline R expression in a heading
我正在使用 knitr's spin() 函数将 R 脚本呈现为 HTML(使用 RStudio 的 "Compile Notebook" 命令)。
虽然在纯文本中包含动态 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.
但是第二个例子,当呈现 HTML 时,仅将标题样式应用于第一行 ("Crosstabulation of"),然后换行并恢复为纯文本:
library(knitr)
var0 <- "Sex"
var1 <- "Education"
#' # Crosstabulation of
{{var0}}
#' # by
{{var1}}
#' # .
如何将标题样式应用到块的所有行?我必须使用任何替代语法吗?我错过了什么?
我认为这是 spin()
的限制。您可以 pre-process 带有 brew
的文件,或者让 R 在 markdown 中输出 header,
`%#%` <- 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)}}
我正在使用 knitr's spin() 函数将 R 脚本呈现为 HTML(使用 RStudio 的 "Compile Notebook" 命令)。
虽然在纯文本中包含动态 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.
但是第二个例子,当呈现 HTML 时,仅将标题样式应用于第一行 ("Crosstabulation of"),然后换行并恢复为纯文本:
library(knitr)
var0 <- "Sex"
var1 <- "Education"
#' # Crosstabulation of
{{var0}}
#' # by
{{var1}}
#' # .
如何将标题样式应用到块的所有行?我必须使用任何替代语法吗?我错过了什么?
我认为这是 spin()
的限制。您可以 pre-process 带有 brew
的文件,或者让 R 在 markdown 中输出 header,
`%#%` <- 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)}}