Changing the background color of rows in a datatable using rownames












0















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?










share|improve this question




















  • 2





    You need to paste the output of dput(test) not the command itself :P

    – RAB
    Jan 3 at 0:31






  • 1





    @RAB can you check the edit

    – Pradeep Chintapalli
    Jan 3 at 16:56











  • Almost, but I have included data the way you should have it in my answer

    – RAB
    Jan 3 at 21:53
















0















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?










share|improve this question




















  • 2





    You need to paste the output of dput(test) not the command itself :P

    – RAB
    Jan 3 at 0:31






  • 1





    @RAB can you check the edit

    – Pradeep Chintapalli
    Jan 3 at 16:56











  • Almost, but I have included data the way you should have it in my answer

    – RAB
    Jan 3 at 21:53














0












0








0








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?










share|improve this question
















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?







r shiny shinydashboard shinyjs shiny-reactivity






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 23:22









RAB

1,416418




1,416418










asked Jan 3 at 0:27









Pradeep ChintapalliPradeep Chintapalli

146




146








  • 2





    You need to paste the output of dput(test) not the command itself :P

    – RAB
    Jan 3 at 0:31






  • 1





    @RAB can you check the edit

    – Pradeep Chintapalli
    Jan 3 at 16:56











  • Almost, but I have included data the way you should have it in my answer

    – RAB
    Jan 3 at 21:53














  • 2





    You need to paste the output of dput(test) not the command itself :P

    – RAB
    Jan 3 at 0:31






  • 1





    @RAB can you check the edit

    – Pradeep Chintapalli
    Jan 3 at 16:56











  • Almost, but I have included data the way you should have it in my answer

    – RAB
    Jan 3 at 21:53








2




2





You need to paste the output of dput(test) not the command itself :P

– RAB
Jan 3 at 0:31





You need to paste the output of dput(test) not the command itself :P

– RAB
Jan 3 at 0:31




1




1





@RAB can you check the edit

– Pradeep Chintapalli
Jan 3 at 16:56





@RAB can you check the edit

– Pradeep Chintapalli
Jan 3 at 16:56













Almost, but I have included data the way you should have it in my answer

– RAB
Jan 3 at 21:53





Almost, but I have included data the way you should have it in my answer

– RAB
Jan 3 at 21:53












1 Answer
1






active

oldest

votes


















1














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






share|improve this answer


























  • 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 3 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!

    – RAB
    Jan 3 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 3 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 3 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: rstudio.github.io/DT/010-style.html

    – RAB
    Jan 3 at 23:20












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014909%2fchanging-the-background-color-of-rows-in-a-datatable-using-rownames%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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






share|improve this answer


























  • 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 3 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!

    – RAB
    Jan 3 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 3 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 3 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: rstudio.github.io/DT/010-style.html

    – RAB
    Jan 3 at 23:20
















1














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






share|improve this answer


























  • 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 3 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!

    – RAB
    Jan 3 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 3 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 3 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: rstudio.github.io/DT/010-style.html

    – RAB
    Jan 3 at 23:20














1












1








1







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






share|improve this answer















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







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 21:58

























answered Jan 3 at 21:49









RABRAB

1,416418




1,416418













  • 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 3 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!

    – RAB
    Jan 3 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 3 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 3 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: rstudio.github.io/DT/010-style.html

    – RAB
    Jan 3 at 23:20



















  • 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 3 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!

    – RAB
    Jan 3 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 3 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 3 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: rstudio.github.io/DT/010-style.html

    – RAB
    Jan 3 at 23:20

















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 3 at 22:25







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 3 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!

– RAB
Jan 3 at 22:37





They should be able to, they integrate pretty well with most things. I don't know how to modify a traditional data.frame, sorry!

– RAB
Jan 3 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 3 at 23:02





My row and column names seem to have disappeared after doing what you did. Any workaround for that? @RAB

– Pradeep Chintapalli
Jan 3 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 3 at 23:15





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 3 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: rstudio.github.io/DT/010-style.html

– RAB
Jan 3 at 23:20





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: rstudio.github.io/DT/010-style.html

– RAB
Jan 3 at 23:20




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014909%2fchanging-the-background-color-of-rows-in-a-datatable-using-rownames%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith