Flexdashboard:将无效值传递给图表标题

时间:2018-01-08 19:39:29

标签: r shiny flexdashboard

在flexdashboard包中,图表标题(网格中单元格的标题)是通过3个哈希标记(例如### Chart title here)生成的。我想将反应值传递给此标头。通常可以定义UI并推送(https://stackoverflow.com/a/48118297/1000343),但是哈希标记告诉编织这是图表标题。我还考虑过使用内联代码(例如`r CODE HERE`)传递无功值,如下面的MWE所示。您可以对图表标题使用内联文本,但在包含反应值时不能使用。这会导致错误:

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

1 个答案:

答案 0 :(得分:2)

发生的错误不是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())`