The webshot package makes it easy to take screenshots of web pages from R. In order to export the graphs or figures created with plotly or other graphing libraries as static images/files, webshot is also great option in addition to the Orca. Hence, saving locally generated plots as png, jpg or pdf will be pretty straightforward at the end of this post. Enjoy with it!
Webshot requires an installation of the external program PhantomJS on R/RStudio. You might get this error: “PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable. NULL”, if you have not already installed PhantomJS on your computer. Anyway, either you might download the binary and put it in PATH manually or you might easily install phantom.js as:
# See ?install_phantomjs for the details # install_phantomjs() webshot::install_phantomjs()
Instead of using webshot, you should consider to try webshot2 because webshot suffers from plotly or graph exporting issues unlike webshot2. See also my detailed answer on stackoverflow. You could find that various developers have issues on stackoverflow related with this side, and I have replicated/solved many of them by just replacing webshot with webshot2. You can access those examples throughout my stackoverflow profile as well.
Yes, moving on webshot2?
Before trying to install webshot2 package, do not forget to remove webshot if you have already one installed. In order to remove it, go to the Packages in right bottom corner of Rstudio, sear the package name and click on the adjacent X icon to remove it or you I handle it in this way from the Rstudio console:
If you have not done this uninstallation, you might get this kind of error:
Attaching package: ‘webshot2’ The following object is masked _by_ ‘.GlobalEnv’: %>% The following objects are masked from ‘package:webshot’: appshot, resize, rmdshot, shrink, webshot
Installation of webshot2
Usage of webshot2
Now, we can present a minimal example:
''' Created on 28/04/2020 @author: Ibrahim Ozturk @author: www.ozturkibrahim.com ''' # Webshot and phantomjs have been previously installed. # Then, I loaded related packages for the minimal scenario. require(webshot2) require(plotly) require(htmlwidgets) data("iris") test_p = plot_ly(x = iris$Petal.Width, y = iris$Petal.Length, type = "scatter", mode = "markers") # Save a rendered widget to an HTML file saveWidget(widget = test_p, file = "test_p.html") # Saving taken webshot webshot(url = "test_p.html", file = "webshot_iris.png")
You will get something like this message on the console:
file://C:\Users\root\.......\test_p.html screenshot completed
You could modify the extension as you like such as .png, .pdf, or .jpeg.
The output file
Once I have tried to uninstall R related sides, I realised that it is not straightforward unlike other programming engines. For that reason, I have posted the procedure on the stackoverflow website under the related questions. See the link: https://stackoverflow.com/questions/55204017/how-to-uninstall-r-and-rstudio-with-all-packages-settings-and-everything-else/61187094#61187094
I have further issues with the answers on the forums/internet. For that reason, I have extended the solution with further steps (which I experienced currently) as below:
Delete everything in folders that was shown after running .libPaths() in R. In my case, it looks like:
1 “C:/Users/%USERNAME%/Documents/R/win-library/3.6” “C:/Program Files/R/R-3.6.3/library”
Therefore, I have manually deleted following files and folders (they remain there after full uninstallation of R, RStudio and RTools) as well:
- C:\Users\%USERNAME%\Documents\R\ - C:\Users\%USERNAME%\Documents\.Rhistory - Various files and folders under C:\Users\%USERNAME%\AppData\Local\Temp related with R and RStudio sessions.
Koronavirus pandemi karantinasi sürecinde her birimizin farklı neden ve kaygılarla evlere veya kapalı mekânlara sıkıştığı, kimilerimizin de durumu kotarabilmek adına…