Colored box around row or column with xtable

tl;dr but for xtable

I would like to call out the occasional row or column in a table during a presentation. With other tools, I would draw a colored box around the data in question. I'm not sure how to do that with xtable. I could probably massage the sanitize.text.function to print the values in question a different color, but I was hoping for a box-style solution. Help?

This will probably be easier if you use the kableExtra package or the huxtable package to generate the tables. Both packages provide interfaces that make it easier (compared with xtable) to generate latex tables in an rmarkdown document with complex formatting and highlighting. The hyperlinks above go to vignettes with examples for each package.


Here's a simple huxtable-based example (I'm the package author):

my_data <- matrix(rnorm(100), 10, 10)
my_hux <- huxtable(my_data)

# row 3, column 5, coloured border:
my_hux <- set_all_borders(my_hux, 3, 5, 1)
my_hux <- set_all_border_colors(my_hux, 3, 5, "red")

# row 7, columns 2-5, orange background:
background_color(my_hux)[7, 2:5] <- "orange"

# yellow background wherever cell value > 1
my_hux <- set_background_color(my_hux, where(my_hux > 1), "yellow")