R Markdown Document
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.
R
library (leaflet) library (networkD3) # Create the leaflet widget office_loc <- c (28.505037217956442, 77.39902539926143) # location stored as an array widget1 <- leaflet () %>% addMarkers (lng = office_loc[2] , lat = office_loc[1], popup = "w3wiki Office" ) %>% addTiles () # Create the networkD3 widget widget2 <- simpleNetwork (SchoolsJournals[ seq (100, 200, by=2), ], zoom = TRUE ) ``` widget1 widget2 |
Output:
In Widget 1
We have used the leaflet library for interactive maps and marked the w3wiki office location which initially we saved as coordinates in an array office_loc. Here is the code:-
- addTiles() function adds the default tile layer made by Google Maps to the leaflet widget forming the base of the map.
- addMarkers() function adds a marker to the map in the leaflet widget by taking in parameters lng (longitude), lat (latitude), and popup (text displayed when the marker is clicked). Array indexing in R starts from 1 hence we have accordingly set the latitude and longitude in the addMarkers() function.
In Widget 2
We 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.
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.