Coloring rows with kableExtra based on cell values
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I use R markdown in combination with LaTeX to create PDF reports. To generate tables I use the kableExtra package.
I'd like to change the styling of rows based on the values in a particular column. I've been trying cell_spec, but as far as I can see it only changes the styling of the value in the column and not the entire row.
For example, in the table below I'd like to highlight all cars with more than six cylinders. So the result should be:
Which I made with the following code:
df %>%
kable(booktabs = T) %>%
kable_styling() %>%
row_spec(5, bold = T, color = "white", background = "red")
Here I specified a row number, which I don't want to do. It should be decided automatically based on the number of cylinders. Using cell_spec, I'm able to get the following:
Which I made with the following code:
df %>%
rownames_to_column('cars') %>% # used to store row names (mutate deletes them)
mutate(
cyl = cell_spec(cyl, color = ifelse(cyl > 6, "white", "black"),
background = ifelse(cyl > 6, "red", "white"),
bold = ifelse(cyl > 6, T, F))) %>%
column_to_rownames('cars') %>% # used to put row names back in place
kable(escape = F, booktabs = T) %>%
kable_styling()
But this changes only the value in the cylinder column, and not the rest of the row.
Is there a solution to my problem (preferrably without specifying the condition for every styling option)?
EDIT:
This is a different question than this one, where someone wants to format a column based on values in that column, but wishes to exclude certain rows/entries. I don't want to exclude any rows, but I want the formatting to apply to the entire row (still conditioning on values in a particular column).
r r-markdown knitr kable kableextra
add a comment |
I use R markdown in combination with LaTeX to create PDF reports. To generate tables I use the kableExtra package.
I'd like to change the styling of rows based on the values in a particular column. I've been trying cell_spec, but as far as I can see it only changes the styling of the value in the column and not the entire row.
For example, in the table below I'd like to highlight all cars with more than six cylinders. So the result should be:
Which I made with the following code:
df %>%
kable(booktabs = T) %>%
kable_styling() %>%
row_spec(5, bold = T, color = "white", background = "red")
Here I specified a row number, which I don't want to do. It should be decided automatically based on the number of cylinders. Using cell_spec, I'm able to get the following:
Which I made with the following code:
df %>%
rownames_to_column('cars') %>% # used to store row names (mutate deletes them)
mutate(
cyl = cell_spec(cyl, color = ifelse(cyl > 6, "white", "black"),
background = ifelse(cyl > 6, "red", "white"),
bold = ifelse(cyl > 6, T, F))) %>%
column_to_rownames('cars') %>% # used to put row names back in place
kable(escape = F, booktabs = T) %>%
kable_styling()
But this changes only the value in the cylinder column, and not the rest of the row.
Is there a solution to my problem (preferrably without specifying the condition for every styling option)?
EDIT:
This is a different question than this one, where someone wants to format a column based on values in that column, but wishes to exclude certain rows/entries. I don't want to exclude any rows, but I want the formatting to apply to the entire row (still conditioning on values in a particular column).
r r-markdown knitr kable kableextra
1
Hi SPK, have you taken a look at stackoverflow.com/questions/50665721/… ? If your question is different, it may be useful to reference that answer in an edit and explain how what you are looking for is different. Thanks and good luck :)
– mysteRious
Nov 16 '18 at 16:00
@mysteRious: thanks for you comment. I've added the difference between my question and the one you mention.
– SPK.z
Nov 19 '18 at 9:12
add a comment |
I use R markdown in combination with LaTeX to create PDF reports. To generate tables I use the kableExtra package.
I'd like to change the styling of rows based on the values in a particular column. I've been trying cell_spec, but as far as I can see it only changes the styling of the value in the column and not the entire row.
For example, in the table below I'd like to highlight all cars with more than six cylinders. So the result should be:
Which I made with the following code:
df %>%
kable(booktabs = T) %>%
kable_styling() %>%
row_spec(5, bold = T, color = "white", background = "red")
Here I specified a row number, which I don't want to do. It should be decided automatically based on the number of cylinders. Using cell_spec, I'm able to get the following:
Which I made with the following code:
df %>%
rownames_to_column('cars') %>% # used to store row names (mutate deletes them)
mutate(
cyl = cell_spec(cyl, color = ifelse(cyl > 6, "white", "black"),
background = ifelse(cyl > 6, "red", "white"),
bold = ifelse(cyl > 6, T, F))) %>%
column_to_rownames('cars') %>% # used to put row names back in place
kable(escape = F, booktabs = T) %>%
kable_styling()
But this changes only the value in the cylinder column, and not the rest of the row.
Is there a solution to my problem (preferrably without specifying the condition for every styling option)?
EDIT:
This is a different question than this one, where someone wants to format a column based on values in that column, but wishes to exclude certain rows/entries. I don't want to exclude any rows, but I want the formatting to apply to the entire row (still conditioning on values in a particular column).
r r-markdown knitr kable kableextra
I use R markdown in combination with LaTeX to create PDF reports. To generate tables I use the kableExtra package.
I'd like to change the styling of rows based on the values in a particular column. I've been trying cell_spec, but as far as I can see it only changes the styling of the value in the column and not the entire row.
For example, in the table below I'd like to highlight all cars with more than six cylinders. So the result should be:
Which I made with the following code:
df %>%
kable(booktabs = T) %>%
kable_styling() %>%
row_spec(5, bold = T, color = "white", background = "red")
Here I specified a row number, which I don't want to do. It should be decided automatically based on the number of cylinders. Using cell_spec, I'm able to get the following:
Which I made with the following code:
df %>%
rownames_to_column('cars') %>% # used to store row names (mutate deletes them)
mutate(
cyl = cell_spec(cyl, color = ifelse(cyl > 6, "white", "black"),
background = ifelse(cyl > 6, "red", "white"),
bold = ifelse(cyl > 6, T, F))) %>%
column_to_rownames('cars') %>% # used to put row names back in place
kable(escape = F, booktabs = T) %>%
kable_styling()
But this changes only the value in the cylinder column, and not the rest of the row.
Is there a solution to my problem (preferrably without specifying the condition for every styling option)?
EDIT:
This is a different question than this one, where someone wants to format a column based on values in that column, but wishes to exclude certain rows/entries. I don't want to exclude any rows, but I want the formatting to apply to the entire row (still conditioning on values in a particular column).
r r-markdown knitr kable kableextra
r r-markdown knitr kable kableextra
edited Nov 19 '18 at 9:11
SPK.z
asked Nov 16 '18 at 15:45
SPK.zSPK.z
1186
1186
1
Hi SPK, have you taken a look at stackoverflow.com/questions/50665721/… ? If your question is different, it may be useful to reference that answer in an edit and explain how what you are looking for is different. Thanks and good luck :)
– mysteRious
Nov 16 '18 at 16:00
@mysteRious: thanks for you comment. I've added the difference between my question and the one you mention.
– SPK.z
Nov 19 '18 at 9:12
add a comment |
1
Hi SPK, have you taken a look at stackoverflow.com/questions/50665721/… ? If your question is different, it may be useful to reference that answer in an edit and explain how what you are looking for is different. Thanks and good luck :)
– mysteRious
Nov 16 '18 at 16:00
@mysteRious: thanks for you comment. I've added the difference between my question and the one you mention.
– SPK.z
Nov 19 '18 at 9:12
1
1
Hi SPK, have you taken a look at stackoverflow.com/questions/50665721/… ? If your question is different, it may be useful to reference that answer in an edit and explain how what you are looking for is different. Thanks and good luck :)
– mysteRious
Nov 16 '18 at 16:00
Hi SPK, have you taken a look at stackoverflow.com/questions/50665721/… ? If your question is different, it may be useful to reference that answer in an edit and explain how what you are looking for is different. Thanks and good luck :)
– mysteRious
Nov 16 '18 at 16:00
@mysteRious: thanks for you comment. I've added the difference between my question and the one you mention.
– SPK.z
Nov 19 '18 at 9:12
@mysteRious: thanks for you comment. I've added the difference between my question and the one you mention.
– SPK.z
Nov 19 '18 at 9:12
add a comment |
1 Answer
1
active
oldest
votes
I think the easiest way is to pass a list of row to be coloured based on your criterias.
library(kableExtra)
df<- mtcars
color.me <- which(df$cyl >6)
df %>%
kable(booktabs = T) %>%
kable_styling() %>%
row_spec(color.me, bold = T, color = "white", background = "red")
or in the same pipe as:
df %>%
kable(booktabs = T) %>%
kable_styling() %>%
row_spec(which(df$cyl >6), bold = T, color = "white", background = "red")
Thanks, this is exactly what I need!
– SPK.z
Nov 19 '18 at 9:13
add a 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%2f53341155%2fcoloring-rows-with-kableextra-based-on-cell-values%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
I think the easiest way is to pass a list of row to be coloured based on your criterias.
library(kableExtra)
df<- mtcars
color.me <- which(df$cyl >6)
df %>%
kable(booktabs = T) %>%
kable_styling() %>%
row_spec(color.me, bold = T, color = "white", background = "red")
or in the same pipe as:
df %>%
kable(booktabs = T) %>%
kable_styling() %>%
row_spec(which(df$cyl >6), bold = T, color = "white", background = "red")
Thanks, this is exactly what I need!
– SPK.z
Nov 19 '18 at 9:13
add a comment |
I think the easiest way is to pass a list of row to be coloured based on your criterias.
library(kableExtra)
df<- mtcars
color.me <- which(df$cyl >6)
df %>%
kable(booktabs = T) %>%
kable_styling() %>%
row_spec(color.me, bold = T, color = "white", background = "red")
or in the same pipe as:
df %>%
kable(booktabs = T) %>%
kable_styling() %>%
row_spec(which(df$cyl >6), bold = T, color = "white", background = "red")
Thanks, this is exactly what I need!
– SPK.z
Nov 19 '18 at 9:13
add a comment |
I think the easiest way is to pass a list of row to be coloured based on your criterias.
library(kableExtra)
df<- mtcars
color.me <- which(df$cyl >6)
df %>%
kable(booktabs = T) %>%
kable_styling() %>%
row_spec(color.me, bold = T, color = "white", background = "red")
or in the same pipe as:
df %>%
kable(booktabs = T) %>%
kable_styling() %>%
row_spec(which(df$cyl >6), bold = T, color = "white", background = "red")
I think the easiest way is to pass a list of row to be coloured based on your criterias.
library(kableExtra)
df<- mtcars
color.me <- which(df$cyl >6)
df %>%
kable(booktabs = T) %>%
kable_styling() %>%
row_spec(color.me, bold = T, color = "white", background = "red")
or in the same pipe as:
df %>%
kable(booktabs = T) %>%
kable_styling() %>%
row_spec(which(df$cyl >6), bold = T, color = "white", background = "red")
answered Nov 16 '18 at 16:02
Jrakru56Jrakru56
609212
609212
Thanks, this is exactly what I need!
– SPK.z
Nov 19 '18 at 9:13
add a comment |
Thanks, this is exactly what I need!
– SPK.z
Nov 19 '18 at 9:13
Thanks, this is exactly what I need!
– SPK.z
Nov 19 '18 at 9:13
Thanks, this is exactly what I need!
– SPK.z
Nov 19 '18 at 9:13
add a 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%2f53341155%2fcoloring-rows-with-kableextra-based-on-cell-values%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
1
Hi SPK, have you taken a look at stackoverflow.com/questions/50665721/… ? If your question is different, it may be useful to reference that answer in an edit and explain how what you are looking for is different. Thanks and good luck :)
– mysteRious
Nov 16 '18 at 16:00
@mysteRious: thanks for you comment. I've added the difference between my question and the one you mention.
– SPK.z
Nov 19 '18 at 9:12