我正在尝试使用 Quarto 构建一个仪表板。最初的目标是在 Shiny 服务器上调用仪表板时计算数据集中的案例数量(稍后使用 API 检索)。在“服务器”上下文中,我使用 output$n 渲染 n 计数 <- renderText(nrow(data), and then attempt to retrieve it in a valuebox with textOutput("n"). However, I only see code in the boxes, not the count. What am I doing wrong? (I've tried several variations):
---
title: "Count N"
format: dashboard
server: shiny
---
```{r}
#| context: setup
data <- tibble::tibble(a = c(1, 2, 3)) # The data should always be retrieved from a server when the dashboard starts later, that's why I need the server context
```
## Row
```{r}
#| content: valuebox
#| title: "n1"
# renderText() with paste0
list(
value = textOutput("n1")
)
```
```{r}
#| content: valuebox
#| title: "n2"
# renderText() without paste0
list(
value = textOutput("n2")
)
```
```{r}
#| content: valuebox
#| title: "n3"
# it works with a blank (but boring) number
list(
value = 99
)
```
```{r}
#| context: server
n <- data |> nrow() |> as.character()
output$n1 <- renderText(n)
output$n2 <- renderText(paste0(n))
```
如here所述,您可以使用
value_box
包中的bslib
来创建动态值框。确保创建一个像这样的 reactive
值:
---
title: "Count N"
format: dashboard
server: shiny
---
```{r}
#| context: setup
library(shiny)
data <- tibble::tibble(a = c(1, 2, 3)) # The data should always be retrieved from a server when the dashboard starts later, that's why I need the server context
```
## Row
```{r}
library(bslib)
library(bsicons)
value_box(
id = "card1",
title = "n1",
value = textOutput("n1")
)
```
```{r}
#| content: valuebox
#| title: "n3"
# it works with a blank (but boring) number
list(
value = 99
)
```
```{r}
#| context: server
n <- reactive({
data |> nrow() |> as.character()
})
output$n1 <- renderText({n()})
```
输出: