Skip to content

Commit 8b3f703

Browse files
committed
try using DT::datatable in place of knitr::kable, which was giving ok results locally but ugly results on the web
1 parent df0ebed commit 8b3f703

5 files changed

+8
-55
lines changed

_posts/2017-08-04-how-we-voted-in-greenville-sc.md

Lines changed: 8 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,10 @@ The election data has a bit of an odd structure.
8888

8989

9090
{% highlight r %}
91-
knitr::kable(election_df %>% mutate(row=1:nrow(.)) %>% select(row,everything()) %>% slice(c(1:5,103:107)))
91+
DT::datatable(election_df %>% mutate(row=1:nrow(.)) %>% select(row,everything()) %>% slice(c(1:5,103:107)))
9292
{% endhighlight %}
9393

94-
95-
96-
| row|county |precinct |office |district |party |candidate | votes|
97-
|---:|:---------|:---------------|:----------------------------|:--------|:-----|:----------------------------------------------|-----:|
98-
| 1|Abbeville |Abbeville No. 1 |STRAIGHT PARTY |NA |DEM |Democratic | 139|
99-
| 2|Abbeville |Abbeville No. 2 |STRAIGHT PARTY |NA |DEM |Democratic | 248|
100-
| 3|Abbeville |Abbeville No. 3 |STRAIGHT PARTY |NA |DEM |Democratic | 134|
101-
| 4|Abbeville |Abbeville No. 4 |STRAIGHT PARTY |NA |DEM |Democratic | 82|
102-
| 5|Abbeville |Antreville |STRAIGHT PARTY |NA |DEM |Democratic | 72|
103-
| 103|Abbeville |Lebanon |STRAIGHT PARTY |NA |LIB |Libertarian | 1|
104-
| 104|Abbeville |Absentee |STRAIGHT PARTY |NA |LIB |Libertarian | 4|
105-
| 105|Abbeville |Abbeville No. 1 |President and Vice President |NA |DEM |Hillary Rodham Clinton / Timothy Michael Kaine | 245|
106-
| 106|Abbeville |Abbeville No. 2 |President and Vice President |NA |DEM |Hillary Rodham Clinton / Timothy Michael Kaine | 373|
107-
| 107|Abbeville |Abbeville No. 3 |President and Vice President |NA |DEM |Hillary Rodham Clinton / Timothy Michael Kaine | 214|
94+
![plot of chunk unnamed-chunk-5](/figures//2017-08-04-how-we-voted-in-greenville-sc.Rmdunnamed-chunk-5-1.png)
10895

10996
So the fancy stuff I did in `kable` above was basically to show the original row numbers and print them to the left of all the other variables. If I had not used the `select(row,everything())`, the row would have printed last. This is a nice example of how to do a quick custom column-sorting. But that's not why we're here. The election file shows how people voted in a rather raw fashion. Specifically, here I want to tally those people who voted for the different candidates for president. But it's not that straightforward, because I have to count the straight-ticket voters as well as the split-party voters who specifically noted their president's choice on the ballot.
11097

@@ -116,18 +103,10 @@ total_pres_votes <- election_df %>%
116103
filter(office %in% c("STRAIGHT PARTY","President and Vice President")) %>%
117104
group_by(precinct) %>%
118105
summarize(total_votes=sum(votes,nm.ra=TRUE))
119-
knitr::kable(total_pres_votes %>% slice(1:5))
106+
DT::datatable(total_pres_votes)
120107
{% endhighlight %}
121108

122-
123-
124-
|precinct | total_votes|
125-
|:---------------|-----------:|
126-
|Abbeville No. 1 | 1250|
127-
|Abbeville No. 2 | 1054|
128-
|Abbeville No. 3 | 744|
129-
|Abbeville No. 4 | 611|
130-
|Abel | 731|
109+
![plot of chunk unnamed-chunk-6](/figures//2017-08-04-how-we-voted-in-greenville-sc.Rmdunnamed-chunk-6-1.png)
131110

132111
The first few results look ok, so we can get votes for the different parties and merge this back on to get the percentage.
133112

@@ -150,23 +129,10 @@ total_party_votes <- election_df %>%
150129

151130

152131
{% highlight r %}
153-
knitr::kable(total_party_votes %>% ungroup() %>% slice(1:10))
132+
DT::datatable(total_party_votes)
154133
{% endhighlight %}
155134

156-
157-
158-
|precinct |party | total_party_votes| total_votes| party_perc|
159-
|:---------------|:-----|-----------------:|-----------:|----------:|
160-
|Abbeville No. 1 |DEM | 385| 1250| 30.80000|
161-
|Abbeville No. 1 |REP | 818| 1250| 65.44000|
162-
|Abbeville No. 2 |DEM | 622| 1054| 59.01328|
163-
|Abbeville No. 2 |REP | 407| 1054| 38.61480|
164-
|Abbeville No. 3 |DEM | 349| 744| 46.90860|
165-
|Abbeville No. 3 |REP | 375| 744| 50.40323|
166-
|Abbeville No. 4 |DEM | 214| 611| 35.02455|
167-
|Abbeville No. 4 |REP | 381| 611| 62.35679|
168-
|Abel |DEM | 418| 731| 57.18194|
169-
|Abel |REP | 241| 731| 32.96854|
135+
![plot of chunk unnamed-chunk-7](/figures//2017-08-04-how-we-voted-in-greenville-sc.Rmdunnamed-chunk-7-1.png)
170136

171137
So one picky issue is worth mentioning here. This dataset counts absentee ballots as its own precinct (or their own precincts). Part of the joys of blogging is sweeping issues like this under the rug, but this can be the source of interesting analyses in their own right.
172138

@@ -178,23 +144,10 @@ total_party_votes_wide <- total_party_votes %>%
178144
select(-total_party_votes,-total_party_votes) %>%
179145
group_by(precinct) %>%
180146
spread(key=party,value=party_perc)
181-
knitr::kable(total_party_votes_wide %>% ungroup() %>% slice(1:10))
147+
DT::datatable(total_party_votes_wide )
182148
{% endhighlight %}
183149

184-
185-
186-
|precinct | total_votes| DEM| REP|
187-
|:-------------------|-----------:|--------:|--------:|
188-
|Abbeville No. 1 | 1250| 30.80000| 65.44000|
189-
|Abbeville No. 2 | 1054| 59.01328| 38.61480|
190-
|Abbeville No. 3 | 744| 46.90860| 50.40323|
191-
|Abbeville No. 4 | 611| 35.02455| 62.35679|
192-
|Abel | 731| 57.18194| 32.96854|
193-
|Abner Creek Baptist | 1221| 15.88862| 80.26208|
194-
|Absentee | 521425| 50.41051| 47.02555|
195-
|Absentee 1 | 56422| 50.27117| 47.29184|
196-
|ABSENTEE 1 | 29495| 46.50958| 50.18478|
197-
|Absentee 2 | 107903| 55.46278| 42.20457|
150+
![plot of chunk unnamed-chunk-8](/figures//2017-08-04-how-we-voted-in-greenville-sc.Rmdunnamed-chunk-8-1.png)
198151

199152
There were some casualties in this operation, namely the total party votes and total votes, but I don't really need them for the simple thing I'm doing here.
200153

0 commit comments

Comments
 (0)