-1

I have a datatable which i want to customise based on the row names and the values present in the rows.

Customizing the color of the datables is generally done by the column names, but not the row names.

Below is my dataframe.

df <- data.frame(`1st`=c(100, 39, 5.6), 
                 `2nd`=c(200, 70, 5.9), 
                 `3rd`=c(230, 100, 6.3), 
                 `4th`=c(300, 98, 7.0))
rownames(df) <- c("Bins", "Accuracy", "SubYeild")

In my case, if you look at the third row ie SubmissionYield, i want the cell of that particular row to be green if it is greater then 6 or else red in color. Is there any way to achieve this?

morgan121
  • 2,213
  • 1
  • 15
  • 33

1 Answers1

0

Alright, here is one way to make pretty tables using the flextable package:

library(flextable) # may need install.packages("flextable") first
library(magrittr)  # for the '%>%' piping function

df <- data.frame(`1st`=c(100, 39, 5.6), 
                 `2nd`=c(200, 70, 5.9), 
                 `3rd`=c(230, 100, 6.3), 
                 `4th`=c(300, 98, 7.0))
rownames(df) <- c("Bins", "Accuracy", "SubYeild")

table <- df %>%
  regulartable() %>%    # turns it into a table
  bg(i = 3, j = which(df[3, ] > 6),  bg="green") %>%
  bg(i = 3, j = which(df[3, ] <= 6), bg="red") 

If you only want to colour text (not background) you can replace bg with color.

flextable is a great package for creating pretty tables. Hope this is what you are after.

EDIT: somehow missed the green/red bit, added it in now

morgan121
  • 2,213
  • 1
  • 15
  • 33
  • Thanks for the answer. It seems to be working fine. But, i need to render the resulting dataframe in shiny and flextables cant seem to be rendered. Or can they? – Pradeep Chintapalli Jan 03 '19 at 22:25
  • They should be able to, they integrate pretty well with most things. I don't know how to modify a traditional data.frame, sorry! – morgan121 Jan 03 '19 at 22:37
  • My row and column names seem to have disappeared after doing what you did. Any workaround for that? @RAB – Pradeep Chintapalli Jan 03 '19 at 23:02
  • Also, i would like to know is there any workaround for the color problem using the data table package itself. "formatstyle" puts emphasis on the column but not the rows. Is there any way to change the color of the row based on the row name, satisfying certain condition(Which is my initial question) – Pradeep Chintapalli Jan 03 '19 at 23:15
  • when you say disappeared, do you mean in the `df` object or the `table` object. `table` shouldn't have rownames becuase its a table, and `df` shouldn't change because it hasn't been modified directly... for data.table check out here: https://rstudio.github.io/DT/010-style.html – morgan121 Jan 03 '19 at 23:20
  • Well, when i turn it into a table, my row and column names are gone which i dont want them to want my datatable to retain all the customizations ive done to it along with the row and column names. As far as the data.table documentation, it clearly emphasizes the color change based on the column names but not rows.Is there any workaround for that? – Pradeep Chintapalli Jan 03 '19 at 23:31