7

In R-markdown there is is an option to move the title: out of the main YAML, as described in the R Markdown Cookbook.

But, in a Xaringan slide set the new --- seems to conflict with the idea of new slide.

The below code works, but when move line #2, title: "Presentation Ninja" outside the main YAML, and inset it as title: "The name of the dog is r dog_name!", by removing all my <!-- ... --> code in line #17-19, it does not work as expected. I do no longer get a title page. I guess I need to work around the --- in Xaringan?

---
title: "Presentation Ninja"
subtitle: "⚔<br/>with xaringan"
author: "Yihui Xie"
output:
  xaringan::moon_reader:
    lib_dir: libs
---
# xaringan set the document title dynamically
       
```{r, code=xfun::read_utf8('script_with_many_dog_names.R')}
```

The name of the dog is `r dog_name`!

<!-- --- -->
<!-- title: "The name of the dog is `r dog_name`!" -->
<!-- --- -->

Some bullets

- Foo

- Bar
halfer
  • 19,824
  • 17
  • 99
  • 186
Eric Fail
  • 8,191
  • 8
  • 72
  • 128

2 Answers2

4

You could use R Markdown parameters :

  1. Create a template file Template.Rmd
---
title: "The name of the dog is `r params$dog_name`"
subtitle: "⚔<br/>with xaringan"
author: "John Doe"
output:
  xaringan::moon_reader:
    lib_dir: libs
params:
    dog_name: NA
---



# xaringan set the document title dynamically

Some bullets

- Foo

- Bar

  1. Render the template after setting dog_name parameter :
source('script_with_many_dog_names.R')

# As an example, but it could be the result of previous script
params <- list(dog_name = 'Charles')

rmarkdown::render('Template.Rmd', output_file = 'presentation.html',
                  params = params,
                  envir = new.env(parent = globalenv())
)

enter image description here

Waldi
  • 39,242
  • 6
  • 30
  • 78
  • Interesting workaround, thanks. It does however complicate the case a bit, it could work. Thanks. – Eric Fail Feb 24 '21 at 16:31
  • Thanks for your feedback. I added a possible implementation of the sourcing of your own script. Parameters can be any list which makes the above structure highly customizable. Didn't either find a way to modify directly YAML. – Waldi Feb 24 '21 at 16:43
3

You can add your logic directly to the title, that seems to work fine for me:

---
title: "The number is `r round(100 * runif(1), 2)`"
subtitle: "⚔<br/>with xaringan"
author: "Yihui Xie"
output:
  xaringan::moon_reader:
    lib_dir: libs
---

example

Ricky
  • 1,005
  • 1
  • 12
  • 15
  • Thank you for responding to my question! I realize that should have mentioned that I need to use an external variable, that I am sourcing in. Sorry! You do have a vote :) – Eric Fail Feb 23 '21 at 19:37
  • 2
    Gotcha, sorry I wasn't originally following. Didn't actually realize you can do this, very cool! I can confirm this seems to work for other outputs I'm testing including Powerpoint, but does not work for xaringan template. I think we need the man himself Yihui for this one. I know he goes through all questions tagged with xaringan and other packages he made, but perhaps worth making an issue on GitHub! Happy to help as needed! – Ricky Feb 23 '21 at 20:12