使用knitr设置HTML元元素

时间:2013-01-02 14:45:59

标签: html r knitr literate-programming

我正在使用knitr生成HTML报告,我想要包含作者和生成日期元标记。

我的Rhtml页面看起来像这样。

<html>
<head>
  <meta name="author" content="<!--rinline Sys.getenv('USERNAME') -->">
  <meta name="date" content="<!--rinline as.character(Sys.time()) -->"> 
</head>
<body>
</body>
</html>

不幸的是,在我knit("test.Rhtml")之后,knitr生成的HTML是

  <meta name="author" content="<code class="knitr inline">RCotton</code>">
  <meta name="date" content="<code class="knitr inline">2013-01-02 14:38:16</code>"> 

这是无效的HTML。我真正想要产生的是

  <meta name="author" content="RCotton">
  <meta name="date" content="2013-01-02 14:38:16">

我可以生成没有包含code标记的R代码吗?或者是否有另一种指定标记属性的方法(如这些内容属性)?

到目前为止,我最糟糕的计划是使用readLines / str_replace / writeLines手动修复内容,但这似乎相当糟糕。

2 个答案:

答案 0 :(得分:12)

另一种(未记录的)方法是在内联代码周围添加I(),以便在没有<code>标记的情况下打印字符,例如。

<html>
<head>
  <meta name="author" content="<!--rinline I(Sys.getenv('USERNAME')) -->">
  <meta name="date" content="<!--rinline I(as.character(Sys.time())) -->"> 
</head>
<body>
</body>
</html>

答案 1 :(得分:4)

不是很好,但似乎没有添加钩子就可以工作:

<head>
<!--begin.rcode results='asis', echo=FALSE
cat('
  <meta name="author" content="', Sys.getenv('USERNAME'), '"> 
  <meta name="date" content="', as.character(Sys.time()),'-->"> 
',sep="")
end.rcode-->

</head>