GithubHelp home page GithubHelp logo

webshot's Introduction

webshot

R-CMD-check

Note: webshot uses PhantomJS, which is a headless browser that stopped development in 2018. Please use webshot2, which uses Chromium-based browsers.

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:

webshot's People

Contributors

coatless avatar francoisguillem avatar hoxo-m avatar jacob-ogre avatar javierluraschi avatar jimhester avatar schloerke avatar timelyportfolio avatar warnes avatar wch avatar yasirs avatar yihui avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

webshot's Issues

Margin/empty page when generating pdf

When using webshot to generate a pdf from a plotly (using latest CRAN 3.6.0) diagram I get a margin right and at the bottom and a second empty page. I don't get the margin nor the empty page when creating a png. A pdf is preferred since it's vectorised. This has direct implications when using knitr reports. The margin makes it difficult to size the diagram within the report. Png is no alternative here because of worse quality.

I wasn't able to suppress the margin with current dev. If there are possibilities, I'd glad to know. If there's none it would help a lot if this could be implemented.

library(plotly)
library(webshot)
data <- data.frame(x = c(1,2,3,4), y = c(5,4,5,7), 
                   date = c("20160101", "20160102", "20160103", "20160104"))

plot <- plot_ly(data = data, 
                x = x,
                y = y,
                color = as.character(date),
                type = "bar") %>%
  layout(title = "graphicTitle",
         hovermode = "closest")

htmlwidgets::saveWidget(as.widget(plot), "test.html")
webshot::webshot("test.html", "test.pdf") # ugly margin at bottom and right
webshot::webshot("test.html", "test.png") # no margin as expected, but poor quality since not vectorized

webshot: export web element type id (not class)

I need to export to png files some web elements from a specific site with webshot R library.

First, installing and loading libraries:

install.packages("webshot",dependencies = TRUE)
library(webshot)
webshot::install_phantomjs()

I'm testing webshot() with www.google.es URL. It works well:

webshot("https://www.google.es/","google.png", selector="#hplogo")

But If I want to export to PNG image the search engine element (id web element), I write the following code:

webshot("https://www.google.es/","google.png", selector=".tsf-p")

What is wrong?

_PHANTOM ERROR: CasperError: No element matching selector found: .tsf-p
TRACE:
-> phantomjs://platform/casper.js: 1066 (in function getElementBounds)
-> phantomjs://code/webshot.js: 137
-> undefined: 0 (in function map)
-> phantomjs://code/webshot.js: 136 (in function findClipRect)
-> phantomjs://code/webshot.js: 85
-> phantomjs://platform/casper.js: 2188 (in function check)
Error in webshot("https://www.google.es/", "google.png", selector = ".tsf-p") :
webshot.js returned failure value: 1
In addition: Warning message:

running command '"C:\Users\Mario Martínez\AppData\Roaming/PhantomJS/phantomjs.exe" "C:/User......"

Curl for application

Rather than waiting the standard 0.5, should we do something like the code instead?

while(!RCurl::url.exists("urltoshinyapp")) {
  Sys.sleep(0.1)
}

I would eliminate the race case, but would introduce a dependency of some kind.

Add user-agent option

