首页 > 解决方案 > 使用 _site.yml 文件时渲染 Xaringan Rmd

问题描述

我有一个包含三个文件的网站。

_site.yml

name: Website
navbar:
  title: Website
  right:
    - text: Home
    - text: Info
output:
  html_document:
    theme: flatly
    highlight: tango

索引.Rmd

---
title: Welcome
output:
  html_document:
    theme: united
    highlight: textmate
---

This is the index.

测试.Rmd

---
title: Test
output:
  html_document:
    theme: united
    highlight: textmate
---

This is the test file.

```{r}
2+2
```

如果我运行rmarkdown::render("test.Rmd"),我会得到一个包含网站标题的 HTML。这类似于输出运行rmarkdown::render_site()

在此处输入图像描述

如果我删除_site.yml文件并运行相同的命令,我会得到常规的 HTML 输出:

在此处输入图像描述

所以,render()必须在使用_site.yml时使用。这可以禁用吗?即使_site.yml文件存在,我也想创建一个常规的 HTML 输出。当我有 xaringan 演示文稿并且我不希望它们与网站标题一起呈现时,这尤其是一个问题。

标签: rr-markdown

解决方案


我认为没有办法在 中禁用它rmarkdown,但您可以尝试以下解决方法暂时重命名它,然后再将其重新命名:

render_without_site_yml <- function(input, ...) {
  dir <- dirname(input)
  site_yml <- file.path(dir, "_site.yml")
  if (file.exists(site_yml)) {
    newname <- file.path(dir, "_site.yml.save")
    if (file.exists(newname))
      stop("'_site.yml.save' exists!")
    file.rename(site_yml, newname)
    on.exit(file.rename(newname, site_yml))
  }
  rmarkdown::render(input, ...)
}

推荐阅读