Tyl*_*ker 4 r shiny flexdashboard
在 flexdashboard 包中,图表标题(网格中单元格的标题)通过 3 个哈希标记(例如,### Chart title here)。我想将一个反应值传递给这个标头。通常人们可以定义一个 UI 并推送它(/sf/answers/3368280821/),但井号告诉编织这是一个图表标题。我还考虑过使用`r CODE HERE`内嵌代码(例如,)传递反应值,如下面的 MWE 所示。您可以将内联文本用于图表标题,但当它包含反应值时则不能。这导致错误:
Error in as.vector: cannot coerce type 'closure' to vector of type 'character'
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我如何将月份作为 chart.title 传入?
---
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()}))`
Run Code Online (Sandbox Code Playgroud)
发生的错误不是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())`
Run Code Online (Sandbox Code Playgroud)