rdater-markdowndashboardflexdashboard

Can I print the date object into the flexdashboard title?


I do not know whether there is a way to print a date object that appears into one of the '''r ''' chuncks in flexdashboard.

this is the flexdashboard title:

---
title: 'r param$title
output: 
  flexdashboard::flex_dashboard:
    theme: simplex
    vertical_layout: scroll
    orientation: rows
param:
  title: paste0( "Metrics from_date_to_date)" 
  from_date_to_date: print(from_date_to_date)

---

And this is my date object:

> from_date_to_date <- data_metrics$month_data_generated %>%
+   unique()
> from_date_to_date
[1] "2023-04-15 to 2023-05-16"

Obviously, as wrong as it is, I've used the r coding language at the heading of the rmarkdown to show what I want. Is there a way to print my date object from a chunk into the title?


Solution

  • Define the date object at first and then use the title yaml key later.

    ---
    output: 
      flexdashboard::flex_dashboard:
        orientation: columns
        vertical_layout: fill
    ---
    
    ```{r setup, include=FALSE}
    library(flexdashboard)
    date_obj <- "2023-04-15 to 2023-05-16"
    ```
    
    ---
    title: 'Metrics From `r date_obj`'
    ---
    
    Column {data-width=650}
    -----------------------------------------------------------------------
    
    ### Chart A
    
    ```{r}
    
    ```
    
    

    You can also use parameters too,

    ---
    output: 
      flexdashboard::flex_dashboard:
        orientation: columns
        vertical_layout: fill
    params:
      date_obj: ""
    ---
    
    ```{r setup, include=FALSE}
    library(flexdashboard)
    ```
    
    ---
    title: 'Metrics From `r params$date_obj`'
    ---
    
    Column {data-width=650}
    -----------------------------------------------------------------------
    
    ### Chart A
    
    ```{r}
    
    ```
    
    

    Then render the document,

    # get your date object
    date_obj <- "2023-04-15 to 2023-05-16"
    
    # then render
    rmarkdown::render("flex_dashboard.Rmd", params = list(date_obj = date_obj))
    

    r code in title