Does devtools::build_readme() use {webshot2} (if installed)?

67 Views Asked by At

I am currently building a package for a rather specific audience. The package itself is coming along nicely but I am having trouble getting my README.Rmd to convert to a README.md (using devtools::build_readme(). Some functions in my package output ggplot objects and these render to GitHub README fine. However, other functions output flextable objects, which are inherently HTML and which GitHub does not like to display.

I was hoping that build_readme() would use {webshot2} to take a snapshot of my tables for use in non-HTML formats, such as github_document but this doesn't seem to be the case. I do have {webshot2} installed and Google Chrome is my primary browser so I have that. My README displays fine on my documentation page (since it's an HTML page).

Is there something I'm missing with build_readme() or {webshot2}? Or is this really something that isn't possible right now and I should direct all users to my documentation's copy of the README rather than the GitHub version?

Thank you in advance for your help.

1

There are 1 best solutions below

2
On

I don't see a simple way to do this, but flextable supports saving a table to a PNG file, and you could do that and then include the file using knitr::include_graphics().

For example, your first example could be modified to look like this:


```{r include=FALSE}
flextable_to_image <- function(x, ...) {
  if (inherits(x, "flextable")) {
    filename <- tempfile(fileext = ".png")
    flextable::save_as_image(x, filename)
    knitr::include_graphics(filename)
  } else
    knitr::normal_print(x)
}
```

```{r num-sum, message = FALSE, render = flextable_to_image}
library(gvsu215)

num_sum(mtcars, ~wt, na_rm = TRUE)
```

It's probably best to only print flextable objects in a chunk that uses render = flextable_to_image because the else clause is overly simplified. It might also be a good idea to unlink() the filename after conversion.