Conditional Formatting Cell R DataTable
up vote
0
down vote
favorite
I need to implement conditional formatting in my datatable. See an example below. What I need is to highlight the third row, "PercentDone", based on if that number is greater than the thresholds in rows 4 and 5.
If the number is greater than 50%, I would like to highlight it green.
If it is between 25% and 50%, I would like to highlight it yellow.
If it is under 25%, I would like to highlight it red.
This is similar to something someone would do in Excel with conditional formatting, I am just unsure as to how to implement it in a datatable in R.
In the example below, the 46% in column 1 should be yellow, the 11% in column 2 should be red, and the 65% in column 3 should be green.
df = data.frame(
c(51, 59, '46%', '25%', '50%'),
c(12, 93, '11%', '25%', '50%'),
c(40, 22, '65%', '25%', '50%'))
colnames(df) = c('Location1', 'Location2', 'Location3')
rownames(df) = c('Done', 'Need', 'PercentDone', 'Threshold1', 'Threshold2')
DT = datatable(df) %>%
formatStyle(...)
r datatable
add a comment |
up vote
0
down vote
favorite
I need to implement conditional formatting in my datatable. See an example below. What I need is to highlight the third row, "PercentDone", based on if that number is greater than the thresholds in rows 4 and 5.
If the number is greater than 50%, I would like to highlight it green.
If it is between 25% and 50%, I would like to highlight it yellow.
If it is under 25%, I would like to highlight it red.
This is similar to something someone would do in Excel with conditional formatting, I am just unsure as to how to implement it in a datatable in R.
In the example below, the 46% in column 1 should be yellow, the 11% in column 2 should be red, and the 65% in column 3 should be green.
df = data.frame(
c(51, 59, '46%', '25%', '50%'),
c(12, 93, '11%', '25%', '50%'),
c(40, 22, '65%', '25%', '50%'))
colnames(df) = c('Location1', 'Location2', 'Location3')
rownames(df) = c('Done', 'Need', 'PercentDone', 'Threshold1', 'Threshold2')
DT = datatable(df) %>%
formatStyle(...)
r datatable
Do the thresholds change each time or are they fixed?
– GGamba
Jun 6 '17 at 1:07
Is the mixed-format (numeric and character) for each column of your df done on purpose?
– Adam Quek
Jun 6 '17 at 3:38
Thank you for your responses. Yes, it's on purpose, as the table must look this way. I suppose getting the number version of row 3 could be done byas.numeric(gsub('%', '', df[3,]))
. The thresholds are fixed, which does make it a little easier. I'm mainly confused by what to put in theformatStyle()
.
– tsouchlarakis
Jun 6 '17 at 16:14
Does anyone know of a way to achieve this?
– tsouchlarakis
Jul 13 '17 at 20:54
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to implement conditional formatting in my datatable. See an example below. What I need is to highlight the third row, "PercentDone", based on if that number is greater than the thresholds in rows 4 and 5.
If the number is greater than 50%, I would like to highlight it green.
If it is between 25% and 50%, I would like to highlight it yellow.
If it is under 25%, I would like to highlight it red.
This is similar to something someone would do in Excel with conditional formatting, I am just unsure as to how to implement it in a datatable in R.
In the example below, the 46% in column 1 should be yellow, the 11% in column 2 should be red, and the 65% in column 3 should be green.
df = data.frame(
c(51, 59, '46%', '25%', '50%'),
c(12, 93, '11%', '25%', '50%'),
c(40, 22, '65%', '25%', '50%'))
colnames(df) = c('Location1', 'Location2', 'Location3')
rownames(df) = c('Done', 'Need', 'PercentDone', 'Threshold1', 'Threshold2')
DT = datatable(df) %>%
formatStyle(...)
r datatable
I need to implement conditional formatting in my datatable. See an example below. What I need is to highlight the third row, "PercentDone", based on if that number is greater than the thresholds in rows 4 and 5.
If the number is greater than 50%, I would like to highlight it green.
If it is between 25% and 50%, I would like to highlight it yellow.
If it is under 25%, I would like to highlight it red.
This is similar to something someone would do in Excel with conditional formatting, I am just unsure as to how to implement it in a datatable in R.
In the example below, the 46% in column 1 should be yellow, the 11% in column 2 should be red, and the 65% in column 3 should be green.
df = data.frame(
c(51, 59, '46%', '25%', '50%'),
c(12, 93, '11%', '25%', '50%'),
c(40, 22, '65%', '25%', '50%'))
colnames(df) = c('Location1', 'Location2', 'Location3')
rownames(df) = c('Done', 'Need', 'PercentDone', 'Threshold1', 'Threshold2')
DT = datatable(df) %>%
formatStyle(...)
r datatable
r datatable
asked Jun 5 '17 at 21:35
tsouchlarakis
354518
354518
Do the thresholds change each time or are they fixed?
– GGamba
Jun 6 '17 at 1:07
Is the mixed-format (numeric and character) for each column of your df done on purpose?
– Adam Quek
Jun 6 '17 at 3:38
Thank you for your responses. Yes, it's on purpose, as the table must look this way. I suppose getting the number version of row 3 could be done byas.numeric(gsub('%', '', df[3,]))
. The thresholds are fixed, which does make it a little easier. I'm mainly confused by what to put in theformatStyle()
.
– tsouchlarakis
Jun 6 '17 at 16:14
Does anyone know of a way to achieve this?
– tsouchlarakis
Jul 13 '17 at 20:54
add a comment |
Do the thresholds change each time or are they fixed?
– GGamba
Jun 6 '17 at 1:07
Is the mixed-format (numeric and character) for each column of your df done on purpose?
– Adam Quek
Jun 6 '17 at 3:38
Thank you for your responses. Yes, it's on purpose, as the table must look this way. I suppose getting the number version of row 3 could be done byas.numeric(gsub('%', '', df[3,]))
. The thresholds are fixed, which does make it a little easier. I'm mainly confused by what to put in theformatStyle()
.
– tsouchlarakis
Jun 6 '17 at 16:14
Does anyone know of a way to achieve this?
– tsouchlarakis
Jul 13 '17 at 20:54
Do the thresholds change each time or are they fixed?
– GGamba
Jun 6 '17 at 1:07
Do the thresholds change each time or are they fixed?
– GGamba
Jun 6 '17 at 1:07
Is the mixed-format (numeric and character) for each column of your df done on purpose?
– Adam Quek
Jun 6 '17 at 3:38
Is the mixed-format (numeric and character) for each column of your df done on purpose?
– Adam Quek
Jun 6 '17 at 3:38
Thank you for your responses. Yes, it's on purpose, as the table must look this way. I suppose getting the number version of row 3 could be done by
as.numeric(gsub('%', '', df[3,]))
. The thresholds are fixed, which does make it a little easier. I'm mainly confused by what to put in the formatStyle()
.– tsouchlarakis
Jun 6 '17 at 16:14
Thank you for your responses. Yes, it's on purpose, as the table must look this way. I suppose getting the number version of row 3 could be done by
as.numeric(gsub('%', '', df[3,]))
. The thresholds are fixed, which does make it a little easier. I'm mainly confused by what to put in the formatStyle()
.– tsouchlarakis
Jun 6 '17 at 16:14
Does anyone know of a way to achieve this?
– tsouchlarakis
Jul 13 '17 at 20:54
Does anyone know of a way to achieve this?
– tsouchlarakis
Jul 13 '17 at 20:54
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Late reply, but might prove useful for someone else so I'm posting.
If transposing the table is not a problem, you can try the following.
The final table should look like this:
DF = data.frame(
c(51, 59, '46%', '25%', '50%'),
c(12, 93, '11%', '25%', '50%'),
c(40, 22, '65%', '25%', '50%'), stringsAsFactors = FALSE )# variables as chr, w/o factor levels)
colnames(DF) = c('Location1', 'Location2', 'Location3')
rownames(DF) = c('Done', 'Need', 'PercentDone', 'Threshold1', 'Threshold2')
head(DF)
Retrieve only the numbers from the percentages, convert to numeric to be able to perform the comparisons:
# Define function for retrieving digits; One-liner courtesy of @stla at GitHub
Numextract <- function(string){ unlist(regmatches(string,gregexpr("[[:digit:]]+\.*[[:digit:]]*",string)))}
# Apply Numextract to all dataframe;
# retrieves only digits but still class is chr
DF [,] <- lapply(DF[,], Numextract)
# Convert to numeric to allow for comparison
DF [,] <- lapply(DF[,], as.numeric)
# Transpose dataframe to access the `PercentDone` as a column
DF = t(DF)
Consider removing the values from the dataframe and have as vars
Threshold1 = 25
Threshold2 = 50
Customizing datatable: Highlight PercentDone
DT::datatable(DF,
filter = "bottom",
caption = "I am the title",
# OPTIONS:
options = list(
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#000', 'color': '#fff'});",
"}"),
columnDefs = list(list(targets = length(colnames(DF)), visible = TRUE)))) %>%
# Change fontsize of cell values
formatStyle(columns = c(1:length(colnames(df))),
fontSize = "85%",
fontFamily = "Verdana")%>%
# Format column based on P.Value levels
formatStyle(fontWeight = 'bold',
# Format this:
"PercentDone",
# Font color
color = styleInterval(c(0.0), c('black', 'black')),
backgroundColor = styleInterval(c(Threshold1, Threshold2),
c('lightgreen', '#f4d35e', "tomato"))) -> fancyTable
# Print customized color-coded datatable:
fancyTable
I'd suggest uploading the image using StackOverflow's built-in system, especially if you are linking to someone else's dropbox. In the event the link goes dead.
– Anonymous coward
Nov 14 at 15:24
1
Thanks for your answer. I have moved on from this project in time, but I hope this may help someone in the future. Thanks again.
– tsouchlarakis
Nov 14 at 19:04
@Anonymouscoward Thanks for suggestion. I am new and posting a picture is not allowed unless one reaches a certain number of reputation points. Ideas on how to alt post an image very welcome
– cgpu
Nov 15 at 14:18
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Late reply, but might prove useful for someone else so I'm posting.
If transposing the table is not a problem, you can try the following.
The final table should look like this:
DF = data.frame(
c(51, 59, '46%', '25%', '50%'),
c(12, 93, '11%', '25%', '50%'),
c(40, 22, '65%', '25%', '50%'), stringsAsFactors = FALSE )# variables as chr, w/o factor levels)
colnames(DF) = c('Location1', 'Location2', 'Location3')
rownames(DF) = c('Done', 'Need', 'PercentDone', 'Threshold1', 'Threshold2')
head(DF)
Retrieve only the numbers from the percentages, convert to numeric to be able to perform the comparisons:
# Define function for retrieving digits; One-liner courtesy of @stla at GitHub
Numextract <- function(string){ unlist(regmatches(string,gregexpr("[[:digit:]]+\.*[[:digit:]]*",string)))}
# Apply Numextract to all dataframe;
# retrieves only digits but still class is chr
DF [,] <- lapply(DF[,], Numextract)
# Convert to numeric to allow for comparison
DF [,] <- lapply(DF[,], as.numeric)
# Transpose dataframe to access the `PercentDone` as a column
DF = t(DF)
Consider removing the values from the dataframe and have as vars
Threshold1 = 25
Threshold2 = 50
Customizing datatable: Highlight PercentDone
DT::datatable(DF,
filter = "bottom",
caption = "I am the title",
# OPTIONS:
options = list(
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#000', 'color': '#fff'});",
"}"),
columnDefs = list(list(targets = length(colnames(DF)), visible = TRUE)))) %>%
# Change fontsize of cell values
formatStyle(columns = c(1:length(colnames(df))),
fontSize = "85%",
fontFamily = "Verdana")%>%
# Format column based on P.Value levels
formatStyle(fontWeight = 'bold',
# Format this:
"PercentDone",
# Font color
color = styleInterval(c(0.0), c('black', 'black')),
backgroundColor = styleInterval(c(Threshold1, Threshold2),
c('lightgreen', '#f4d35e', "tomato"))) -> fancyTable
# Print customized color-coded datatable:
fancyTable
I'd suggest uploading the image using StackOverflow's built-in system, especially if you are linking to someone else's dropbox. In the event the link goes dead.
– Anonymous coward
Nov 14 at 15:24
1
Thanks for your answer. I have moved on from this project in time, but I hope this may help someone in the future. Thanks again.
– tsouchlarakis
Nov 14 at 19:04
@Anonymouscoward Thanks for suggestion. I am new and posting a picture is not allowed unless one reaches a certain number of reputation points. Ideas on how to alt post an image very welcome
– cgpu
Nov 15 at 14:18
add a comment |
up vote
1
down vote
accepted
Late reply, but might prove useful for someone else so I'm posting.
If transposing the table is not a problem, you can try the following.
The final table should look like this:
DF = data.frame(
c(51, 59, '46%', '25%', '50%'),
c(12, 93, '11%', '25%', '50%'),
c(40, 22, '65%', '25%', '50%'), stringsAsFactors = FALSE )# variables as chr, w/o factor levels)
colnames(DF) = c('Location1', 'Location2', 'Location3')
rownames(DF) = c('Done', 'Need', 'PercentDone', 'Threshold1', 'Threshold2')
head(DF)
Retrieve only the numbers from the percentages, convert to numeric to be able to perform the comparisons:
# Define function for retrieving digits; One-liner courtesy of @stla at GitHub
Numextract <- function(string){ unlist(regmatches(string,gregexpr("[[:digit:]]+\.*[[:digit:]]*",string)))}
# Apply Numextract to all dataframe;
# retrieves only digits but still class is chr
DF [,] <- lapply(DF[,], Numextract)
# Convert to numeric to allow for comparison
DF [,] <- lapply(DF[,], as.numeric)
# Transpose dataframe to access the `PercentDone` as a column
DF = t(DF)
Consider removing the values from the dataframe and have as vars
Threshold1 = 25
Threshold2 = 50
Customizing datatable: Highlight PercentDone
DT::datatable(DF,
filter = "bottom",
caption = "I am the title",
# OPTIONS:
options = list(
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#000', 'color': '#fff'});",
"}"),
columnDefs = list(list(targets = length(colnames(DF)), visible = TRUE)))) %>%
# Change fontsize of cell values
formatStyle(columns = c(1:length(colnames(df))),
fontSize = "85%",
fontFamily = "Verdana")%>%
# Format column based on P.Value levels
formatStyle(fontWeight = 'bold',
# Format this:
"PercentDone",
# Font color
color = styleInterval(c(0.0), c('black', 'black')),
backgroundColor = styleInterval(c(Threshold1, Threshold2),
c('lightgreen', '#f4d35e', "tomato"))) -> fancyTable
# Print customized color-coded datatable:
fancyTable
I'd suggest uploading the image using StackOverflow's built-in system, especially if you are linking to someone else's dropbox. In the event the link goes dead.
– Anonymous coward
Nov 14 at 15:24
1
Thanks for your answer. I have moved on from this project in time, but I hope this may help someone in the future. Thanks again.
– tsouchlarakis
Nov 14 at 19:04
@Anonymouscoward Thanks for suggestion. I am new and posting a picture is not allowed unless one reaches a certain number of reputation points. Ideas on how to alt post an image very welcome
– cgpu
Nov 15 at 14:18
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Late reply, but might prove useful for someone else so I'm posting.
If transposing the table is not a problem, you can try the following.
The final table should look like this:
DF = data.frame(
c(51, 59, '46%', '25%', '50%'),
c(12, 93, '11%', '25%', '50%'),
c(40, 22, '65%', '25%', '50%'), stringsAsFactors = FALSE )# variables as chr, w/o factor levels)
colnames(DF) = c('Location1', 'Location2', 'Location3')
rownames(DF) = c('Done', 'Need', 'PercentDone', 'Threshold1', 'Threshold2')
head(DF)
Retrieve only the numbers from the percentages, convert to numeric to be able to perform the comparisons:
# Define function for retrieving digits; One-liner courtesy of @stla at GitHub
Numextract <- function(string){ unlist(regmatches(string,gregexpr("[[:digit:]]+\.*[[:digit:]]*",string)))}
# Apply Numextract to all dataframe;
# retrieves only digits but still class is chr
DF [,] <- lapply(DF[,], Numextract)
# Convert to numeric to allow for comparison
DF [,] <- lapply(DF[,], as.numeric)
# Transpose dataframe to access the `PercentDone` as a column
DF = t(DF)
Consider removing the values from the dataframe and have as vars
Threshold1 = 25
Threshold2 = 50
Customizing datatable: Highlight PercentDone
DT::datatable(DF,
filter = "bottom",
caption = "I am the title",
# OPTIONS:
options = list(
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#000', 'color': '#fff'});",
"}"),
columnDefs = list(list(targets = length(colnames(DF)), visible = TRUE)))) %>%
# Change fontsize of cell values
formatStyle(columns = c(1:length(colnames(df))),
fontSize = "85%",
fontFamily = "Verdana")%>%
# Format column based on P.Value levels
formatStyle(fontWeight = 'bold',
# Format this:
"PercentDone",
# Font color
color = styleInterval(c(0.0), c('black', 'black')),
backgroundColor = styleInterval(c(Threshold1, Threshold2),
c('lightgreen', '#f4d35e', "tomato"))) -> fancyTable
# Print customized color-coded datatable:
fancyTable
Late reply, but might prove useful for someone else so I'm posting.
If transposing the table is not a problem, you can try the following.
The final table should look like this:
DF = data.frame(
c(51, 59, '46%', '25%', '50%'),
c(12, 93, '11%', '25%', '50%'),
c(40, 22, '65%', '25%', '50%'), stringsAsFactors = FALSE )# variables as chr, w/o factor levels)
colnames(DF) = c('Location1', 'Location2', 'Location3')
rownames(DF) = c('Done', 'Need', 'PercentDone', 'Threshold1', 'Threshold2')
head(DF)
Retrieve only the numbers from the percentages, convert to numeric to be able to perform the comparisons:
# Define function for retrieving digits; One-liner courtesy of @stla at GitHub
Numextract <- function(string){ unlist(regmatches(string,gregexpr("[[:digit:]]+\.*[[:digit:]]*",string)))}
# Apply Numextract to all dataframe;
# retrieves only digits but still class is chr
DF [,] <- lapply(DF[,], Numextract)
# Convert to numeric to allow for comparison
DF [,] <- lapply(DF[,], as.numeric)
# Transpose dataframe to access the `PercentDone` as a column
DF = t(DF)
Consider removing the values from the dataframe and have as vars
Threshold1 = 25
Threshold2 = 50
Customizing datatable: Highlight PercentDone
DT::datatable(DF,
filter = "bottom",
caption = "I am the title",
# OPTIONS:
options = list(
initComplete = JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#000', 'color': '#fff'});",
"}"),
columnDefs = list(list(targets = length(colnames(DF)), visible = TRUE)))) %>%
# Change fontsize of cell values
formatStyle(columns = c(1:length(colnames(df))),
fontSize = "85%",
fontFamily = "Verdana")%>%
# Format column based on P.Value levels
formatStyle(fontWeight = 'bold',
# Format this:
"PercentDone",
# Font color
color = styleInterval(c(0.0), c('black', 'black')),
backgroundColor = styleInterval(c(Threshold1, Threshold2),
c('lightgreen', '#f4d35e', "tomato"))) -> fancyTable
# Print customized color-coded datatable:
fancyTable
edited Nov 19 at 11:51
answered Nov 14 at 15:18
cgpu
263
263
I'd suggest uploading the image using StackOverflow's built-in system, especially if you are linking to someone else's dropbox. In the event the link goes dead.
– Anonymous coward
Nov 14 at 15:24
1
Thanks for your answer. I have moved on from this project in time, but I hope this may help someone in the future. Thanks again.
– tsouchlarakis
Nov 14 at 19:04
@Anonymouscoward Thanks for suggestion. I am new and posting a picture is not allowed unless one reaches a certain number of reputation points. Ideas on how to alt post an image very welcome
– cgpu
Nov 15 at 14:18
add a comment |
I'd suggest uploading the image using StackOverflow's built-in system, especially if you are linking to someone else's dropbox. In the event the link goes dead.
– Anonymous coward
Nov 14 at 15:24
1
Thanks for your answer. I have moved on from this project in time, but I hope this may help someone in the future. Thanks again.
– tsouchlarakis
Nov 14 at 19:04
@Anonymouscoward Thanks for suggestion. I am new and posting a picture is not allowed unless one reaches a certain number of reputation points. Ideas on how to alt post an image very welcome
– cgpu
Nov 15 at 14:18
I'd suggest uploading the image using StackOverflow's built-in system, especially if you are linking to someone else's dropbox. In the event the link goes dead.
– Anonymous coward
Nov 14 at 15:24
I'd suggest uploading the image using StackOverflow's built-in system, especially if you are linking to someone else's dropbox. In the event the link goes dead.
– Anonymous coward
Nov 14 at 15:24
1
1
Thanks for your answer. I have moved on from this project in time, but I hope this may help someone in the future. Thanks again.
– tsouchlarakis
Nov 14 at 19:04
Thanks for your answer. I have moved on from this project in time, but I hope this may help someone in the future. Thanks again.
– tsouchlarakis
Nov 14 at 19:04
@Anonymouscoward Thanks for suggestion. I am new and posting a picture is not allowed unless one reaches a certain number of reputation points. Ideas on how to alt post an image very welcome
– cgpu
Nov 15 at 14:18
@Anonymouscoward Thanks for suggestion. I am new and posting a picture is not allowed unless one reaches a certain number of reputation points. Ideas on how to alt post an image very welcome
– cgpu
Nov 15 at 14:18
add a comment |
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%2f44378056%2fconditional-formatting-cell-r-datatable%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
Do the thresholds change each time or are they fixed?
– GGamba
Jun 6 '17 at 1:07
Is the mixed-format (numeric and character) for each column of your df done on purpose?
– Adam Quek
Jun 6 '17 at 3:38
Thank you for your responses. Yes, it's on purpose, as the table must look this way. I suppose getting the number version of row 3 could be done by
as.numeric(gsub('%', '', df[3,]))
. The thresholds are fixed, which does make it a little easier. I'm mainly confused by what to put in theformatStyle()
.– tsouchlarakis
Jun 6 '17 at 16:14
Does anyone know of a way to achieve this?
– tsouchlarakis
Jul 13 '17 at 20:54