rr-markdownknitrstringr

str_view() in pdf slides is no longer working


In the past, the html output from str_view() could be properly rendered in pdf slides, given that the webshot package was installed (see also this answer). However, this no longer works.

The following Rmd file should produce a title slide and a slide showing a regex example:

---
title: "Regex"
output: beamer_presentation
---

```{r setup, include=FALSE}
library(stringr)
```

## `str_view`

```{r regex1}
str_view(c("abc", "bcd", "cde"), "bc", html = TRUE)
```

If only the webshot package, but not webshot2, is installed, I get the following error on knitting:

Error:
! webshot.js returned failure value: 1
Backtrace:
  1. rmarkdown::render(...)
  2. knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
  3. knitr:::process_file(text, output)
  7. knitr:::process_group.block(group)
  8. knitr:::call_block(x)
     ...
 19. knitr (local) value_fun(ev$value, ev$visible)
 20. knitr (local) fun(x, options = options)
 22. knitr::knit_print(x, ...)
 23. knitr:::html_screenshot(x)
 26. webshot (local) `<fn>`(...)
Execution halted

Calling webshot::is_phantomjs_installed() returns TRUE, so PhantomJS is installed.

If I install webshot2, knitting works without error, but the html-output of str_view() is not shown:

enter image description here

The expected output that could be created in the past would look something like this (recreated with an image manipulation program):

enter image description here

What is the problem here? And is there still a way to get this to work?

(I am aware that I could use the text output from str_view() without html = TRUE, but I would prefer the html output.)

I am running this on Ubuntu 22.04.2 using the following versions:


Solution

  • I finally figured out the reason for this problem and it has to do with the fact that webshot2 depends on a chromium based browser to take a screenshot of the html widget that is created by str_view().

    When the Rmd-file is rendered, the html-widget is stored in an html-file in the temporary folder used by the R-session, which is inside /tmp. webshot2 then uses a chromium based browser (brave in my case) to open that file and take the screenshot. However, in Ubuntu brave, chromium and chrome are installed as snaps and snaps are not allowed to access /tmp and therefore, taking the screenshot fails.

    I found two solutions to this:

    This allowed my to successfully build my beamer presentation, but unfortunately, the result is not satisfactory: the screenshot of the widget is much too small. But this is another problem...