mindlessgreen
mindlessgreen

Reputation: 12112

Render Xaringan Rmd when using _site.yml file

I have a website set up with three files.

_site.yml

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

index.Rmd

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

This is the index.

test.Rmd

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

This is the test file.

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

If I run rmarkdown::render("test.Rmd"), I get an HTML that includes the website header. This is similar to output running rmarkdown::render_site().

enter image description here

If I remove _site.yml file and run the same command, I get the regular HTML output:

enter image description here

So, render() must be using the _site.yml when present. Can this be disabled? I would like to create a regular HTML output even when the _site.yml file is present. This is especially an issue when I have xaringan presentations and I do not want them rendered with the website header.

Upvotes: 2

Views: 272

Answers (2)

mindlessgreen
mindlessgreen

Reputation: 12112

It seems like output: specified in individual .Rmd files overrides the default in _site.yml.

So my _site.yml contains

output:
  html_document:
    highlight: textmate
    toc: true
    css: "assets/doc.css"
    include:
      after_body: assets/footer-doc.html

And my xaringan presentation contains

output:
  xaringan::moon_reader:
    css: 'assets/slide.css'
    lib_dir: libs
    include: NULL

And if I call render_site(), md and Rmd files without any output: specified takes the defaults from _site.yml, gets doc.css styles and add custom footer footer-doc.

My xaringan presentations with custom output: is rendered through moon_reader and gets slide.css style and no footer is attached.

The output files are rendered and moved to destination folder. But I get this kind of warnings for xaringan slides:

In file.rename(output, output_dest) :
  cannot rename file './source/slide_dge.html' to './dest/slide_dge.html', reason 'No such file or directory'

I do not have an explanation for this yet.

In addition, if I render the xaringan Rmd alone (in presence of _site.yml) using rmarkdown::render('xaringan.Rmd'), this results in an incorrect output.

Upvotes: 1

user2554330
user2554330

Reputation: 44867

I don't think there's a way to disable it in rmarkdown, but you could try the following workaround to temporarily rename it, then name it back afterwards:

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, ...)
}

Upvotes: 2

Related Questions