0

I'm trying to have thousand separator in my shiny table output. The following are my code:

UI <- dashboardPage(     
  dashboardBody(        
    fluidPage(          
      selectInput(inputId = "TableName",
                  label = "Select Data of Interest",
                  choices = list.files(path = "CSVData", pattern = "AMD_")),          
      uiOutput("Channel"),    
      formattableOutput("ResultTable")         
    )
  )
)

Server <- function(input, output){      
  output$Channel <- renderUI({        
    df <- read.csv(paste("Pathname/",input$TableName, sep = ""))        
    selectInput(inputId = "ChannelSelect",
                label = "Select Channel:",
                choices = unique(df) %>% select(Channel))),
                selected = NULL, multiple = FALSE)        
  })

  output$ResultTable <- renderFormattable({

    df <- read.csv(paste("CSVData/",input$TableName, sep = ""))
    ChanSelect <- input$ChannelSelect    
    A1 <- df %>% filter(if(ChanSelect != "All") (Channel == ChanSelect) else TRUE) %>%
      select(Channel, Amount)        
    formattable(A1, list(
      `Amount` = comma(`Amount`, digits = 0, format = "f", big.mark = ","
    ))        
  })      
}    

My df's are varied but all of them are in the format:

data.frame("Channel" = c("A", "B", "C", "D"),
           "Amount" = c(1000000, 2000000, 3000000, 4000000 ))

The problem is with the formattable part that it didn't work. I don't know how to input the function comma() properly with formattable.

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
Rosette
  • 35
  • 5
  • 1
    Please provide your data, current and expected output to get more options. This is currently an [X-Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem/250659#250659). – NelsonGon Jun 12 '19 at 07:01
  • My data has columns of big numbers, let's say column D. I wish all the numbers in this column to have thousand separator – Rosette Jun 12 '19 at 07:02
  • You can edit your post to add details. Please follow these [guidelines](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It might make it easier to get help. – NelsonGon Jun 12 '19 at 07:04
  • 1
    I read it. It might take me a while to edit my question. I thought putting my actual codes and data here is a bad thing to do. My bad. – Rosette Jun 12 '19 at 07:08
  • Do you need `formattable` here or could you just use `format`? – Sven Jun 12 '19 at 07:37
  • I preferred using formattable as I also need to use their other features. I can find examples of doing stuff like setting colors and line. But setting number format is nowhere to be found. – Rosette Jun 13 '19 at 02:56

2 Answers2

1

There are two ways to do this:

1. Create a Custom Function

mycomma <- function(digits = 0) {
      formatter("span", x ~ comma(x, digits = digits)
      )
    }

Use for a single named column:

formattable(df, list(`Account` = mycomma(digits=0))
  )

Use for several columns:

formattable(df, 
            list(area(col = c(3:6)) ~ mycomma(digits=0))
  )

2. pass the formattable function each time

formattable(df, list(`Account` = formatter("span", x ~ comma(x, digits = 0))
      )

NOTE: You could just as easily create a custom function that could be passed a different function, like percent or accounting. YMMV

mysimpleformat <- function(fun = "comma", digits = 0) {
  fun <- match.fun(fun)
  formatter("span", x ~ fun(x, digits = digits)
  )
}
jessi
  • 1,438
  • 1
  • 23
  • 36
0

If you're just trying to add the thousand markers to your Amount column, you can do it as follows:

df <- data.frame("Channel" = c("A", "B", "C", "D"),
                 "Amount" = c(1000000, 2000000, 3000000, 4000000 ))
df$Amount <- format(as.integer(df$Amount), big.mark=",")
Sven
  • 1,203
  • 1
  • 5
  • 14