This would be useful for web pages that are served different content for different browser. (See #39.)

Better knitr integration

It would be useful to be able to put calls to webshot in a knitr document. Also, perhaps have an option so that if the image already exists, don't try to re-capture it.

resize finds wrong convert.exe on Windows

On windows, Sys.which("convert") returns the path to the windows program of the same name. I tried switching the order of environment variables etc, but to no avail. Anyway, it would be great to have an option for resize to specify the path to the correct convert program (like in the animation package).

Webshot R-Package not working at all

I'm trying to use webshot for R, but even the simplest examples won't work. For instance,

URL <- "http://rstudio.github.io/leaflet/"
webshot(URL, delay = 0.5)

doesn't produce any output. This example is in the package vignette. None of the examples there works, actually.

I have phantomjs installed under AppData/Roaming, as it should. What is happening? It seems that it works for everyone else and my problem is undocumented : ( So I don't have a clue of what is going on.

Here is my R session info:

R version 3.3.3 (2017-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

loaded via a namespace (and not attached):
[1] magrittr_1.5      rsconnect_0.4.2.2 tools_3.3.3      
[4] jsonlite_1.2      webshot_0.4.0

If you need more info, let me know.

Thanks in advance.

.click to work

Hi all, I am fairly new to this and so any help would be appreciated.

I am trying to write a simple code to understand how this all works and then I will progress it further.
For the learning process I am using a site called: www.trademe.co.nz and I am wanting to take a screenshot of what the search function returns. I will then progress it to enter a username and password for a different project (internal website so I can't share it).

My code so far is below but whatever I try to do, I can't get it to click down on the search button (ignore code I have there for it now, it doesn't work) next to where the text is entered. Can someone please help me out? I have the devel version of webshot

#Configuration
library(webshot)

webshot("https://www.trademe.co.nz/",
eval = "casper.then(function() {

 this.sendKeys('input[name =searchString]', 'waitati');

    //this.click('#field field-right');
    // Wait 500ms
    this.wait(500);
    });"

)

webshot for untrusted https websites

I have failed on trying to take a webshot of an https site with an untrusted certificate. Here is a MWE:

webshot("https://untrusted-root.badssl.com/")
# Could not load  https://untrusted-root.badssl.com/
# Error in webshot("https://untrusted-root.badssl.com/")

Is this behaviour expected? Is there any alternative for taking screenshots of such sites?

I am using:

> version
               _                           
platform       x86_64-apple-darwin13.4.0   
arch           x86_64                      
os             darwin13.4.0                
system         x86_64, darwin13.4.0        
status                                     
major          3                           
minor          2.3                         
year           2015                        
month          12                          
day            10                          
svn rev        69752                       
language       R                           
version.string R version 3.2.3 (2015-12-10)
nickname       Wooden Christmas-Tree       
> packageVersion("webshot")
[1] ‘0.4.1.9000’

htmlwidgets element not showing in webshot

hi there
I have been using the web shot as an final step of my automated daily report for a while.
it's been working for my previous web shot of markdown made of high charter and other screen shot.

but recently I have try out the flexdashboard in R for better visual effect.
then when I try webshot, it turns out like this.
all the element made of HTML widgets is not showing.

snipaste_2017-12-22_11-22-13

is there any way of fixing this ? that would be really helpful.

look a little harder for phantom in Windows

Thanks so much for this. I discovered when working on bower in htmlwidgets that many Windows users might not have PATH set properly for node modules. In these lines, the search for bower just goes one level further. The same could easily be used for phantom. Happy to work into a pull if interested.

webshot fails to take screenshots of local .html under Windows

I try to take screenshots of local .html files. The below code works fine on Ubuntu 16.04 64-bit, R version 3.0.0, but fails on the same machine using the same R version under Windows 10.

## sample widget
library(mapview)
m <- mapview(breweries91)

## save to html
htmlwidgets::saveWidget(widget = m@map, file = "~/map.html")

## save html to png 
webshot::webshot(url = "C:/Users/fdetsch/Documents/map.html", 
                 file = "C:/Users/fdetsch/Documents/map.png")

More precisely, the created .html looks just fine when opened e.g. in Firefox and the code does not throw any errors, but the resulting image is just black. On Ubuntu, by contrast, the created .png file looks just as expected. Here is my session info.

R version 3.3.0 (2016-05-03)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252    LC_MONETARY=German_Germany.1252
[4] LC_NUMERIC=C                    LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] mapview_1.0.29     leaflet_1.0.1.9003

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.5         raster_2.5-2        magrittr_1.5        gdalUtils_2.0.1.7   munsell_0.4.3      
 [6] colorspace_1.2-6    viridisLite_0.1.3   lattice_0.20-33     foreach_1.4.3       plyr_1.8.3         
[11] tools_3.3.0         webshot_0.3         rgdal_1.1-10        grid_3.3.0          R.oo_1.20.0        
[16] png_0.1-7           latticeExtra_0.6-28 htmltools_0.3.5     iterators_1.0.8     yaml_2.1.13        
[21] digest_0.6.9        RColorBrewer_1.1-2  htmlwidgets_0.6     R.utils_2.3.0       codetools_0.2-14   
[26] sp_1.2-3            scales_0.4.0        R.methodsS3_1.7.1   stats4_3.3.0        jsonlite_0.9.20    
[31] satellite_0.2.0 

Link embedded in the image?

Is it possible to embed the link in the image so that you can click on the image to browse the link?
I imagine that this is possible in HTML. It is.

Thanks for this great package!

use website 'inside' a shiny app?

Is it feasible to run webshot from a shiny app? Ideally, I'd like to press a button to take a screenshot of the current page in a shiny app, store the image, and then put a reference to the screenshot into an knitr document in the app or into Rstudio. I tried a few things it but it locked up the session but I want the app to continue working after the screenshot is taken. I think I could get this (mostly) working assuming webshot can be called from a shiny app.

Alternatively, could an Rstudio addin be used to create a screen-shot of the active app and put the result into the Rstudio Viewer? As far as I can tell, you can only run one addin at a time unfortunately.

screenshot for googlesheet

Hi
It is a great package help a lot, great job, But while taking the snapshot of googlesheet it stuck on gmail login page and webshot captured a login screen .... please help

webshot does not preserve addWebGLHeatmap layer from leaflet.extras

Hello, I noticed on Stack Overflow that there was an unresolved question about this dated to August 2017, and that in the comments someone suggested to open an issue here, but I did not see one posted.

My issue is this: when I execute the following code in RStudio, I produce this image:

map = leaflet(df) %>%
      addWebGLHeatmap(lng=~df$longitude, lat=~df$latitude, opacity= 1 , intensity = .5 ,size = 1000, gradientTexture='deep-sea', data=df)%>%
      addTiles(urlTemplate = map, attribution=attr)

screen shot 2018-07-12 at 4 31 22 pm
However, when I try to save it using webshot with this code:

saveWidget(map, "tmp.html", selfcontained = F)
webshot("temp.html", file = "new_orleans.png", cliprect = "viewport")

It instead produces:
new_orleans_la_map

Is there a way to resolve this? Thanks!

Add animation options

It would be useful to allow animation. This will also require the ability to take multiple screenshots.

How to enable cookies

Hi,
I encountered a problem: I new to login into facebook, because the screenshots I'd like to take are not possible without being loged in. The Login-Process documented below works, but there is an error that cookies should be allowed.

How to allow cookies?

webshot("https://www.facebook.com/FacebookDeutschland/", file = "Test.png", eval = "casper.then(function() {
this.sendKeys('#login_form #email', 'XXX@XXX');
this.sendKeys('#login_form #pass', 'XXX');
this.click('#u_0_2');
this.wait(1000);
});"
)

Thank you very much for this package!

vwidth

Really nice package! 👌

I am getting unexpected results with vwidth, but maybe it's a PhantomJS issue or something I got wrong.

Works well: the resulting image width is 550

webshot::webshot("https://cran.r-project.org/web/packages/webshot/index.html", 
  vwidth = 550)

img <- magick::image_read("webshot.png")
magick::image_info(img)
#>   format width height colorspace matte filesize density
#> 1    PNG   550    989       sRGB  TRUE    60770   38x38

Doesn't work as expected

webshot::webshot("https://itsalocke.com/blog/using-travis-make-sure-you-use-a-github-pat/", 
  vwidth = 550)

