![]() |
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. 1. Standalone HTML PagesWe 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
Output:-
001 1 1 850
002 1 2 740
003 1 3 900
004 1 4 1070
005 1 5 930
R
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.
After having defined the widget we need to save it as an HTML file using the save widget () function. It contains the following parameters:-
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. 2. Shiny applicationShiny 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:- install.packages("shiny")
R
Output: ![]() HTML widget using shiny in R the
In the above code, we have used an extra library DT which helps in creating interactive tables in HTML. We have used the data table () function of the DT library to convert the wind dataset provided by the Plotly package in R(it is not a preloaded dataset in R) to an interactive HTML table. The package name the
3. R Markdown DocumentR 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. R
Output: ![]() R markdown plot ![]() R markdown plot In Widget 1We have used the leaflet library for interactive maps and marked the GeeksForGeeks office location which initially we saved as coordinates in an array office_loc. Here is the code:-
In Widget 2We have used the networkD3 library to create a simple network graph from R using the dataset SchoolsJournals present in the networkD3 library (It is not a preloaded dataset in R). In the code `seq(100,200,by=2)` all the 51 rows numbered from 100 to 200 skipping two rows in the middle are mentioned in the dataset to be used to create the network graph. The zoom parameter has been set to true so that the graph can be zoomed in and out for clarity. The simple network () function creates our graph after passing the dataset. 4. Word, PowerPoint, Excel, and PDFWord, PowerPoint, Excel, and PDF are non-HTML formats, hence they do not support the rendering of interactive elements like HTML widgets. To overcome this limitation, we can take a screenshot of the HTML widget and insert the screenshot in the document and thus indirectly embedding the static capture of the widget. Webshot library in R helps to take screenshots of web pages automating the process of capturing the screenshot after the widget is created rather than us manually capturing screenshots. Before using the webshot package, we need to install it in our R environment. Additionally, the phantomjs tool, which is required by webshot, also needs to be installed. install.packages("webshot")
webshot::install_phantomjs()
Capturing Screenshots of HTML Output in R CodeFirst, we will create the HTML widget, then save it as an HTML file and then take a screenshot using the webshop package. R
Output: ![]() Plot Here we have used the dygraphs package of R. It is used for plotting time-series data i.e. dataset which shows variations with time. There are a couple of preloaded datasets in R to try this out like the EuStockMarkets, austres, JohnsonJohnson, and LakeHuron. Here we will use the LakeHuron dataset which contains data about the levels of Lake Huron from 1875 till 1972. We simply pass the dataset to the dygraph() function of dygraphs package in order to plot it and for clarity, we have labeled the x-axis as “Time” and the y-axis as “Level of Lake Huron”. The plot generated in the HTML file widget.html is an interactive one. Finally after the a creation of html file the webshot() function takes a screenshot of the widget in the html file as mentioned in url parameter and saves it in the file parameter value. Capturing Screenshots in the Shiny web applicationFor capturing screenshots in Shiny web applications, the preferred choice is the install.packages("shinyscreenshot")
R
Output: word cloud In the UI definition, In the server definition, Capturing Screenshots in R MarkdownR
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 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 |
Reffered: https://www.geeksforgeeks.org
R Language |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |