Use r object in title of flexdashboard

I'm building a static HTML flexdashboard document and I want to have the title be dynamically updated depending on some R code I run in the RMD. For regular R Markdown HTML documents, one way to do this is to include a second YAML block with the title after the R code chunk where the R object is defined as suggested here: https://stackoverflow.com/questions/31861569/setting-document-title-in-rmarkdown-from-parameters.

I'm trying this with the flexdashboard template and not having any luck. Any ideas?

Here is a minimal example that doesn't work with flexdashboard:

---
output: 
  flexdashboard::flex_dashboard
---

```{r setup, include=FALSE}
library(flexdashboard)
my_title <- "I am the title"
```
---
title: '`r my_title`'
---

Test R Markdown flexdashboard

And here is a working example with the html_document format

---
output: html_document
---

```{r setup, include=FALSE}
my_title <- "I am the title"
```
---
title: '`r my_title`'
---

Test R Markdown HTML

Hi,

you may have found an undesired behavior or a bug. Let's explain what is going on.

In HTML document, you show that you can separate the yaml metadata definition in several blocks: it is a nice pandoc feature. If two metadata are the same, the first one is used and metadata from the document are erase by other provided as arguments.
However, when Rmarkdown does not see a title in the yaml header, for a avoiding pandoc2 warning, it will add a metadata pagetitle as argument in pandoc call, by using the --metadata flag, that is prioritary on document metadata. This is set to the document intermediary input name : *.utf8.md.
As no title was provided in your yaml header in html example, a pagetitle has been set and if you search for utf8.md in html source code, you should find it.
(this is a first undesired behaviour for me)

So why do you have the correct title in html anyway ?
This is because html document template is using two variables : pagetitle and title. In your html example, both are set, and the title is used to set the h1 title as you can find in the template.

Let's note that when a title is provided in the yaml header, no pagetitle is added in pandoc call, and if no pagetile is set explicitely, it is set by pandoc to the title provided.

We would expect for flexdashboard to be the same. And it fact, it is. However, the flexdashaboad pandoc template does not use a title variable., only pagetitle.
This is why when you set title in the Rmd, it does not work because pagetitle provided in --metadata flag superseed.
I think flexdashboard should use title for the nav bar: this is a second undesired behavior

For now, I don't have a solution for flexdashboard unless you modified the title at loading with javascript the navbar title.
Something like this

---
output: 
  flexdashboard::flex_dashboard
---

```{r setup, include=FALSE}
library(flexdashboard)
my_title <- "I am the title"
```

<script>
document.querySelector(".navbar-header > span.navbar-brand").innerHTML = "`r my_title`";
</script> 

Test R Markdown flexdashboard

Thanks for the strange behavior report. Some issues should be raised in several repos.

1 Like

Thanks for the thorough diagnosis, @cderv! I'm happy to open some GH issues, but always get a little confused as to where exactly in the RMarkdown > knitr > Markdown > pandoc > html chain these issues are cropping up, so please correct me if I'm wrong:

One issue is that the flexdashboard default.html template does not use title in the same way the rmarkdown html template does and so doesn't find the title I define in the second yaml block. This would require a change to the flexdashboard html template.

The other issue is what Rmarkdown::html_document_base() does if there is no title in the first yaml header.

Do I have this right?

And thank you also for the temporary JS workaround -- that works well enough for me for now.

You get it right. Nice synthesis.
I plan to deal with those issues when I have time. You can open issues or I will do it.
Opening issues requires some reprex or details examples.
If you want to open them, please do it. I'll complete with what I have in mind.

hi @mfherman

I open issues and also suggested a first fix that makes it possible to customize pagetitle in flexdashoard

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.