2014-10-16 69 views
0

在我的閃亮閃亮的包renderTable,我有以下代碼:用逗號

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

我怎樣才能讓最後的4列在他們的逗號。因此,例如10000變成10,000

回答

3

由於您沒有提供可複製的示例,因此在此處演示瞭如何操作。據我所知,沒有辦法用renderTable聲明指定數字格式,所以你必須重新格式化你的值。關鍵是使用formatprettyNum並設置big.mark選項。但謹慎的說法是,將數字值轉換爲字符。建議您一旦不再對值進行處理,就應該進行格式轉換。

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

這真棒,正是我所需要的:-)。 – user1357015 2014-10-16 18:34:22