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.

Assume we create a simple HTML widget using Plotly, a popular visualization library. We will create a plot using a preloaded dataset in R. To get a list of preloaded datasets in R type the following in the R console:-

data()

We will use the Morley dataset for our plot, it contains 100 observations where 5 experiments were conducted with 20 observations each for measuring the speed of light. It contains 3 columns `Expt`, `Run`, and `Speed`. We will try to plot a simple line plot with `Run` on the x-axis and `Speed` on the y-axis for 5 different experiments with different colors and save it in a html file named widget.html.

R




library(plotly)
library(htmlwidgets)
 
# Viewing the contents of morley dataset
print(morley)


Output:-


001    1   1   850
002    1   2   740
003    1   3   900
004    1   4  1070
005    1   5   930

R




# Saving it as a plot_ly widget
widget <- plot_ly(data = morley, x = ~Run, y = ~Speed,
          color = ~factor(Expt), mode = "line") %>%
          layout(legend = list(title = list(text = "Expt no.")))
 
# Saving the Plotly widget as an HTML file
saveWidget(widget, file = "widget.html", selfcontained = TRUE)


Output:

htmlwidgets documentation in R

In our plotly widget, the color parameter is passed the factor() passed data structure, here we pass the `Expt` column meaning that with a passed value of the experiment the color of our line plot changes, `mode = “line”` indicates a line plot to be generated.

‘%>% ‘ is the pipe operator which is used to chain multiple function calls together by taking output from the previous function call and passing it as the first argument to the next function call. It allows for a more concise and readable code structure.

After having defined the widget we need to save it as an HTML file using the save widget () function. It contains the following parameters:-

  • `widget` is the name of the HTML widget object that we want to save.
  • file parameter specifies the name of the file where our widget will be saved.
  • selfcontained parameter specifies whether to include all the necessary JavaScript and CSS dependencies within the HTML file itself or not. When self-contained is set to TRUE, the resulting HTML file will be self-contained and can be opened and viewed without any external dependencies.

Wait for the code to execute completely, after it finishes notice that the widget.html file is generated in the present working directory. Now open it in the browser to view it.
let’s

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....