Interactive Charts and Tables

Coding Confident

6/26/2021

Step 1 - Install and Load Packages

If you have already installed these packages, do not install again

install.packages("rpivotTable")
install.packages("gt")
install.packages("dplyr")
install.packages("lubridate")
#Load Packages
library(rpivotTable)
library(gt)   
library(dplyr)
library(lubridate)

Step 2 - Import Data

Load in a dataset that is located in the gt package containing pizza sales transactions. After the package is loaded we can use datasets that are within the package. If we type pizzaplace we can view a dataset containing pizza sales transactions. To use other datasets, run the code ?gt to view datatsets within the gt package.

pizzaplace
## # A tibble: 49,574 x 7
##    id          date       time     name        size  type    price
##    <chr>       <chr>      <chr>    <chr>       <chr> <chr>   <dbl>
##  1 2015-000001 2015-01-01 11:38:36 hawaiian    M     classic  13.2
##  2 2015-000002 2015-01-01 11:57:40 classic_dlx M     classic  16  
##  3 2015-000002 2015-01-01 11:57:40 mexicana    M     veggie   16  
##  4 2015-000002 2015-01-01 11:57:40 thai_ckn    L     chicken  20.8
##  5 2015-000002 2015-01-01 11:57:40 five_cheese L     veggie   18.5
##  6 2015-000002 2015-01-01 11:57:40 ital_supr   L     supreme  20.8
##  7 2015-000003 2015-01-01 12:12:28 prsc_argla  L     supreme  20.8
##  8 2015-000003 2015-01-01 12:12:28 ital_supr   M     supreme  16.5
##  9 2015-000004 2015-01-01 12:16:31 ital_supr   M     supreme  16.5
## 10 2015-000005 2015-01-01 12:21:30 ital_supr   M     supreme  16.5
## # ... with 49,564 more rows

Turn the dataset into a dataframe using as.data.frame() and assign it to a name, In this case we are naming our dataframe “df”

df <- as.data.frame(pizzaplace)

Step 3 - Create a Customizable Chart or Table

rpivotTable is an amazing package that allows us to produce dynamic pivot charts and pivot tables. Simply type data = the name of your dataframe within the rpivotTable function. Once the table is loaded simply drag and drop the fields in the gray space to create charts. The dropdowns have common tables and charts that you would find in Excel, along with different methods to summarize the data, like “sum as fraction of total”.

Once the plot is created click the Alt text button to open in full screen.

rpivotTable(data = df)

Setting the Chart to Load with a Specific format

There is also the option to set predefined parameters that will define what chart is loaded first. These parameters are specific to the rpivotTable package.

Parameters of first loaded chart:

cols = the name of the default column in the chart

rows = the name of the default row in the chart

vals = the name of the default variable to count or summarize

rendererName = the default type of chart or table

aggregatorName = the default method of summarizing the data

rpivotTable(data = df, 
            cols = "name", rows = "type", vals = "price", 
            rendererName = "Bar Chart", aggregatorName = "Sum")

Save the Dynamic Chart

Keep an eye out for our future courses to learn how to save this chart to an HTML website.