Search code examples
htmlrknitrliterate-programming

Setting HTML meta elements with knitr


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.


Solution

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