Reputation: 1878
when I try to print HTML generated by knitr, the print page losses all colors and format. Is there any way to print HTML output as it is without losing formatting? I have tried getting around this by using css suggested by this however, does not make any difference.
styles.css
@media print {
body {-webkit-print-color-adjust: exact;}
}
@media print {
table {-webkit-print-color-adjust: exact;}
}
MARKDOWN CODE:
---
title: "Habits"
output:
html_document:
css: styles.css
---
```{r, echo = FALSE}
df <- data.frame(
id = 1:10,
name = c("Bob", "Ashley", "James", "David", "Jenny",
"Hans", "Leo", "John", "Emily", "Lee"),
age = c(28, 27, 30, 28, 29, 29, 27, 27, 31, 30),
grade = c("C", "A", "A", "C", "B", "B", "B", "A", "C", "C"),
test1_score = c(8.9, 9.5, 9.6, 8.9, 9.1, 9.3, 9.3, 9.9, 8.5, 8.6),
test2_score = c(9.1, 9.1, 9.2, 9.1, 8.9, 8.5, 9.2, 9.3, 9.1, 8.8),
final_score = c(9, 9.3, 9.4, 9, 9, 8.9, 9.25, 9.6, 8.8, 8.7),
registered = c(TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE),
stringsAsFactors = FALSE)
```
``` {r, eval = TRUE, echo = FALSE, results='asis'}
library(formattable)
formattable(df, list(
age = color_tile("white", "orange"),
grade = formatter("span",
style = x ~ ifelse(x == "A", style(color = "green", font.weight = "bold"), NA)),
final_score = formatter("span",
style = x ~ style(color = ifelse(rank(-x) <= 3, "green", "gray")),
x ~ sprintf("%.2f (rank: %02d)", x, rank(-x))),
registered = formatter("span",
style = x ~ style(color = ifelse(x, "green", "red")),
x ~ icontext(ifelse(x, "ok", "remove"), ifelse(x, "Yes", "No")))
))
```
Upvotes: 1
Views: 1068
Reputation: 100
A solution that does not exactly get at the issue, but, allows you to print markdown tables with color would be to export the formattable table as an image so that your table on the actual markdown is a png file type. Below is a function that you can then apply to your tables.
export_formattable <- function(f, file, width = "100%", height = NULL,
background = "white", delay = 0.2,
width_svg=720, height_svg=740){
w <- as.htmlwidget(f, width = width_svg, height = height_svg)
path <- html_print(w, background = background, viewer = NULL)
url <- paste0("file:///", gsub("\\\\", "/", normalizePath(path)))
webshot(url,
file = file,
selector = ".formattable_widget",
delay = delay)
}
Applying this to your table:
formattble_table <- formattable(df, list(
age = color_tile("white", "orange"),
grade = formatter("span",
style = x ~ ifelse(x == "A", style(color = "green", font.weight = "bold"), NA)),
final_score = formatter("span",
style = x ~ style(color = ifelse(rank(-x) <= 3, "green", "gray")),
x ~ sprintf("%.2f (rank: %02d)", x, rank(-x))),
registered = formatter("span",
style = x ~ style(color = ifelse(x, "green", "red")),
x ~ icontext(ifelse(x, "ok", "remove"), ifelse(x, "Yes", "No")))
))
export_formattable(formattble_table, "formattble_table.png")
This post was helpful in finding a solution to export tables: Command for exporting/saving table made with Formattable package in R.
Cheers to faaabyan - https://stackoverflow.com/users/4065921/faaabyan for finding this export solution.
Upvotes: 1