简体   繁体   English

如何在knitr :: spin中输出文字反引号

[英]How to output literal backticks in knitr::spin

I'm trying to create some snippet templates for knitr::spin documents in R Studio, and I need them to include literal backticks so that the resulting document contains an R snippet: 我正在尝试为R Studio中的knitr::spin文档创建一些片段模板,我需要它们包含文字反引号,以便生成的文档包含R片段:

Example of desired output: 期望输出的示例:

#' ---
#' author: 'ENTER'
#' title: 'ENTER'
#' date: '`r Sys.time()`'
#' output: 
#'    html_document
#' ---

However, I can't figure out how to output backticks. 但是,我无法弄清楚如何输出反引号。 This: 这个:

`r paste("#' date: '`Sys.time()`')`

will not work as the tick marks interrupt the paste command when rendering from R Studio snippet to R code. 当从R Studio片段渲染到R代码时,刻度线中断粘贴命令将无效。 I've been trying to hash out the tick marks, adding forwards and backwards slashes etc., but haven't found a solution that renders this line correctly to: 我一直在试图勾选刻度线,添加前进和后退斜线等,但还没有找到一个解决方案,使该行正确呈现:

#' date: '`r Sys.time()`'

Windows 7 Enterprise, 64-bit Operating System Windows 7企业版,64位操作系统
R Version: 3.2.5 R版本:3.2.5
R Studio Version: 0.99.903 R Studio版本:0.99.903
knitr Version: 1.14 knitr版本:1.14


Example: I tried this, but it still translates the R code instead of just printing the text: 示例:我试过这个,但它仍然翻译R代码而不是只打印文本:

snippet spin.header
`r paste("#' ---")`
`r paste("#' author: 'ENTER'")`
`r paste("#' title: 'ENTER'")`
`r paste("#' date:  '<code>``` `r Sys.time()` ```</code>'")`
`r paste("#' output:")` 
`r paste("#'    html_document")`
`r paste("#' ---")`

The correct answer was posted by rawr in the comments (he only missed r and a tick mark): rawr在评论中发布了正确答案(他只错过了r和刻度线):

snippet spin.header
`r paste("#' ---")`
`r paste("#' author: 'ENTER'")`
`r paste("#' title: 'ENTER'")`
`r paste("#\' date: '\x60r Sys.time()\x60'")`
`r paste("#' output:")` 
`r paste("#'    html_document")`
`r paste("#' ---")`

One option is to break the token used during snippet processing for inline R. 一种选择是打破内联R的片段处理期间使用的令牌。

snippet sh
  #' ---
  #' author: '${1:AUTHOR}'
  #' title: '${2:TITLE}'
  #' date: '`${3:}r Sys.time()`'
  #' output:
  #'    html_document
  #' ---
  ${0}

Another option is to avoid the inline R parsing completely. 另一种选择是完全避免内联R解析。

snippet sh
  `r paste0(readLines("~/.R/snippets/spinheader.txt"),collapse = '\n')`

spinheader.txt

#' ---
#' author: '${1:AUTHOR}'
#' title: '${2:TITLE}'
#' date: '`r Sys.time()`'
#' output:
#'    html_document
#' ---
${0}

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

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