• Stars
    star
    223
  • Rank 172,073 (Top 4 %)
  • Language
    JavaScript
  • Created over 9 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Take screenshots of web pages from R

webshot

R-CMD-check

Webshot makes it easy to take screenshots of web pages from R. It can also:

  • Run Shiny applications locally and take screenshots of the application.
  • Render R Markdown documents and take screenshots of the document. Webshot can handle both static Rmd documents and interactive ones (those with runtime: shiny).

See the introduction article for examples in addition to the ones below.

Installation

Webshot can be installed from CRAN. Webshot also requires the external program PhantomJS. You may either download PhantomJS from its website, or use the function webshot::install_phantomjs() to install it automatically.

install.packages("webshot")
webshot::install_phantomjs()

Usage

By default, webshot will use a 992x744 pixel viewport (a virtual browser window) and take a screenshot of the entire page, even the portion outside the viewport.

library(webshot)
webshot("https://www.r-project.org/", "r.png")
webshot("https://www.r-project.org/", "r.pdf") # Can also output to PDF

You can clip it to just the viewport region:

webshot("https://www.r-project.org/", "r-viewport.png", cliprect = "viewport")

You can also get screenshots of a portion of a web page using CSS selectors. If there are multiple matches for the CSS selector, it will use the first match.

webshot("https://www.r-project.org/", "r-sidebar.png", selector = ".sidebar")

If you supply multiple CSS selectors, it will take a screenshot containing all of the selected items.

webshot("https://www.r-project.org/", "r-selectors.png",
        selector = c("#getting-started", "#news"))

The clipping rectangle can be expanded to capture some area outside the selected items:

webshot("https://www.r-project.org/", "r-expand.png",
        selector = "#getting-started",
        expand = c(40, 20, 40, 20))

You can take higher-resolution screenshots with the zoom option. This isnโ€™t exactly the same as taking a screenshot with a HiDPI (โ€œRetinaโ€) device: it is like increasing the zoom to 200% in a desktop browser and doubling the height and width of the browser window. This differs from using a HiDPI device because some web pages load different, higher-resolution images when they know they will be displayed on a HiDPI device (but using zoom will not report that there is a HiDPI device).

webshot("https://www.r-project.org/", "r-sidebar-zoom.png",
        selector = ".sidebar", zoom = 2)

Vectorization

All parameters of function webshot. That means that multiple screenshots can be taken with a single command. When taking a lot of screenshots, vectorization can divide by 5 the execution time.

# Take a screenshot of different sites
webshot(c("https://www.r-project.org/", "https://github.com/wch/webshot"),
        file = c("r.png", "webshot.png"))

# Save screenshots of the same site in different formats
webshot("https://www.r-project.org/", file = c("r.png", "r.pdf"))

# Take screenshots of different sections of the same site.
# Note that unlike arguments "url" and "file", a list is required to specify
# multiple selectors. This is also the case for arguments "cliprect" and
# "expand"
webshot("http://rstudio.github.io/leaflet/",
        file = c("leaflet_features.png", "leaflet_install.png"),
        selector = list("#features", "#installation"))

Screenshots of Shiny applications

The appshot() function will run a Shiny app locally in a separate R process, and take a screenshot of it. After taking the screenshot, it will kill the R process that is running the Shiny app.

# Get the directory of one of the Shiny examples
appdir <- system.file("examples", "01_hello", package="shiny")
appshot(appdir, "01_hello.png")

Screenshots of R Markdown documents

The rmdshot() function takes screenshots of R Markdown documents. For static R Markdown documents, it renders them to HTML in a temporary directory (using rmarkdown::render())and then takes a screenshot.

For dynamic R Markdown documents, it runs them using rmarkdown::run() in a separate R process and then takes a screenshot. After taking the screenshot, it will kill the R process that is running the document.

rmdshot("document.rmd", "document.png")

Manipulating images

If you have GraphicsMagick (recommended) or ImageMagick installed, you can pass the result to resize() to resize the image after taking the screenshot. This can take any valid ImageMagick geometry specifictaion, like "75%", or "400x" (for an image 400 pixels wide). However, you may get different (and often better) results by using the zoom option: the fonts and graphical elements will render more sharply. However, compared to simply resizing, zooming out may result in slightly different positioning of text and layout elements.

You can also call shrink(), which runs OptiPNG to shrink the PNG file losslessly.

webshot("https://www.r-project.org/", "r-small-resized.png") %>%
  resize("75%") %>%
  shrink()

# Using zoom instead of resize()
webshot("https://www.r-project.org/", "r-small-zoomed.png", zoom = 0.75) %>%
  shrink()

# Can specify pixel dimensions for resize()
webshot("https://www.r-project.org/", "r-small.png") %>%
  resize("400x") %>%
  shrink()