img <- magick::image_read("webshot.png")
magick::image_info(img)
#>   format width height colorspace matte filesize density
#> 1    PNG   804   2520       sRGB  TRUE   155814   38x38
Session info
devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.4.4 (2018-03-15)
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  English_United States.1252  
#>  tz       Europe/Paris                
#>  date     2018-03-22
#> Packages -----------------------------------------------------------------
#>  package   * version    date       source                            
#>  backports   1.1.2      2017-12-13 CRAN (R 3.4.3)                    
#>  base      * 3.4.4      2018-03-15 local                             
#>  compiler    3.4.4      2018-03-15 local                             
#>  curl        3.1        2017-12-12 CRAN (R 3.4.3)                    
#>  datasets  * 3.4.4      2018-03-15 local                             
#>  devtools    1.13.5     2018-02-18 CRAN (R 3.4.3)                    
#>  digest      0.6.15     2018-01-28 CRAN (R 3.4.3)                    
#>  evaluate    0.10.1     2017-06-24 CRAN (R 3.4.3)                    
#>  formatR     1.5        2017-04-25 CRAN (R 3.4.0)                    
#>  graphics  * 3.4.4      2018-03-15 local                             
#>  grDevices * 3.4.4      2018-03-15 local                             
#>  htmltools   0.3.6      2017-04-28 CRAN (R 3.4.0)                    
#>  httr        1.3.1      2017-08-20 CRAN (R 3.4.1)                    
#>  jsonlite    1.5        2017-06-01 CRAN (R 3.4.0)                    
#>  knitr       1.20       2018-02-20 CRAN (R 3.4.4)                    
#>  magick      1.8        2018-03-19 CRAN (R 3.4.3)                    
#>  magrittr    1.5        2014-11-22 CRAN (R 3.4.0)                    
#>  memoise     1.1.0      2017-04-21 CRAN (R 3.4.0)                    
#>  methods   * 3.4.4      2018-03-15 local                             
#>  mime        0.5        2016-07-07 CRAN (R 3.4.0)                    
#>  R6          2.2.2      2017-06-17 CRAN (R 3.4.3)                    
#>  Rcpp        0.12.16    2018-03-13 CRAN (R 3.4.4)                    
#>  rmarkdown   1.9        2018-03-01 CRAN (R 3.4.4)                    
#>  rprojroot   1.3-2      2018-01-03 CRAN (R 3.4.3)                    
#>  stats     * 3.4.4      2018-03-15 local                             
#>  stringi     1.1.7      2018-03-12 CRAN (R 3.4.4)                    
#>  stringr     1.3.0.9000 2018-02-28 Github (tidyverse/stringr@097c0c0)
#>  tools       3.4.4      2018-03-15 local                             
#>  utils     * 3.4.4      2018-03-15 local                             
#>  webshot     0.5.0      2017-11-29 CRAN (R 3.4.3)                    
#>  withr       2.1.2      2018-03-15 CRAN (R 3.4.4)                    
#>  xml2        1.2.0.9000 2018-03-08 Github (hadley/xml2@bb6efb2)      
#>  yaml        2.1.18     2018-03-08 CRAN (R 3.4.4)

Not capturing some features on a webpage

The page at this link includes ovals to indicate air quality
webshot("https://www.purpleair.com/map?&zoom=12&lat=39.09864026298141&lng=-108.56749455168722&clustersize=27&orderby=L&latr=0.22700642752714373&lngr=0.4785919189453125", "paMap.png")

The png it produces doesn't include these ovals.
pamap

I'm using webshot version 0.5 with the package process at version 2.0.01 (the newer source version, 3.0.3, won't compile on my mac. I use home-brew and the installation doesn't pickup the location of clang.

failure value: 127

Hi,

This is a great tool! Thanks.

I am having an issue when using webshot function:

> webshot("https://www.r-project.org/", "r.png")
sh: ~/Library/Application Support/PhantomJS/phantomjs: No such file or directory
Error in webshot("https://www.r-project.org/", "r.png") : 
  webshot.js returned failure value: 127

In function phantom_run, if using the full path of phantomjs (/Users/username/.../phantomjs instead of ~/.../phantomjs), the function is working as expected.

