Search code examples
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:

  • R 4.3.1
  • Pandoc 3.1.1 (the version that comes with RStudio)
  • webshot 0.5.5
  • webshot2 0.1.0
  • rmarkdown 2.23
  • knitr 1.43
  • stringr 1.5.0

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:

    • You can change the directory, where R sessions store their temporary data by adding the following line to the file .Renviron in your home (create it, if it does not exist) (see this answer):

      TMPDIR = "PATH_TO_YOUR_TEMPDIR"
      

      Note that the path must be inside your home and cannot be a hidden directory or inside a hidden directory in order for the snap to be able to access it.

    • You can also change the temporary directory only in the R session in which the Rmd file is rendered by using the package unixtools (see this answer). Install the package with

      remotes::install_github("s-u/unixtools")
      

      Afterwards, add the following to a chunk in your Rmd file:

      library(unixtools)
      tmp_dir <- "tmp/"
      dir.create(tmp_dir, showWarnings = FALSE)
      set.tempdir(normalizePath(tmp_dir))
      

      This will create a subfolder tmp/ in the same directory where the Rmd-file is stored and use it for temporary data. (Of course, you can use a different folder if you want, again with the restrictions mentioned above.)

    This allowed me 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...