ryamlr-markdownknitr

Why doesn't this inline expression `r getwd() |> strsplit('/')` work when inside the yaml header of a rmarkdown document?


I am trying to include as a subtitle in the yaml of a rmarkdown document an inline r code expression to get a part of the directory name.

So I tried this:

---
title: "Untitled"
subtitle: "`r getwd() |> str_split('/')|>pluck(1) |> pluck(7)`"
author: "Gandalf"
date: "`r Sys.Date()`"
output: html_document
---

The piece of code getwd() |> str_split('/')|>pluck(1) |> pluck(7) works fine in a r chunk, but when in the yaml, it does not work when knitting, and this happens after the first pipe (if I only use subtitle: "`r getwd()`" it works), e.g.this does not work:

---
title: "Untitled"
subtitle: "`r getwd() |> str_split('/')`"
author: "Gandalf"
date: "`r Sys.Date()`"
output: html_document
---

and I get a 'halted execution' in line 2 error message.

I tried the following which all failed:

So I thought it was because of the pipe but the following actually works:

subtitle: "`r getwd() |> paste('hello')`"

The directory name looks like this: "D:/Oswaldo/Autoentrepreneur/Formateur_RStudio/Formation_Tidyverse/Sessions_clients/2024_06_24.Presentiel.Module_deb.CD_UM_fr/evaluations/satisfaction_beneficiaires"

So, what am I missing?


Solution

  • The following should work that avoids using the pipe operator and base R strsplit():

    ---
    title: "Untitled"
    subtitle: "`r strsplit(getwd(), '/')[[1]][7]`"
    author: "Gandalf"
    date: "`r Sys.Date()`"
    output: html_document
    ---