Setting HTML meta elements with knitr

*爱你&永不变心* 提交于 2019-12-10 03:19:32

问题


I'm generating HTML reports using knitr, and I'd like to include author and generation date meta tags.

My Rhtml page looks something like this.

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

Unfortunately, after I knit("test.Rhtml"), the HTML that knitr generates is

  <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>"> 

which isn't valid HTML. What I'd really like to generate is something like

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

Can I generate R code that doesn't get a code tag wrapping it? Or is there another way to specify tag attributes (like these content attributes)?

So far my least-worst plan is to manually fix the content with readLines/str_replace/writeLines, but this seems rather kludgy.


回答1:


Another (undocumented) approach is to add I() around your inline code to print the characters as is without the <code> tag, e.g.

<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>



回答2:


Not really nice, but seems to work without adding a hook:

<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>


来源:https://stackoverflow.com/questions/14124022/setting-html-meta-elements-with-knitr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!