I'm trying to build a dashboard with Quarto. One initial goal is to count the number of cases in a dataset (later with API retrieval) when the dashboard is called on the Shiny server. In the 'server' context, I render the n-count with output$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))
```
As described here you can use the value_box
from the bslib
package to create a dynamic value box. Make sure you create a reactive
value like this:
---
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()})
```
Output: