How can I screenshot a website using R?
Asked Answered
L

2

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?

Lynnette answered 2/4, 2015 at 17:20 Comment(0)
I
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
Integument answered 2/4, 2015 at 17:27 Comment(2)
You can use browseURL(tf) to view screenshot on LinuxGlenine
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."Poisson
P
11

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")
Pneumothorax answered 15/3, 2016 at 15:47 Comment(2)
I tried this and got error with curl unable to authenticateMartijn
RSelenium seems to be defunct.Showbread

© 2022 - 2024 — McMap. All rights reserved.