Indrajeet Patil
Indrajeet Patil

Reputation: 4879

getting rmarkdown to print improved tibble printing

The pillar package offers a number of options to format tibble printing.

https://pillar.r-lib.org/reference/pillar-package.html#package-options

For example, this is what I see on my Windows machine, which supports these options:

enter image description here

But when I set the same options for rmarkdown document, I don't see any difference in the printed output.

enter image description here

Is there a way to successfully get this to work or this is not supported in rmarkdown itself?

Upvotes: 5

Views: 3428

Answers (2)

W. Joel Schneider
W. Joel Schneider

Reputation: 1806

In the vignette for the tibble package, there is a possible solution. In your setup chunk of your .Rmd file, put:

knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(tibble)
set.seed(1014)

options(crayon.enabled = TRUE)
options(pillar.bold = TRUE, pillar.subtle_num = TRUE)

knitr::opts_chunk$set(collapse = TRUE, comment = pillar::style_subtle("#>"))

colourise_chunk <- function(type) {
  function(x, options) {
    lines <- x
    if (type != "output") {
      lines <- crayon::red(lines)
    }
    paste0(
      '<div class="sourceCode"><pre class="sourceCode"><code class="sourceCode">',
      paste0(
        fansi::sgr_to_html(htmltools::htmlEscape(lines)),
        collapse = "\n"
      ),
      "</code></pre></div>"
    )
  }
}

knitr::knit_hooks$set(
  output = colourise_chunk("output"),
  message = colourise_chunk("message"),
  warning = colourise_chunk("warning"),
  error = colourise_chunk("error")
)

In a new chunk:


broom::tidy(stats::chisq.test(table(ggplot2::msleep$vore)))

My HTML output:

tibble output

Upvotes: 3

Ram Pari
Ram Pari

Reputation: 76

I'd say that from my markdown experience that Pillow will not work as markdown uses pandoc.

As alternatives, I'd recommend using the kable package for a similar look using it's themes options. A handy tutorial with using a relatively similar theme ->

https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html

Another cool option of making really cool markdown tables would be to use formattable, which has a lot of in-depth options for formatting

formattable example

And a couple of handy tutorials for that ->

https://www.littlemissdata.com/blog/prettytables

https://www.littlemissdata.com/blog/pretty-r-tables-in-github

Hopefully, this helps you out.

Upvotes: 3

Related Questions