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.

Inserting screenshot within a Word document

Inserting screenshot within a PowerPoint presentation



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.

The various implementations in different documents have been described below.

Similar Reads

1. Standalone HTML Pages

We can embed HTML widgets in standalone HTML pages using the saveWidget() function. This will create an HTML file that includes the necessary JavaScript and CSS dependencies. We can then include the generated HTML file in our HTML document using an iframe tag....

2. Shiny application

...

3. R Markdown Document

...

4. Word, PowerPoint, Excel, and PDF

Shiny is an R package that allows us to build interactive web applications directly from R. In Order to create a simple shiny app we need to have an `app.R` file that contains ui and server. The UI (User Interface) controls the looks and experience of the user and the server function defines the server logic of the Shiny app which controls what is being rendered in the application screen. To install it, type in R console the following:-...

Capturing Screenshots of HTML Output in R Code

...

Capturing Screenshots in the Shiny web application

R Markdown is a format for creating dynamic documents combining code, text, and visualizations. It is similar to IPython Notebooks in Python programming language though not exactly the same. R Markdown documents are rendered to HTML output by default since it allows for the inclusion of dynamic content like executing code, interactive visualization, and others that are best supported in HTML format....