Insert tags in header via .Rmd

2019-07-13 21:14发布

I'm using Rstudio to create Rmd reports, and I'd like to be able to insert meta tags into the <head> when the Rmd is knit into html.

From the documentation on knitr options I thought I could set the header option to insert text between the <head> tags like so:

```{r}
opts_knit$set(header = "<meta name=\"description\" content=\"this is a description\">")
```

However, nothing seems to be inserted. Am I doing something wrong or is this not possible?

标签: r knitr rstudio
1条回答
贪生不怕死
2楼-- · 2019-07-13 21:42

You use a line in the yaml header that reads in an external .html file with your header snippet in it as per this link.

Here is a slight modification from the link above including your code and includes the option the creation of the external .html header text in the .Rmd file which is not required:

---
title: "Test"
output:
  html_document:
    includes:
       in_header: header.html
---

```{r setup, include=FALSE, echo=FALSE}
# Create header.html
CON <- file("header.html")
writeLines('<meta name="description" content="this is a description" />', CON)
close(CON)
```
查看更多
登录 后发表回答