Browse Source

initial commit

master
boB Rudis 4 years ago
parent
commit
e87dc6bc71
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 2
      .Rbuildignore
  2. 10
      DESCRIPTION
  3. 2
      LICENSE
  4. 21
      LICENSE.md
  5. 2
      NAMESPACE
  6. 21
      R/get-delegates.R
  7. 71
      R/gg-catchpole.R
  8. BIN
      R/sysdata.rda
  9. 11
      README.Rmd
  10. 80
      README.md
  11. 12
      data-raw/DATASET.R
  12. BIN
      man/figures/README-u-1-1.png
  13. 16
      man/gg_catchpole.Rd
  14. 14
      man/read_delegates.Rd

2
.Rbuildignore

@ -19,3 +19,5 @@
^CRAN-RELEASE$
^appveyor\.yml$
^tools$
^data-raw$
^LICENSE\.md$

10
DESCRIPTION

@ -12,13 +12,17 @@ Description: A good description goes here otherwise CRAN checks fail.
URL: https://git.rud.is/hrbrmstr/catchpole
BugReports: https://git.rud.is/hrbrmstr/catchpole/issues
Encoding: UTF-8
License: AGPL
License: MIT + file LICENSE
Suggests:
covr, tinytest
Depends:
R (>= 3.2.0)
Imports:
httr,
jsonlite
sf,
dplyr,
tidyr,
jsonlite,
ggplot2,
hrbrthemes
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.0.2

2
LICENSE

@ -0,0 +1,2 @@
YEAR: 2020
COPYRIGHT HOLDER: Bob Rudis

21
LICENSE.md

@ -0,0 +1,21 @@
# MIT License
Copyright (c) 2020 Bob Rudis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

2
NAMESPACE

@ -1,4 +1,6 @@
# Generated by roxygen2: do not edit by hand
export(gg_catchpole)
export(read_delegates)
import(httr)
importFrom(jsonlite,fromJSON)

21
R/get-delegates.R

@ -0,0 +1,21 @@
#' Retrieves current U.S. State delegate assignments for 2020 from the WSJ
#'
#' @export
#' @references <https://asset.wsj.net/wsjnewsgraphics/election/2020/delegates.json>
read_delegates <- function() {
jsonlite::fromJSON(
url("https://asset.wsj.net/wsjnewsgraphics/election/2020/delegates.json"),
simplifyDataFrame = FALSE
) -> del
state_del <- del
state_del$data[["US"]] <- NULL
map_df(state_del$data, ~bind_cols(.x$delCount), .id = "state") %>%
gather(candidate, delegates, -state) %>%
filter(delegates > 0) -> states
states
}

71
R/gg-catchpole.R

@ -0,0 +1,71 @@
#' Plot a U.S. Democratic Delegates Cargogram (2020)
#'
#' @param delegates a data frame with `state`, `candidate`, and
#' `delegates` count or use the default which gets the
#' current data as long as the WSJ keeps it alive.
#' @export
gg_catchpole <- function(delegates = read_delegates()) {
map_df(unique(delegates$state), ~{
filter(delegates, state == .x) %>%
arrange(desc(delegates)) -> cur
tibble(
state = character(0),
candidate = character(0),
idx = integer(0)
) -> res
last_idx <- 0
for (cand in unique(cur$candidate)) {
tibble(
state = cur$state[1],
candidate = cand,
idx = (last_idx+1):(last_idx+1+cur[cur$candidate == cand,]$delegates)
) -> out
last_idx <- tail(out$idx, 1)
res <- bind_rows(res, out)
}
res
}) -> candidates_expanded
c(
"Biden" = "#5ac4c2",
"Sanders" = "#63bc51",
"Warren" = "#9574ae",
"Buttigieg" = "#007bb1",
"Klobuchar" = "#af973a",
"Bloomberg" = "#AA4671",
"Steyer" = "#4E4EAA",
"Yang" = "#C76C48",
"Gabbard" = "#7B8097"
) -> dcols
gsf <- left_join(delegates_sf, candidates_expanded, by = c("state", "idx"))
ggplot() +
geom_sf(
data = gsf,
aes(fill = candidate),
col = "#617a89", shape = 22, size = 2, stroke = 0.125
) +
scale_fill_manual(
name = NULL, values = dcols, na.value = "gray20",
limits = intersect(unique(delegates$candidate), names(dcols))
) +
guides(
fill = guide_legend(
override.aes = list(size = 4)
)
) +
coord_sf(datum = NA)
}

BIN
R/sysdata.rda

Binary file not shown.

11
README.Rmd

@ -39,6 +39,17 @@ packageVersion("catchpole")
```
```{r u-1, fig.width=1300/96, fig.height=700/96}
library(sf)
library(catchpole) # hrbrmstr/catchpole
library(hrbrthemes)
library(tidyverse)
gg_catchpole() +
theme_ft_rc(grid="") +
theme(legend.position = "bottom")
```
## catchpole Metrics
```{r cloc, echo=FALSE}

80
README.md

@ -0,0 +1,80 @@
[![Project Status: Active – The project has reached a stable, usable
state and is being actively
developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![Signed
by](https://img.shields.io/badge/Keybase-Verified-brightgreen.svg)](https://keybase.io/hrbrmstr)
![Signed commit
%](https://img.shields.io/badge/Signed_Commits-100%25-lightgrey.svg)
[![Linux build
Status](https://travis-ci.org/hrbrmstr/catchpole.svg?branch=master)](https://travis-ci.org/hrbrmstr/catchpole)
![Minimal R
Version](https://img.shields.io/badge/R%3E%3D-3.2.0-blue.svg)
![License](https://img.shields.io/badge/License-MIT-blue.svg)
# catchpole
catchpole title goes here otherwise CRAN checks fail
## Description
A good description goes here otherwise CRAN checks fail.
## What’s Inside The Tin
The following functions are implemented:
- `gg_catchpole`: Plot a U.S. Democratic Delegates Cargogram (2020)
- `read_delegates`: Retrieves current U.S. State delegate assignments
for 2020 from the WSJ
## Installation
``` r
remotes::install_git("https://git.rud.is/hrbrmstr/catchpole.git")
# or
remotes::install_git("https://git.sr.ht/~hrbrmstr/catchpole")
# or
remotes::install_gitlab("hrbrmstr/catchpole")
# or
remotes::install_bitbucket("hrbrmstr/catchpole")
```
NOTE: To use the ‘remotes’ install options you will need to have the
[{remotes} package](https://github.com/r-lib/remotes) installed.
## Usage
``` r
library(catchpole)
# current version
packageVersion("catchpole")
## [1] '0.1.0'
```
``` r
library(sf)
library(catchpole) # hrbrmstr/catchpole
library(hrbrthemes)
library(tidyverse)
gg_catchpole() +
theme_ft_rc(grid="") +
theme(legend.position = "bottom")
```
<img src="man/figures/README-u-1-1.png" width="1300" />
## catchpole Metrics
| Lang | \# Files | (%) | LoC | (%) | Blank lines | (%) | \# Lines | (%) |
| :--- | -------: | ---: | --: | ---: | ----------: | ---: | -------: | ---: |
| R | 5 | 0.83 | 72 | 0.83 | 27 | 0.61 | 19 | 0.39 |
| Rmd | 1 | 0.17 | 15 | 0.17 | 17 | 0.39 | 30 | 0.61 |
## Code of Conduct
Please note that this project is released with a Contributor Code of
Conduct. By participating in this project you agree to abide by its
terms.

12
data-raw/DATASET.R

@ -0,0 +1,12 @@
## code to prepare `DATASET` dataset goes here
library(sf)
library(xml2)
library(hrbrthemes)
library(tidyverse)
delegates_sf <- st_read("https://rud.is/dl/delegates.geojson", stringsAsFactors = FALSE)
usethis::use_data(delegates_sf, internal = TRUE)

BIN
man/figures/README-u-1-1.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 69 KiB

16
man/gg_catchpole.Rd

@ -0,0 +1,16 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/gg-catchpole.R
\name{gg_catchpole}
\alias{gg_catchpole}
\title{Plot a U.S. Democratic Delegates Cargogram (2020)}
\usage{
gg_catchpole(delegates = read_delegates())
}
\arguments{
\item{delegates}{a data frame with \code{state}, \code{candidate}, and
\code{delegates} count or use the default which gets the
current data as long as the WSJ keeps it alive.}
}
\description{
Plot a U.S. Democratic Delegates Cargogram (2020)
}

14
man/read_delegates.Rd

@ -0,0 +1,14 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/get-delegates.R
\name{read_delegates}
\alias{read_delegates}
\title{Retrieves current U.S. State delegate assignments for 2020 from the WSJ}
\usage{
read_delegates()
}
\description{
Retrieves current U.S. State delegate assignments for 2020 from the WSJ
}
\references{
\url{https://asset.wsj.net/wsjnewsgraphics/election/2020/delegates.json}
}
Loading…
Cancel
Save