14

So I'm not 100% sure this is possible, but I found a good solution in Ruby and in python, so I was wondering if something similar might work in R.

Basically, given a URL, I want to render that URL, take a screenshot of the rendering as a .png, and save the screenshot to a specified folder. I'd like to do all of this on a headless linux server.

Is my best solution here going to be running system calls to a tool like CutyCapt, or does there exist an R-based toolset that will help me solve this problem?

Community
  • 1
  • 1
Zach
  • 29,791
  • 35
  • 142
  • 201

2 Answers2

21

You can take screenshots using Selenium:

library(RSelenium)
rD <- rsDriver(browser = "phantomjs")
remDr <- rD[['client']]
remDr$navigate("http://www.r-project.org")
remDr$screenshot(file = tf <- tempfile(fileext = ".png"))
shell.exec(tf) # on windows
remDr$close()
rD$server$stop()

In earlier versions, you were able to do:

library(RSelenium)
startServer()
remDr <- remoteDriver$new()
remDr$open()
remDr$navigate("http://www.r-project.org")
remDr$screenshot(file = tf <- tempfile(fileext = ".png"))
shell.exec(tf) # on windows
lukeA
  • 53,097
  • 5
  • 97
  • 100
  • 1
    You can use `browseURL(tf)` to view screenshot on Linux – jsta Oct 29 '15 at 00:16
  • 2
    The function `startServer()` is defunct in version 1.7.1. They say "The recommended way to run a selenium server is via Docker. Alternatively see the RSelenium::rsDriver function." – drmariod Mar 15 '17 at 14:48
10

I haven't tested it, but this open source project seems to do exactly that: https://github.com/wch/webshot

It is a easy as:

library(webshot)
webshot("https://www.r-project.org/", "r.png")
nassimhddd
  • 8,340
  • 1
  • 29
  • 44