-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
103 lines (71 loc) · 5.17 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
---
title: "README"
author: "Maria Guideng"
output: github_document
---
# kableExtra-Pkg-NV-LQ
## Application of kableExtra, an R package
Looking for a way to generate a formatted table in R? No? Well now you can!
R is not that great for final product data visualization, but if there was a simple way to give my plain tables a little love, I figured, 'Why not?' So I asked The Google, and it brought me to [this article](https://haozhu233.github.io/kableExtra/awesome_table_in_html.html) by Hao Zhu, creator of the [kableExtra](https://CRAN.R-project.org/package=kableExtra) R package. The features to manipulate table styles seemed straightforward enough for a noob like me with no experience programming in CSS, so I decided to give it a run. The lucky table chosen for the makeover was from tabular data to calculate location quotients (LQs) in Las Vegas. The LQs quantified how concentrated a particular industry is in a region as compared to the nation. Specifically, I wanted to highlight the obvious about my table: gaming and hospitality drives the Vegas economy. It took a few iterations, but I figured out how to style everything the way I wanted (after augmentation with the `formattable` package). Check out the before and after tables below.
```{r, echo=FALSE, results='hide', message=FALSE, warning=FALSE}
library(knitr)
options(knitr.table.format = "html")
library(kableExtra)
ls("package:kableExtra")
#help(package = "kableExtra")
library(magrittr) # %>%
library(dplyr) # to mutate
library(formattable) # to add gradient to table cell specs
urlfile <- 'https://raw.githubusercontent.com/mguideng/KableExtra-Pkg-NV-LQ/master/data/lqmod.csv'
lqtab <- read.csv(urlfile)
#Change class modes
sapply(lqtab, class)
cols.num <- c("US.Empl", "US.Percent", "LV.Empl", "LV.Percent", "LV.EmplRqmts")
lqtab[cols.num] <- sapply(lqtab[cols.num],as.numeric)
cols.char <- c("NAICS", "Industry", "LV.LQ", "LV.LQClass", "LV.ExcessEmpl")
lqtab[cols.char] <- sapply(lqtab[cols.char],as.character)
#Add a column row for totals
library(janitor)
lqtab <- lqtab %>%
adorn_totals("row", fill="", na.rm = F)
# Revert back to a numeric. Was set as characters because did not want summed in adorn_totals()
lqtab$LV.LQ <- as.numeric(lqtab$LV.LQ)
lqtab$LV.ExcessEmpl <- as.numeric(lqtab$LV.ExcessEmpl)
# Format to include percents, commas and decimals for clarity
# Percent columns
lqtab$US.Percent <- paste(round(lqtab$US.Percent*100, digits=2),"%",sep="")
lqtab$LV.Percent <- paste(round(lqtab$LV.Percent*100, digits=2),"%",sep="")
# Number columns
lqtab$US.Empl <- format(lqtab$US.Empl,big.mark=",", trim=TRUE)
lqtab$LV.Empl <- format(lqtab$LV.Empl,big.mark=",", trim=TRUE)
lqtab$LV.EmplRqmts <- format(lqtab$LV.EmplRqmts,big.mark=",", trim=TRUE)
lqtab$LV.ExcessEmpl <- format(lqtab$LV.ExcessEmpl,big.mark=",", trim=TRUE)
# Quotient column
lqtab$LV.LQ <- format(round(lqtab$LV.LQ, 2), nsmall = 2)
# Name headers that will include spaces in them for clarity
names(lqtab)
colnames(lqtab) = c("NAICS", "Industry", "US Employment", "US % Share", "Las Vegas Employment",
"Las Vegas % Share", "Employment for Las Vegas Requirements", "Las Vegas LQ",
"Las Vegas LQ Classification", "Las Vegas Excess Employment (to export) or Deficit")
```
This is the basic HTML table in `kable`:
```{r}
kable(lqtab, "html")
```
And here's the makeover after some formatting help from `kableExtra`.
![after](images/after.PNG)
It's a nice bundle of functions. It's one that is easy to use and easy to understand; thus, it should be popular for those looking to keep the simplicity of the [Bootstrap](https://www.w3schools.com/bootstrap/bootstrap_tables.asp) table style while adding flexible customization. It's better suited for model-output documents and statistical result-type tables, rather than for fully leveraging on the power of visualization to help interpret the data.
**Package description**
> "Build complex HTML or 'LaTeX' tables using 'kable()' from 'knitr'
and the piping syntax from 'magrittr'. Function 'kable()' is a light weight
table generator coming from 'knitr'. This package simplifies the way to
manipulate the HTML or 'LaTeX' codes generated by 'kable()' and allows
users to construct complex tables and customize styles using a readable
syntax."
**Process**
The source of the industry data comes from the Bureau of Labor Statistics [QCEW program](https://www.bls.gov/cew/datatoc.htm). The two CSV input files are zipped up and located in the `/data/allfiles` subfolder. One is Las Vegas data and the other is national data. This mini-project is for 2016 annual averages only, but other years are available (`/data/allfilesMASTER`).
Files in the `/script` subfolder serve different purposes.
* _(1script-merge-allfiles.R)_: Merges the two CSV input files for the chosen year into a single dataframe.
* _(2script-lq-model.R)_: Calculates and models the LQs into a table layout.
* _(3script-lq-table.Rmd)_: Runs the `knitr::kable` and `kableExtra` functions to format the table. This is also the output since it's intended for conversions from R Markdown to Markdown and HTML.
![process](images/process.PNG)