Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 402
Richie Cotton Avatar asked Jan 02 '13 14:01

Richie Cotton


2 Answers

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>
like image 153
Yihui Xie Avatar answered Sep 22 '22 07:09

Yihui Xie


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>
like image 40
Dieter Menne Avatar answered Sep 22 '22 07:09

Dieter Menne