2017-05-04 112 views
1

我想在Shiny應用程序的表格中格式化一列,但它呈現爲十進制。任何人都可以幫助我獲得百分比格式?這裏是我的代碼:Shiny格式化表格

閃亮Server代碼

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") 

這裏的函數被調用

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 
} 
+0

你已經看過嗎? http://stackoverflow.com/questions/34728353/datatable-in-r-formatting-rows-with-specific-value-category-to-a-percentage – BigDataScientist

+0

你可以使用'formatPercentage',看看[這裏](https: //rstudio.github.io/DT/functions.html) – NicE

+0

我使用了scales :: percent()來解決我的格式問題。 – thatsawinner

回答

1

我用百分比()函數在尺度包來解決這個問題。

freqTopCount[, input$brwyline] <- scales::percent(freqTopCount[, input$brwyline])