how to display a specific value in Shiny as a percentage

Hello All,

I would like to format the display output of numbers (in dataframe below) to percentage in Shiny R output. Essentially, I am displaying numbers in three rows: "X1", "X2", "X3". At the moment, my output is like: X1: 0.6692 X2: 0.8466 X3: 0.5890

What I'd like to have is X1: 66.92% X2: 84.66% X3: 58.90%

I tried using scales::percent instead of the "round" but I am getting errors. I was wondering what is the easiest way to do this? Thank you all in advance.

output$pred_info <- renderReactable({
a<-PRED_res_CI()
req(a)
a<-t(a)
rownames(a)<-c("X1","X2","X3")
a<-a[c(1,3,2),]

reactable(round(a %>% data.frame(),4),style = list(fontFamily = "sans-serif", fontSize = "1.3rem"),
          
          rowStyle = JS("function(rowInfo) {
  return { background: '#eee', borderLeft: '2px solid #ffa62d' }

If you're just looking to display them, I'd paste the values as strings in your data table. You can always convert back to numerics in the future by removing the % and utilizing the as.numeric function.

paste0(X*100,"%") #Converts decimal value to percent, then concatenates % symbol

as.numeric(gsub("%","",X)) #Removes % symbol from string and converts to numeric

Hope this helps!

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.