user1357015
user1357015

Reputation: 11686

Shiny renderTable with commas

In my shiny package, I have the following code:

output$growthTable <- renderTable({
  table <- makeTable()
  colnames(table) <- c("Year","BOY Balance", "Yearly Growth", "EOY Contribution", "EOY Balance")
  return(table)
}, include.rownames = FALSE)

How can I make the last 4 columns have commas in them. So for instance 10000 becomes 10,000?

Upvotes: 1

Views: 3539

Answers (1)

cdeterman
cdeterman

Reputation: 19960

Since you didn't provide a reproducible example here is a working demonstration of how to do it. To my knowledge there is no means to specify number format with the renderTable statement so you must reformat your values. The key is to use format or prettyNum with the big.mark option set. A word of caution, however, this is converting your numeric values into characters. It is recommended that you should do this formatting conversion once there will be no further processing of the values.

require(shiny)
data(iris)

runApp(
  list(
    ui = fluidPage(
      headerPanel('Print big numbers with comma'),
      mainPanel(
        tableOutput("table"))
    ),

    server = function(input, output) {

      makeTable <- reactive({
        iris[,1:4] <- iris[1:4] * 1000
        #iris[,1:4] <- sapply(iris[,1:4], FUN=function(x) format(x, big.mark=","))
        iris[,1:4] <- sapply(iris[,1:4], FUN=function(x) prettyNum(x, big.mark=","))
        iris
      })

      output$table <- renderTable({
        table <- makeTable()
        table
      }, include.rownames = FALSE)
    }
  )
)

Upvotes: 6

Related Questions