KR,
David

> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.3 (El Capitan)

locale:
[1] fr_FR.UTF-8/fr_FR.UTF-8/fr_FR.UTF-8/C/fr_FR.UTF-8/fr_FR.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] webshot_0.3.0.9000

loaded via a namespace (and not attached):
[1] magrittr_1.5 tools_3.2.3 

addWebGLHeatmap layer from leafletextras won't be saved as a png

Please help me with the following.

I am making a heatmap using leaflet and addWebGLHeatmap. This works fine, until I try and save the plot as PNG using webshot. See the code below. It saves the terrain layer but does not show the heating on the saved image, while it does show in the viewer.

library(leaflet)
library(leaflet.extras)
library(mapview)
library(dplyr)

testframe <- data.frame(lat = rnorm(500, mean = 50, sd = 0.1),
lon = rnorm(500, mean = 4, sd = 0.05))

testmap <- leaflet() %>%
addProviderTiles("Stamen.Terrain") %>%
setView(map, lng = 4.0,lat = 50.0 ,zoom = 9)

save <- testmap %>%
addWebGLHeatmap(data = testframe, lng=~lon, lat=~lat, size = 4000, opacity = 0.60, intensity = 0.25) mapshot(save, file = "heatmap.png"))

Hope anyone is able to help.

add option for better render resolution ( code included)

Hi,
To get a better resolution in renderd images I usually change in the webshot.js file (system.file("webshot.js", package="webshot"))

the follwoing line
casper.start(url).viewport(opts.vwidth, opts.vheight);
to
casper.start(url).viewport(opts.vwidth, opts.vheight).zoom(2);

Could that be added as a option instead?

How to use `rmdshot()` on a remote server?

Hi,
I'm trying to capture a dynamic index.Rmd document located on a remote VM. Both appshot() and rmdshot() error out when I run them from R console via SSH or in RStudio Server. This VM has Shiny Server on port 3838 and RStudio Server on port 8787. Not sure which port to use/open on the remote server to make this work.
Any tip?

> rmdshot("bio-profiles/index.Rmd", "test.png", delay=3)
Could not load  http://127.0.0.1:3907/
=> errors out
> rmdshot("bio-profiles/index.Rmd", "test.png", delay=3, port=3838)
=> this captures a 404 blank page
> rmdshot("bio-profiles/index.Rmd", "test.png", delay=3, port=8787)
=> this captures RStudio Server Login page

webshot not working for highcharter

hi there
i am working on my project , but the webshot seems stuck,
no matter how much delay I set, the flex dashboard html page , could only capture the chart it self , but all the only words were not showing at all ( mostly Chinese, not English words.)
is anybody has this problem before ?

specifying PhantomJS path

This probably isn't so much an issue as a question: Is there any way I can specify the path to the PhantomJS executable? There are several versions of PhantomJS installed on my system and I'd like to choose a particular one.

appshot vignette example not working

I can't make appshot work on my windows PC. I just installed phantomjs and have successfully run the first couple of webshot examples on the rstudio homepage. Here's console output from when I try the appshot() vignette example (which I ran after clearing workspace and restarting R):

> appdir <- system.file("examples", "01_hello", package="shiny")
> appshot(appdir, "01_hello.png")
Warning PhantomJS v2.0 not yet released. There will not be any official support for any bugs until stable version is released!
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open file 'C:\Users\Mark\AppData\Local\Temp\RtmpeI2i8Z\pid207878a65777': No such file or directory

However I do have this (empty) file in the same temp directory:
'file207878583cc9'.

devtools::session_info()
Session info ------------------------------------------------------------------------------
setting value
version R version 3.1.2 (2014-10-31)
system x86_64, mingw32
ui RStudio (0.99.441)
language (EN)
collate English_United Kingdom.1252
tz Europe/London

