Capturing Screenshots in the Shiny web application
For capturing screenshots in Shiny web applications, the preferred choice is the shinyscreenshot
package instead of webshot
. The shinyscreenshot
the package is specifically designed as a Shiny extension to offer a simple and convenient solution for capturing screenshots within a Shiny application. It provides an easy way to capture screenshots of specific UI elements or the entire application window.Unlike webshot
with PhantomJS, shinyscreenshot
does not require any external dependencies or additional installations.
install.packages("shinyscreenshot")
R
library (shiny) library (shinyscreenshot) library (wordcloud2) # Create the wordcloud widget widget <- wordcloud2 (demoFreq, size=1.5, shape = 'cardioid' ) # Define the Shiny app UI ui <- fluidPage ( wordcloud2Output ( "widget" ), actionButton ( "go" , "Take a screenshot" ) ) # Define the server and make the take a screenshot button functional server <- function (input, output) { output$widget <- renderWordcloud2 ({ widget}) observeEvent (input$go, { screenshot (filename = "screenshot" , selector = "#widget" ) }) } # Run the Shiny app shinyApp (ui, server) |
Output:
word cloud
In the above code, we have used the wordcloud2 library to create an interactive word cloud. We have generated a cardioid shape word cloud using the wordcloud2() function with different words present in the demoFreq dataset.
In the UI definition, actionButton()
function
adds an actionButton
widget to the UI for users to click and trigger an action. We have labeled the button as “Take a screenshot” having the ID “go“.
In the server definition, observeEvent()
function, the input$go
is used to
observe changes in the “go” action button. Inside the observer, the screenshot
the function is called with the filename
parameter set to “screenshot” and the selector parameter ensures only the screenshot of the widget is clicked and not the entire page.
Capturing Screenshots in R Markdown
R
library (rbokeh) # load the data from rbokeh package data (flightfreq) # create the rbokeh widget widget <- figure (height = 500, width = 1000, xlab = "Date" , ylab = "Frequncy of Flights" ) %>% ly_points (date, Freq, data = flightfreq, color = dow, glyph = dow, hover = list (date, Freq, dow)) widget |
Output:
Here we have used the Rbokeh library of R for visualizing the flightfreq dataset that comes with rbokeh library. It contains the frequency of flights in the world from 1999 to 2008. We have tried to plot the date vs frequency of flights and marked all the days of weeks with different colors and different glyphs for more clarity in the plot. In the figure() function, we have specified the parameters of the plot like height width, x-label, and y-label. In the ly_points() function we pass date first as x-axis and then the Freq as the y-axis, the color and glyph parameters for the colorful plot, and the hover parameter controls what to display when we drag the mouse pointer over some point in the plot.
Save it as some “filename.Rmd”, lets assuming filename to be test6.Rmd. No need to render it. Now save the below code in a different R file in the same location.
The rmdshot()
function is used to capture screenshots of R Markdown documents. It can handle both static and dynamic R Markdown documents. It first renders the R markdown document and then captures the screenshot. Hence no need to render the R markdown file separately the before compiling above file as it does the operation for us inherently.
After having generated our screenshot we can simply insert the images saved in our personal computer within Word, PowerPoint, Excel or PDF file and do as per requirement.
How to Embed htmlwidgets into Documents in R
HTML widgets in R Programming Launagauge are interactive web components, JavaScript-based visualizations that can be easily integrated into different documents. They are built on top of the HTML widgets package, which provides a framework for creating R bindings to JavaScript libraries. Examples of HTML widgets available for R include leaflet, dygraphs, Plotly, DiagrammeR, and others.
HTML widgets can be embedded directly into various types of documents created in R, such as R Markdown files, Shiny web applications, and standalone HTML pages, and even indirectly into documents like Word, Excel, PowerPoint, and PDF files. Embedding HTML widgets into documents ensures reproducibility, eliminating any discrepancies that may arise from the manual re-creation of the same document on different setups and environments.
Prerequisites :-
The various implementations in different documents have been described below.
Contact Us