Bea
Bea

Reputation: 1110

How to custom format numeric columns in shiny renderTable?

I would like to custom format the numeric columns in my table. I would like to have the p-values in scientific format, and the other numeric columns with 0 or 2 decimals. How can I do this? I have tried

output$addtable <- renderTable({ ...} , digits = -2)

but that converts all numeric columns into scientific format.

output$addtable <- renderTable({
    dataset <- datasetInput()
    d=dataset[which(dataset$gene==input$textgene),]
    names(d)=c("Gene","P","CmafUsed","NsnpsUsed","MinP","MinGene","Ratio")
    if(dim(d)[1]==0) {'No entry found'} else return(d)    
  })  

What I am looking for is to have:

Gene    P       CmafUsed    NsnpsUsed   MinP    MinGene Ratio
GENE1   1.50E-05    0.20    12        4.62E-05  GENE2   1.11

Upvotes: 3

Views: 3103

Answers (2)

Mike Wise
Mike Wise

Reputation: 22807

Try adding:

  d$P <- sprintf("%7.2e",d$P)
  d$MinP <- sprintf("%7.2e",d$MinP)

I like using sprintf because it works in many languages, so I have less to remember. But format like BigDataScientist suggested will work just as well.

Upvotes: 1

Tonio Liebrand
Tonio Liebrand

Reputation: 17689

Since you mentioned scientific, this comes to mind:

df = data.frame(P = 1e-6, Rest = 1e-6)
df$P <- format(df$P, scientific = FALSE)
df$Rest <- format(df$Rest, scientific = TRUE)

Upvotes: 3

Related Questions