Packages ----------------------------------------------------------------------------------
package * version date source
bitops 1.0-6 2013-08-17 CRAN (R 3.1.2)
devtools 1.8.0 2015-05-09 CRAN (R 3.1.3)
digest 0.6.8 2014-12-31 CRAN (R 3.1.2)
git2r 0.10.1 2015-05-07 CRAN (R 3.1.3)
magrittr 1.5 2014-11-22 CRAN (R 3.1.2)
memoise 0.2.1 2014-04-22 CRAN (R 3.1.2)
RCurl 1.95-4.6 2015-04-24 CRAN (R 3.1.3)
rversions 1.0.0 2015-04-22 CRAN (R 3.1.3)
webshot * 0.2.1 2015-06-08 Github (810f302)
XML 3.98-1.2 2015-05-31 CRAN (R 3.1.3)

helper function for files produced with html_print

I think a very useful helper function with webshot would be something that provides the valid url for a local file created with htmltools::html_print. I doubt this works on non-Windows but something like this might be very helpful especially with htmlwidgets (see ramnathv/htmlwidgets#95).

library(leaflet)
library(htmltools)
library(webshot)

tagList(
  tags$h1( 'Leaflet Map for Screenshot' )
  ,leaflet() %>% addTiles() %>% fitBounds(0, 40, 10, 50)
) %>%
  html_print %>%
  # get forward slash on windows
  normalizePath(.,winslash="/") %>%
  # replace drive:/ with drive:// so C:/ becomes C://
  gsub(x=.,pattern = ":/",replacement="://") %>%
  # appends file:/// to make valid uri
  paste0("file:///",.) %>%
  # screenshot it for lots of good reasons
  webshot( file = "stream_screen.png", delay = 3 )

image

Selector not 100% accurate

hi,

In this mre I get the desired png, but at the very top of the picture the filter and search field are visible, even though they are not part of the selector:

data <- data.frame(a = 1:9, b = c(5, 2, 3, 9, 4, 3, 1, 5, 3))
table <- datatable(data)
htmlwidgets::saveWidget(widget = table, file = "table.html")
webshot("table.html", file = "table.png", selector = "#DataTables_Table_0", expand = 10, zoom = 5)

Screenshot:
image

phantomjs

Vectorize webshot

Hello,

I want to convert a lot of html pages generated with dygraphs in order to generate a gif animation. But for now, each call to webshot takes about three seconds to execute.

After doing a few tests, it looks like most of the time is spent to initialize phantomjs. I think it would save a lot of time to be able to pass a vector of URL and file names to the script webshot.js and to use casper.each to do the job.

Can't change user agent in requests

Reprex:

webshot::webshot("https://www.rstudio.com/products/rstudio/download/")

blank

Since rstudio.com and other sites reject the default user agent.

eval argument does not work in the webshot() function

The eval argument doesn't work for running javascript in the webshot() function.

When I try the example cited in the documentation (code below), a screenshot is taken, but the username and password are not filled in. I have also tested writing other javascript outside of webshot and it functions as expected, but as soon as I put it in webshot, it stops working.

webshot("http://www.reddit.com/", "reddit-input.png",
  selector = c("#search", "#login_login-main"),
  eval = "casper.then(function() {
    // Check the remember me box
    this.click('#rem-login-main');
    // Enter username and password
    this.sendKeys('#login_login-main input[type=\"text\"]', 'my_username');
    this.sendKeys('#login_login-main input[type=\"password\"]', 'password');

    // Now click in the search box. This results in a box expanding below
    this.click('#search input[type=\"text\"]');
    // Wait 500ms
    this.wait(500);
  });"
)```

Check that phantomjs is installed?

Is there a way to include something like what I have for packages

needed_pkgs <- c("nycflights13", "dplyr", "ggplot2", "knitr", 
  "devtools", "ggplot2", "webshot")
new.pkgs <- needed_pkgs[!(needed_pkgs %in% installed.packages())]

if (length(new.pkgs)) {
  install.packages(new.pkgs, repos = "http://cran.rstudio.com")
}

to check to see if phantomjs is installed? Right now I just have a commented out

#webshot::install_phantomjs()

in the same chunk as my chunk above but it would be nice if I could run a check and install it if needed.

save multiple captures from same site with one casper.open

I needed to save multiple captures from same site with one casper.open.

i solved it by modifying webshot.js as below incase anyone is intrested

`// This must be executed with phantomjs
// Take a screenshot of a URL and saves it to a .png file
// phantomjs webshot.js
//
// 'optsList' is a JSON array containing configurations for each screenshot that has
// to be taken. Each configuration object needs to contain at least properties
// "url" and "file". For instance:
// [{"url":"http://rstudio.github.io/leaflet/","file":"webshot.png"}]

var utils = require('./utils');
var system = require('system');

phantom.casperPath = phantom.libraryPath + '/casperjs';
phantom.injectJs(phantom.casperPath + '/bin/bootstrap.js');

var opt_defaults = {
delay: 0.2,
vwidth: 992,
vheight: 744,
zoom: 1
};

// =====================================================================
// Command line arguments
// =====================================================================
var args = system.args;

if (args.length < 2) {
console.log(
'Usage:\n' +
' phantomjs webshot.js \n' +
'\n' +
'optsList is a JSON array containing configuration for each screenshot.\n' +
'For instance:\n' +
''[{"url":"url1.html","file":"file1.png"},{"url":"url2.html","file":"fil2.png","zoom":2}]'');
phantom.exit(1);
}

var optsList = JSON.parse(args[1]);

// Options passed to CasperJS
var casperOpts = {};
if (optsList[0].options) {
casperOpts = JSON.parse(optsList[0].options);
}

// debug is a special option. The value from the first element in the
// optsList array is applied globally.
if (optsList[0].debug) {
casperOpts.verbose = true;
casperOpts.logLevel = 'debug';
}
delete optsList.debug;

var casper = require('casper').create(casperOpts);

// =====================================================================
// Screenshot
// =====================================================================

casper.start();
casper.options.onLoadError = function(c, url) {
console.log("Could not load ", url);
phantom.exit(1);
};

//////// ADDED FOR ONLY REQUESTING FIRST URL, REST IS IGNORED //////////////////
casper.then(function() {
var opts = optsList[1];

// Prepare options
opts = utils.fillMissing(opts, opt_defaults);

// Go to url and perform the desired screenshot
this.zoom(opts.zoom)
.viewport(opts.zoom * opts.vwidth, opts.zoom * opts.vheight)
.thenOpen(opts.url)
.wait(opts.delay * 1000)
});
//////// ADDED FOR ONLY REQUESTING FIRST URL, REST IS IGNORED //////////////////

casper.eachThen(optsList, function(response) {
var opts = response.data;

// Prepare options
opts = utils.fillMissing(opts, opt_defaults);

// This should be four numbers separated by ","
if (opts.cliprect) {
opts.cliprect = opts.cliprect.split(",");
opts.cliprect = opts.cliprect.map(function(x) { return +x; });
}

// Can be 1 or 4 numbers separated by ","
if (opts.expand) {
opts.expand = opts.expand.split(",");
opts.expand = opts.expand.map(function(x) { return +x; });
if (opts.expand.length !== 1 && opts.expand.length !== 4) {
console.log("'expand' must have either 1 or 4 values.");
phantom.exit(1);
}
}

// Can have multiple selectors
if (opts.selector) {
opts.selector = opts.selector.split(",");
}

//////// FIRST THREE CALLS ARE ALREADY DONE, ONLY SAVE selector //////////////////
// Go to url and perform the desired screenshot
// this.zoom(opts.zoom)
// .viewport(opts.zoom * opts.vwidth, opts.zoom * opts.vheight)
// .thenOpen(opts.url)
// .wait(opts.delay * 1000)
this.then(function() {
if (opts.eval) {
eval(opts.eval);
}
})
.then(function() {
var cr = findClipRect(opts, this);
this.capture(opts.file, cr);
});
});

casper.run();

`

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.