# Creating a Formatted HTML Document with Graphed Data with the rmarkdown Package

You can use the rmarkdown package with another Javascript-enabled package to create a formatted HTML document with graphed data.

## About this task

This example uses the same data from the task described in the topic Mapping data with TERR and leaflet to create a map chart with data available on the internet. Additionally, we create a document with formatted text and an interactive map, and then display the results in a stand-alone HTML file.

- To install the required packages, call the `install.packages()` function from the TERR console.
- To run the script that creates and displays the results, call the functions from the command line.

Package link | Package short description
--- | ---
https://dplyr.tidyverse.org/ | Work with data frame-like objects, both in memory and out of memory.
https://rstudio.github.io/leaflet/ | Create and customize interactive maps using the 'Leaflet' JavaScript library and the 'htmlwidgets' package.
https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf | Convert R Markdown documents into a variety of formats.

## Before you begin

TERR, access to the internet, and a web browser.

## Procedure

1. From the TERR console, install the dplyr and leaflet packages.

       install.packages(c("dplyr", "leaflet", "rmarkdown"))

   TERR checks TRAN and then CRAN for the packages to install, and then installs them along with any packages they require.
2. Copy the code below to a text editor, and then save the file using the file name leaflet_starbucks.Rmd.

       ---
       title: "Starbucks in Seattle"
       author: "Cloud Software Group, Inc."
       date: "`rformat(Sys.time(), '%d %B %Y')`"
       output: html_document
       ---

       ```{r setup, include=FALSE}
       knitr::opts_chunk$set(echo = TRUE)
       ```

       ## Using TERR with Rmarkdown and leaflet

       We are creating an html document in TERR using the `rmarkdown` package.
       For graphics, we use the `leaflet` package to draw a map.

       For this example we use data on the location of Starbucks stores in the U.S.
       which is downloaded from the https://opendata.socrata.com website.

       This document can be processed by TERR from the command line with the command:
       ```{r eval=FALSE}
       TERR -e "rmarkdown::render('leaflet_starbucks.Rmd')"
       ```

       This will create the file `leaflet_starbucks.html` which we can view in a browser.

       ## Setup
       First we load the required packages.
       ```{r packages, message=FALSE, warning=FALSE}
       library("dplyr")
       library("leaflet")
       ```

       ## The Data

       We restrict the data to stores in Washington state
       (using `filter` from the `dplyr` package).

       ```{r data}
       file <- "https://opendata.socrata.com/api/views/ddym-zvjk/rows.csv"
       starbucks <- read.csv(file) %>%
         filter(State == "WA")
       ```

       ## Starbucks in Seattle

       We use the `leaflet` package functions to draw a map center in Seattle with the
       Starbuck store locations identified.

       ```{r leaflet}
       leaflet() %>%
         addTiles() %>%
         setView(-122.32, 47.605, zoom = 12) %>%
         addMarkers(data = starbucks, lat = ~ Latitude, lng = ~ Longitude,
           popup = starbucks$Name)
       ```

3. Open a command window, and navigate to the directory where you saved the file leaflet_starbucks.Rmd.
4. At the command prompt, type the following command.

       TERR -e "rmarkdown::render('leaflet_starbucks.Rmd')"

   Note: You might have to supply the entire path to the .Rmd file, unless you run the command from the directory where the file is saved.
   This command starts a TERR engine and calls the rmarkdown function `render`, passing in the file as the argument. This function renders the markdown text as HTML, and formats the code contained in the text, even as the code is run. (For information about the `render` function, see its help topic.)
   1. The packages are loaded.
   2. The .csv containing the data is loaded from the web site.
   3. The data is filtered to just those values in Washington state.
   4. The leaflet functions are called to do the following.
      - Add the map tile service.
      - Set the view to center and zoom on the city of Seattle.
      - Add latitude and longitude markers for each entry in the filtered data.
      - Add the ability to show the value in the Name column for the selected entry in a popup.
   Note: You might notice warning messages for functions that are not yet implemented in TERR. These warnings do not affect the output.
5. Open the directory where you saved the .Rmd file, and then locate the file that the command built: leaflet_starbucks.html.
6. Open the file and review the results in a browser.

## Results

The resulting HTML file shows formatted text, containing a walkthrough for creating the map example.