Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Markdown YAML date settings using functions that require library()

I'd like to set date in YAML in R Markdown settings using following function:

paste(month(previous_month(part="start"),label=TRUE,locale=Sys.setlocale("LC_TIME", "English"),abbr=FALSE), year(previous_month(part="start")), sep=" ")

but this code contains functions from 2 packages: timeperiodsR and lubridate, so it's necessary to prior use library(timeperiodsR) and library(lubridate).

How should code after date: look like, because the following is not working?

title: " "
date: '`r library(timeperiodsR) library(lubridate) paste(month(previous_month(part="start"),label=TRUE,locale=Sys.setlocale("LC_TIME", "English"),abbr=FALSE), year(previous_month(part="start")), sep=" ")`'
output: 
   html_document:
    toc: true
    toc_depth: 2
    toc_float:
      collapsed: false
      smooth_scroll: false
fig_align: 'center'
like image 208
Agnieszka Avatar asked May 25 '26 01:05

Agnieszka


1 Answers

Use ";" to indicate newline:

---
title: "mytitle"
date: '`r library(timeperiodsR); library(lubridate); paste(month(previous_month(part="start"),label=TRUE,locale=Sys.setlocale("LC_TIME", "English"),abbr=FALSE), year(previous_month(part="start")), sep=" ")`'
output: 
   html_document:
    toc: true
    toc_depth: 2
    toc_float:
      collapsed: false
      smooth_scroll: false
fig_align: 'center'
---

Or use base (no dependencies, one-liner):

---
title: "mytitle"
date: '`r format(seq(Sys.Date(), length = 2, by = "-1 months")[ 2 ], format = "%B %Y")`'
output: 
   html_document:
    toc: true
    toc_depth: 2
    toc_float:
      collapsed: false
      smooth_scroll: false
fig_align: 'center'
---

Both give the below same output:

enter image description here

like image 50
zx8754 Avatar answered May 27 '26 18:05

zx8754



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!