Reputation: 151
I am trying to format a column in a table in a Shiny app but it is rendering as decimal. Can anybody help me get the percentage formatting? Here's my code:
Shiny Server code
output$freqTopCount = DT::renderDataTable({
freqTopCount <- faultCodeFrequency(Data, input$brwyline)
freqTopCount[, input$brwyline] <- percent(freqTopCount[, input$brwyline])
freqTopCount
}, options = list(paging = FALSE, searching = FALSE)
, rownames = TRUE, caption = "Frequency")
Here's function being called
faultCodeFrequency <- function(FuncData, line) {
FuncData <- as.data.frame(FuncData[which(FuncData[, 15] == line),])
# Calculate frequency of fault codes by brewery line.
freq <- as.data.frame(ungroup(mutate(summarise(group_by(FuncData, BRWYLine,Fault_Code), n = n()), freq = n / sum(n))))
# Format frequency to 4 decimal points.
freq$freq <- round(freq$freq,4)
# Put the top 5 fault code frequencies by brewery line in a data table.
freqTop <- data.table(freq)[order(freq, decreasing = TRUE), head(.SD, 5), by="BRWYLine"]
# Remove column n from data table.
freqTop$n <- NULL
# Swing freqTop table into a crosstab. Brewery Line columns, Fault Code rows. The sum function sums each combination of BrwyLine/FaultCode in the data table.
freqTop <- tapply(freqTop$freq,list(freqTop$Fault_Code, freqTop$BRWYLine), FUN=sum)
# Calculate sum of frequencies.
freqTopSum <- as.matrix(t(colSums(freqTop,na.rm=TRUE)))
# Name sum row.
rownames(freqTopSum) <- ('Total')
# Add sum row to frequency matrix.
freqTopCount <- rbind(freqTop, freqTopSum)
result <- freqTopCount
result
}
Upvotes: 1
Views: 1182
Reputation: 151
I used the percent() function in the scales package to solve this.
freqTopCount[, input$brwyline] <- scales::percent(freqTopCount[, input$brwyline])
Upvotes: 1