I have the following table:
|name|Tin|Tout|Hin|Hout|DT min/2|alpha|
|-----|-----|-----|-----|-----|-----|-----|
|water|20 |15 |2.33 |0 |5 |1 |
|waste|15 |20 |0 |2.12 |5 |1 |
|air |20 |15 |0.03 |0 |5 |1 |
|wast |15 |20 |0 |0.03 |5 |1 |
|co2 |15 |20 |0 |0.45 |5 |1 |
And the following number, declared in another chunk.
pinch = 16
I would like to highlight in red all the rows for which Tin < pinch < Tout
. I know in this example it is the case for all rows, however I have many other tables like this and I would like to automate this process.
Does anyone know how to do this ?
I tried looking for other solutions to similar problems, however I found it quite hard to adapt said solutions to my problem, as they were too specific. Plus, I don't have much knowledge with R yet to understand the code written by others.
This would help me greatly !
CodePudding user response:
You could use kableExtra
and dplyr
as in the following code. This assumes your data is in a data frame.
library(knitr)
library(kableExtra)
library(dplyr)
dfs <- "name|Tin|Tout|Hin|Hout|DT_min2|alpha
water|20 |15 |2.33 |0 |5 |1
waste|15 |20 |0 |2.12 |5 |1
air |20 |15 |0.03 |0 |5 |1
wast |15 |20 |0 |0.03 |5 |1
co2 |15 |20 |0 |0.45 |5 |1"
df <- read.table(text=dfs, sep='|', header=T)
pinch <- 16
red <-
df %>%
mutate(red = ifelse(Tin < pinch & pinch < Tout, TRUE, FALSE)) %>%
mutate(row=row_number()) %>%
select(red, row)
kable(df) %>% kable_styling(full_width = T) %>% row_spec(red$row[red$red], color='red')
The dataframe called red
uses ifelse
to work out the row numbers to colour red in the call to kableExtra::row_spec
.