Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xaringan set the document title dynamically

In r-markdown 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?

Any help would be appreciated!

---
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
like image 872
Eric Fail Avatar asked Feb 22 '21 08:02

Eric Fail


Video Answer


2 Answers

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

like image 196
Waldi Avatar answered Oct 23 '22 18:10

Waldi


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

like image 3
Ricky Avatar answered Oct 23 '22 19:10

Ricky