Changing the background color of rows in a datatable using rownames
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
add a comment |
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
2
You need to paste the output ofdput(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
add a comment |
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
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
r shiny shinydashboard shinyjs shiny-reactivity
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 ofdput(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
add a comment |
2
You need to paste the output ofdput(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
add a comment |
1 Answer
1
active
oldest
votes
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
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 thedf
object or thetable
object.table
shouldn't have rownames becuase its a table, anddf
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
|
show 1 more comment
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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 thedf
object or thetable
object.table
shouldn't have rownames becuase its a table, anddf
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
|
show 1 more comment
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
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 thedf
object or thetable
object.table
shouldn't have rownames becuase its a table, anddf
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
|
show 1 more comment
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
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
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 thedf
object or thetable
object.table
shouldn't have rownames becuase its a table, anddf
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
|
show 1 more comment
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 thedf
object or thetable
object.table
shouldn't have rownames becuase its a table, anddf
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
|
show 1 more comment
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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