用knitr设置HTML元素 [英] Setting HTML meta elements with knitr

查看:134
本文介绍了用knitr设置HTML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用knitr生成HTML报告,并且希望包含作者和生成日期元标记。

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

 < html> 
< head>
< meta name =authorcontent =<! - rinline Sys.getenv('USERNAME') - >>
< meta name =datecontent =<! - rinline as.character(Sys.time()) - >>
< / head>
< body>
< / body>
< / html>

不幸的是,在I knit(test.Rhtml),knitr生成的HTML是

 < meta name =authorcontent =< code class =knitr inline> RCotton< / code>> 
< meta name =datecontent =< code class =knitr inline> 2013-01-02 14:38:16< / code>>

这是无效的HTML。我真正想要生成的内容类似于

 < meta name =authorcontent =RCotton> 
< meta name =datecontent =2013-01-02 14:38:16>

我可以生成没有得到代码的R代码标签包装它?或者是否有另一种方式来指定标签属性(如这些内容属性)?

到目前为止,我最不想要的计划是用 readLines / str_replace / writeLines ,但这看起来相当糟糕。

解决方案

另一种方法是在内联代码中添加 I()不需要< code> 标记就可以打印字符,例如

 < HTML> 
< head>
< meta name =authorcontent =<! - rinline I(Sys.getenv('USERNAME')) - >>
< meta name =datecontent =<! - rinline I(as.character(Sys.time())) - >>
< / head>
< body>
< / body>
< / html>


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.

解决方案

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>

这篇关于用knitr设置HTML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