0

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 Answers1

0

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.

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • This didn't seem to work for me sadly. I ended up submitting an issue on GitHub but I really appreciate your help! I learned something new today (the render = option)! – icurtis Aug 20 '23 at 18:54
  • @icurtis, if you don't get fast action from your issue, you should expand your question or ask a new one to explain what went wrong. Someone might be able to fix it. – user2554330 Aug 22 '23 at 11:31