Shiny namespaces for UI Elements without Output IDs - bslib::valueBox

I'm working on a golem application and came across bslib. The value box elements are highly desirable for an existing dashboard, but I don't see a straightforward way to feed reactive elements defined in server-side namespaces to these in the ui side of the module.

Very barebones example below, knowing that shiny::valueBox / shiny::renderValueBox is not compatible with the bslib one.

mod_display_ui <- function(id){
  ns <- NS(id)

  tagList(
      bslib::value_box(title = "Title",
                  value = ns("value")
      )
}

mod_display_server <- function(id){
  moduleServer( id, function(input, output, session){
    ns <- session$ns

   output$value <- renderValueBox({
          valueBox(
                   value = value
          )
        })
}

Is bslib at all compatible with modules/golem architecture?

library(shiny)
library(bslib)

mod_display_ui <- function(id){
  ns <- NS(id)
  
  uiOutput(ns("main_out"))
}
mod_display_server <- function(id, title, text) {
  moduleServer(id, function(input, output, session) {
    stopifnot(is.reactive(title))
    stopifnot(is.reactive(text))

    output$main_out <- renderUI({
      bslib::value_box(
        title = req(title()),
        value = req(text())
      )
    })
  })
}

ui <- fluidPage(
  textInput("mytext_title","Put text here","Starting Title"),
  textInput("mytext_body","Put text here","Initial Body"),
  mod_display_ui("mybox")
)

server <- function(input, output, session) {
  
  mod_display_server("mybox",title=reactive(input$mytext_title),
                     text=reactive(input$mytext_body))
}

shinyApp(ui, server)

Hi @nirgrahamuk, this is exactly what I was looking for! Thank you so much for your help, I didn't realize uiOutput/renderUI could service objects like this.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.