Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

书籍中交互网页图形的截图操作 #25

Closed
XiangyunHuang opened this issue Nov 26, 2022 · 1 comment
Closed

书籍中交互网页图形的截图操作 #25

XiangyunHuang opened this issue Nov 26, 2022 · 1 comment

Comments

@XiangyunHuang
Copy link
Owner

XiangyunHuang commented Nov 26, 2022

我的诉求

在可重复、代码优先的要求下,希望 MacOS 、基于 Rocker 的 Ubuntu 容器和 Github Action (Ubuntu 环境)都可以顺利编译出四种书籍格式。可见,书籍的测试工作是非常复杂的。下面以交互图形的截图操作为例

现状梳理

在 MacOS 系统中,如下代码可以非常轻松的截图

---
title: "Untitled"
output: 
  bookdown::epub_book: 
    toc: yes
    epub_version: "epub3"
date: "2022-11-26"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## 交互图形


```{r}
#| label: echarts4r
#| fig-cap: "交互图形"
#| eval: !expr knitr::is_html_output()

library(echarts4r)
mtcars |>
  e_charts(qsec) |>
  e_line(mpg)
```

但是,在 Docker 容器(来自 Rocker 项目的 Ubuntu 系统)报出如下错误,且无法按照指示安装软件,(系统已安装 chromium-browser,不知为何还要 snap install chromium)

Quitting from lines 18-26 (Untitled.Rmd) 
Error in launch_chrome(path, args) : Failed to start chrome. Error: 
Command '/usr/bin/chromium-browser' requires the chromium snap to be installed.
Please install it with:

snap install chromium
Calls: <Anonymous> ... initialize -> <Anonymous> -> initialize -> launch_chrome
Execution halted

但是,在 Github Action 中输出的 EPUB 格式书籍中的截图是黑屏的。

截屏2022-11-26 10 00 36

截屏2022-11-26 10 01 08

目前已知

总结一下,在 Ubuntu 系统上:

webshot 需要系统安装 phantomjs

apt-get install phantomjs

webshot2 需要系统安装 chromium

snap install chromium

若想在所有系统环境中都运行,尝试 webshot 而不是 webshot2,发现 R Markdown Cookbook 也是使用 webshot

https://github.com/rstudio/rmarkdown-cookbook/blob/658f0d9fd463078ac58cc3c860e1aaaab9950185/index.Rmd#L26-L26

在 Rocker 中使用 webshot 截图,报出新的问题

Error in (function (url = NULL, file = "webshot.png", vwidth = 992, vheight = 744,  : 
  webshot.js returned failure value: -6
Calls: <Anonymous> ... html_screenshot -> in_dir -> do.call -> <Anonymous>
Execution halted
@XiangyunHuang
Copy link
Owner Author

所以,另一个绕过去的办法是,一半使用代码生成,一半使用截图。就是输出是 HTML 网页时,代码生成,其它输出格式,采用截图。 借助调整代码块选项和 Quarto 的条件内容展示来实现。knitr 相关问题, https://d.cosx.org/d/423716

knitr::is_html_output(excludes = 'epub')

默认情况下 epub 会被视作 HTML 输出。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant