Format valueBox contents in $ Shinydashboard

How can i format contents of a value box (unit price and dollar sales) in shiny dashboard in $? The code is as follows without formatting:

Regards,

Chris

#dollarsales
  output$dollarsales <- renderValueBox({
      data() %>%
      filter(transDate >= input$dRange[1] & transDate <= input$dRange[2]) %>% 
      summarise(Dollarsales = sum(Amount)) %>%
      as.integer() %>%
      prettyNum(big.mark = ",") %>%
      valueBox(subtitle = "Dollar Sales")
  })

  
  #Quantitysales
  output$quantitysales <- renderValueBox({
      data() %>%
      filter(transDate >= input$dRange[1] & transDate <= input$dRange[2]) %>% 
      summarise(Quantitysales = sum(Quantity)) %>%
      as.integer() %>%
      prettyNum(big.mark = ",") %>%
      valueBox(subtitle = "Unit Sales")
  })

  
  #Averageunitprice
  output$averageprice <- renderValueBox({
      data() %>%
      filter(transDate >= input$dRange[1] & transDate <= input$dRange[2]) %>%
      summarise(Averageprice = mean(UnitPrice)) %>%
      prettyNum(big.mark = ",", digits =4) %>%
      valueBox(subtitle = "Average Unit Price")
  })

Created on 2019-07-18 by the reprex package (v0.3.0)

Hello!

Instead of using prettyNum() to format it, I suggest dollar() from the { scales } package.

> library(scales)
> x <- 2334.12
> x
[1] 2334.12
> scales::dollar(x)
[1] "$2,334.12"

One thing to note, however, is that the object returned is a character rather than a number.

> foo <- scales::dollar(x)
> str(foo)
 chr  "$2,334.12"

Many thanks for your input. I cannot apply this to a data frame, i get the following error message:

08%20PM

You have to pass the argument as a value not as a dataframe, see this reproducible example

library(shinydashboard)
library(dplyr)
# UI
ui <- dashboardPage(skin = "black",
                    dashboardHeader(title = "Test"),
                    dashboardSidebar(disable = TRUE),
                    dashboardBody(
                        fluidRow(
                            valueBoxOutput("test_box")
                        )
                    )
)

# Server response
server <- function(input, output, session) {
    output$test_box <- renderValueBox({
        iris %>% 
            summarise(Petal.Length = mean(Petal.Length)) %>% 
            .$Petal.Length %>% 
            scales::dollar() %>% 
            valueBox(subtitle = "Unit Sales",
                     icon = icon("server"),
                     color = "purple"
        )
    })
}

shinyApp(ui, server)

2 Likes

Noted with great thanks

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