9

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.

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360

2 Answers2

13

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>
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • I believe the newly added `knit_expand` function would be ideal here. By passing a file through `knit_expand` first and then `knit2html`, you can easily achieve the best of `brew` and `knitr`. Maybe @Yihui can add to this. – Ramnath Jan 03 '13 at 18:19
  • @DieterMenne sure, I'll add to the website; @Ramnath in this case, `` is not a bad choice if there are further chunks in the file, and `knit_expand()` will be an additional step before `knit()` – Yihui Xie Jan 03 '13 at 21:17
  • @yihui Makes sense. But knit_expand provides `brew` like functionality which has been long sought for by Sweave users. One way to reduce the tool chain is to allow users to specify `opts_knit$set(expand = TRUE)` which will run the document through `knit_expand` before knitting it. Alternately, it can be achieved using the document hook, which I think makes more sense since knit_expand can then use data from the parent frame. – Ramnath Jan 04 '13 at 18:07
4

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>
Dieter Menne
  • 10,076
  • 44
  • 67