Search code examples
rflexdashboardgauge

Adding dynamic titles in flexdashboard, or adding dynamic titles to gauge()


I am creating a page of gauges in flexdashboard. I would like to be able to update the title and value of the gauges from a separate csv, so that other members of my team can update the values without needing to touch the code.
How can I:

  • Make the titles dynamic, so they are taken from text from a csv? For example:

### myData$myTitles[1] (Except, of course, this doesn't work.)

  • Or add a title to the gauge (so I can leave the box title blank).

Some example code is below. I'd be very grateful for any advice.

    ---
    title: "My dynamic gauges"
    output: 
    flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    ---

    ```{r setup, include=FALSE}
    library(flexdashboard)
    library(readxl)
    library(dplyr)

    #myData <- read_csv("mydata.csv")

    myData <- tibble(myTitles = c("title 1", "title 2", "title 3","title  4","title 5","title 6"),
            myValues = c(2,4,3,3,5,2))

    ```

    Column 
    -----------------------------------------------------------------------
    <h3> Column Name 1 </h3>

    ### title 1 

    ```{r}

    gauge(myData$myValues[1],
          min = 0, 
          max = 6,
          gaugeSectors(success = c(0,2), 
                 warning = c(3,4), 
                 danger = c(5,6)))
    ```

    ### title 2

    ```{r}

    gauge(myData$myValues[2],
          min = 0, 
          max = 6,
          gaugeSectors(success = c(0,2), 
                 warning = c(3,4), 
                 danger = c(5,6)))
    ```

    Column 
    -----------------------------------------------------------------------
    <h3> Column Name 2 </h3>

    ### title 3 

    ```{r}

    gauge(myData$myValues[3],
          min = 0, 
          max = 6,
          gaugeSectors(success = c(0,2), 
                 warning = c(3,4), 
                 danger = c(5,6)))
    ```

    ### title 4

    ```{r}

    gauge(myData$myValues[4],
          min = 0, 
          max = 6,
          gaugeSectors(success = c(0,2), 
                 warning = c(3,4), 
                 danger = c(5,6)))
    ```


    Column 
    -----------------------------------------------------------------------
    <h3> Column Name 3 </h3>

    ### title 5

    ```{r}

    gauge(myData$myValues[5],
          min = 0, 
          max = 6,
          gaugeSectors(success = c(0,2), 
                 warning = c(3,4), 
                 danger = c(5,6)))
    ```

    ### title 6

    ```{r}

    gauge(myData$myValues[6],
          min = 0, 
          max = 6,
          gaugeSectors(success = c(0,2), 
                 warning = c(3,4), 
                 danger = c(5,6)))
    ```

Solution

  • Here is simple solution for that:

    ```{r results='asis'}
    cat(paste0("### ",myData$myTitles[1]))
    ```
    

    You should use that instead of ### myData$myTitles[1]