Flexdashboard: pass reactive value to chart title

In the flexdashboard package, chart headers (headers for cells in the grid) are executed using three hash marks (for example, ### Chart title here). I want to pass the reactive value to this header. You can usually define the user interface and click on ( https://stackoverflow.com/a/1671895/... ), but hash tags are what tells knitting that this is the name of the diagram. I also thought about passing reactive values ​​using inline code (like `r CODE HERE`), as shown in MWE below. You can use inline text for chart titles, but not when it contains reactive values. This results in an error:

Error in as.vector: cannot coerce type 'closure' to vector of type 'character'

In this case, how can I pass the month in the form of chart.title?

MWE (deleting the last line allows this to be done)

---
title: "test"
output: flexdashboard::flex_dashboard
runtime: shiny
---

```{r}
library(flexdashboard)
library(shiny)
```

Inputs {.sidebar}
-------------------------------------

```{r}
selectInput(
    "month", 
    label = "Pick a Month",
    choices = month.abb, 
    selected = month.abb[2]
)

getmonth <- reactive({
    input$month
})

renderText({getmonth()})
```  

Column  
-------------------------------------

### `r sprintf('Box 1 (%s)', month.abb[1])`


### `r sprintf('Box 2 (%s)', renderText({getmonth()}))`
+4
source share
1 answer

The error that occurred was not part of the flexdashboardinability to display dynamic content, but sprintfcould not format the closure, that is renderText.

You only need to do part of the formatting of yours reactive, and everything is fine.

---
title: "test"
output: flexdashboard::flex_dashboard
runtime: shiny
---

```{r}
library(flexdashboard)
library(shiny)
```

Inputs {.sidebar}
-------------------------------------

```{r}
selectInput(
  "month", 
  label = "Pick a Month",
  choices = month.abb, 
  selected = month.abb[2]
)

getmonth <- reactive({
  sprintf('Box 2 (%s)', input$month)
})

renderText({getmonth()})
```  

Column  
-------------------------------------

### `r renderText(getmonth())`
+2
source

Source: https://habr.com/ru/post/1691892/


All Articles