To illustrate the difference between resize() and zoom, here is an image with resize("50%"):

And here is one with zoom = 0.5. If you look closely, youโ€™ll see that the text and graphics are sharper. Youโ€™ll also see that the bullet points and text are positioned slightly differently:

More Repositories

1

r-source

Read-only mirror of R source code from https://svn.r-project.org/R/, updated hourly. See the build instructions on the wiki page.
R
1,053
star
2

extrafont

Tools for using fonts in R graphics
R
304
star
3

latexsheet

LaTeX cheat sheet
TeX
155
star
4

harbor

An R package for controlling docker containers on local and remote hosts
R
100
star
5

r-debug

Dockerfile with several builds of R for memory/thread debugging
Dockerfile
85
star
6

chatstream

Example Shiny for Python app which talks to the OpenAI API
Python
71
star
7

SendText

A Sublime Text plugin to send text to a terminal or other program
JavaScript
70
star
8

shiny-jsdemo

Demo of Shiny integration with third-party Javascript libraries
R
54
star
9

staticimports

Statically import R functions into packages and projects
R
54
star
10

shiny-wordle

Wordle in Shiny
R
48
star
11

downloader

R package for downloading files with https
R
32
star
12

shiny-gridster

Add-on package for using the Gridster library with Shiny
R
25
star
13

fontcm

Computer Modern font package for R
R
22
star
14

testapp

Shiny apps for testing
R
20
star
15

retirement-simulation-dashboard

Retirement Simulation Dashboard, built with Quarto and Shiny for Python
16
star
16

bisectr

Bisect tools for R
R
16
star
17

vtest

Visual test system for R packages
R
13
star
18

Rttf2pt1

R package for ttf2pt1- to be used with extrafont package
C
12
star
19

2020-01-later

Materials for talk about later and async at 2020 RStudio Conf
R
11
star
20

shinylive-example

Example of Shinylive application deployed on GitHub Pages
Python
11
star
21

qstack

Fast basic data structures for R
R
11
star
22

gcookbook

Data sets for R Graphics Cookbook
R
10
star
23

rspeed

R speed tests
C
10
star
24

vagrant-ec2-r

Vagrant config for Ubuntu VM with R, under EC2
Ruby
10
star
25

movies

Movie explorer Shiny app
R
10
star
26

s3ops

S3 dispatch on operators
R
9
star
27

extrafontdb

Package that holds the database for the fonts package for R
R
9
star
28

dockr

Docker files related to R
R
9
star
29

shinycomponent

Example Shiny component
TypeScript
7
star
30

shiny-quarto

Quarto extension for building Shiny apps
5
star
31

string_builder

Performance tests of various ways of building strings in R
R
5
star
32

shiny-reactlog-examples

Example data from Shiny reactlog
R
5
star
33

shiny_demo

Shiny app for looking at crandb data
R
4
star
34

send-custom-message

Custom server-to-client message example app for R shiny
R
4
star
35

shiny-handsontable

Handsontable bindings for Shiny
JavaScript
3
star
36

image-output

Image output example for R Shiny
R
3
star
37

shiny-disconnect

Shinyapps.io disconnection test app
R
2
star
38

r-devel-nix

R-devel for Nix
Nix
2
star
39

shiny_express_doc

Introduction to Shiny Express
Lua
2
star
40

badadd

Example of operator overloading problem in R
2
star
41

flang-webr

Nix flake for flang-webr
Nix
2
star
42

custom-input-binding

Custom input binding demo app for R Shiny
JavaScript
2
star
43

shiny-slickgrid

Package for using Slickgrid with Shiny
JavaScript
2
star
44

python-wasm-demo

Python wasm demo
Python
2
star
45

s3methodtest

Package for showing S3 method warning
R
1
star
46

envtest

Environment test package for R
R
1
star
47

gtable-vtest

Visual test results for gtable
1
star
48

rcpptest

Experimenting with rcpp
C++
1
star
49

pyshiny-bootswatch-example

Example Shiny for Python application using a Bootswatch theme
Python
1
star
50

2020-vote-count

Plots of 2020 vote count
HTML
1
star
51

ggplot2-vtest

Visual test results for ggplot2
1
star
52

ranked-choice-vote

Ranked-choice vote calculation script
R
1
star
53

namespace-cpp-test

Attempt to create a C++ namespace clash
C++
1
star
54

pkgClosure

Tests of closures that span packages in R
R
1
star
55

movie_import

Scripts for importing and converting movie files from digital cameras
Python
1
star
56

animated-terminal

Animated Terminal JS component
JavaScript
1
star
57

focuswatch

Program for monitoring which application has focus on macOS
Objective-C
1
star
58

examplepackage

An example R package
R
1
star