Giter Site home page Giter Site logo

webshot's Introduction

webshot

Travis-CI Build Status

Webshot makes it easy to take screenshots of web pages from R. It can also run Shiny applications locally and take screenshots of the app.

Installation

It requires an installation of the external program PhantomJS. Please make sure you have PhantomJS version 2 or higher installed. Previous versions may have trouble rendering some fonts. You may either download PhantomJS from its website, or use the function webshot::install_phantomjs() to install it automatically.

Once PhantomJS is installed you can install webshot with:

devtools::install_github("wch/webshot")

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 also change the user agent string, which is often used by sites to determine how to render. E.g. a mobile user string:

webshot("https://www.google.com", "mobile-useragent.png",
        userAgent="Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X)
        AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1")

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")

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).

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

webshot("http://www.google.com/", "google-small.png") %>%
 resize("75%") %>%
 shrink()

webshot("http://www.google.com/", "google-small.png") %>%
 resize("400x") %>%
 shrink()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.