Pass colour object into a valuebox in shiny module

Is there a way of passing a colour object in to a valuebox() using shiny modules?

This works without the colour argument, but does not work with it.

color = uiOutput(ns("colour")), # this does not work !!!!!

library(shiny)
library(shinydashboard)

# values  -----------------------------------------------------------------
label1 <- "rows"
icon1 <- icon("calendar")   
colour1 <- "blue"

# infobox module ----------------------------------------------------------
info_stat_ui <- function(id){
  ns <- NS(id) # define namespace for below
  valueBox(uiOutput(ns("num")),
           uiOutput(ns("label")),
           icon = uiOutput(ns("icon")),
           color = uiOutput(ns("colour")), # this does not work !!!!!
           # color = "blue", # but using this does work
           width = 4)
}

info_stat_server <- function(id, dataset, label, icon, colour){
  moduleServer(id, function(input, output, session){
    
    # the label of the valuebox
    output$label <- renderUI(paste0(label))
    
    # the icon of the valuebox
    output$icon <- renderUI(icon)
    
    # the colour of the valuebox
    output$colour <- renderUI(colour)
    
    # the value
    output$num <- renderUI({
      paste(
          nrow(dataset)
      )
    })
  })
}


# the app -----------------------------------------------------------------
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      info_stat_ui("test")
    )
  )
)

server <- function(input, output) {
  
  info_stat_server("test",
                   dataset = iris,
                   label = label1,
                   icon = icon1,
                   colour = colour1
                   )
}

shinyApp(ui, server)

It produces this error:

Error during wrapup: Invalid color:

. Valid colors are: red, yellow, aqua, blue, light-blue, green, navy, teal, olive, lime, orange, fuchsia, purple, maroon, black.
Error: no more error handlers available (recursive errors?); invoking 'abort' restart

Here is one option of a way to do this sort of thing.
edit.. sidenote : this is slightly overengineered for the example as various things are made reactive that the demo structure wont vary and could be passsed through yet more directly.

library(shiny)
library(shinydashboard)

# values  -----------------------------------------------------------------
label1 <- "rows"
icon1 <- icon("calendar")   
colour1 <- "blue"

# infobox module ----------------------------------------------------------
info_stat_ui <- function(id){
  ns <- NS(id) # define namespace for below
     uiOutput(ns("vb"))
}

info_stat_server <- function(id, dataset, label, icon, colour){
  moduleServer(id, function(input, output, session){
    
    output$vb <- renderUI({
      valueBox(value = req(rnum()),
      subtitle=req(rlabel()),
               icon =req(ricon()),
               color = req(rcolour()),
               width = 4)
    })
    
    # the label of the valuebox
    rlabel <- reactive({paste0(label)})
    
    # the icon of the valuebox
   ricon <- reactive(icon)
    
    # the colour of the valuebox
    rcolour <- reactive(colour)
    
    # the value
    rnum <-reactive({paste(nrow(dataset))})
 })
}


# the app -----------------------------------------------------------------
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      info_stat_ui("test")
    )
  )
)

server <- function(input, output) {
  
  info_stat_server("test",
                   dataset = iris,
                   label = label1,
                   icon = icon1,
                   colour = colour1
  )
}

shinyApp(ui, server)
1 Like

Great. Thanks for the help.

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.