Flexdashboard:将反应值传递给 chart-title

Flexdashboard: Pass reactive value to chart-title

在包 flexdashboard 中,图表标题(header 用于网格中的单元格)是通过 3 个哈希标记(例如,### Chart title here)制作的。我想向此 header 传递一个反应值。通常可以定义一个 UI 并推送它 (),但散列标记告诉编织者这是一个 chart-title。我还考虑过使用 in-line 代码(例如 `r CODE HERE`)传递反应值,如下面的 MWE 所示。您可以在 chart-title 中使用内联文本,但当它包含反应值时则不能。这导致错误:

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

在这种情况下,我如何将月份作为 chart.title 传递?

MWE(删除最后一行允许 运行)

---
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()}))`

发生的错误不是 flexdashboard 无法呈现动态内容的一部分,而是 sprintf 无法格式化闭包的一部分,即 renderText.

您只需将格式设置作为 reactive 的一部分就可以了。

---
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())`