Insert tags in header via .Rmd

不羁岁月 提交于 2019-12-24 05:40:22

问题


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?


回答1:


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)
```


来源:https://stackoverflow.com/questions/19748205/insert-tags-in-header-via-rmd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!