【问题标题】:Flexdashboard: Pass reactive value to chart-titleFlexdashboard:将反应值传递给图表标题
【发布时间】:2018-06-17 19:54:00
【问题描述】:

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

【问题讨论】:

  • 我认为如果不破坏原始 HTML 中的图表标题,这将很难。我有 something 可以工作,但它真的很丑。

标签: r shiny flexdashboard


【解决方案1】:

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

【讨论】:

猜你喜欢
  • 2019-12-30
  • 2021-09-27
  • 2018-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-03
  • 2015-05-12
  • 1970-01-01
相关资源
最近更新 更多