gpt4 book ai didi

r - Flexdashboard:将 react 值传递给图表标题

转载 作者:行者123 更新时间:2023-12-02 02:04:34 25 4
gpt4 key购买 nike

在包 flexdashboard 中,图表标题(网格中单元格的标题)是通过 3 个井号标记制作的(例如,### 这里是图表标题)。我想将 react 值传递给此 header 。通常情况下,人们可以定义一个 UI 并推送它( https://stackoverflow.com/a/48118297/1000343 ),但散列标记告诉编织这是一个图表标题。我还考虑过使用内联代码(例如,`r CODE HERE`)传递无功值,如下面的 MWE 所示。您可以将内联文本用于图表标题,但当它包含 react 值时则不能。这会导致错误:

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

在这种情况下,我如何将月份作为图表.标题传递?

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.

您只需将格式部分设置为响应式,就可以了。

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

关于r - Flexdashboard:将 react 值传递给图表标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48157026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com