Color printing R mark down html

I wonder if there is a way to get the web browser to print r to mark html in colors. This can be achieved by editing the boot file, but I wonder if this can be done in the markdown file.

Playable example:

test.Rmd

---
title: "Habits"
output:
  html_document
---

```{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)

format_table(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")))
))

```

The HTML output table looks like this in a browser: HTML output

When I try to print it, it will look like this: enter image description here

+4
source share
1 answer

This is the browser option mentioned in [ https://stackoverflow.com/users/4497050/alistaire] . The best way to print in color is to save the HTML page from shinyapp in PDF format and then print from PDF.

enter image description here

0
source

Source: https://habr.com/ru/post/1652260/


All Articles