Skip to content
This repository has been archived by the owner on Dec 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #13 from slu-openGIS/cran-release
Browse files Browse the repository at this point in the history
Cran release
  • Loading branch information
Christopher Prener authored May 2, 2019
2 parents bb2dfca + 69f9c82 commit 96c2036
Show file tree
Hide file tree
Showing 66 changed files with 1,807 additions and 292 deletions.
2 changes: 2 additions & 0 deletions CRAN-RELEASE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This package was submitted to CRAN on 2019-03-22.
Once it is accepted, delete this file and tag the release (commit e1edbe830f).
24 changes: 12 additions & 12 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
Package: compstatr
Type: Package
Title: Tools for St. Louis Crime Data
Version: 0.1.0
Version: 0.1.1
Authors@R: c(
person("Christopher", "Prener", ,"chris.prener@slu.edu", c("aut", "cre")),
person("Cree", "Foeller", ,"cree.foeller@slu.edu", c("aut")),
person("Taylor", "Braswell", , , c("com"))
)
Description: Among cities in the United States, St. Louis has the
distinction of having the highest or one of the highest violent crime
and homicide rates since 2010. It therefore presents an important site
for social scientists, public health researchers, and health care
providers as well as policy makers to understand the effects of violent
crime on urban communities. The City's crime data, however, are difficult
to work with and present a number of challenges for researchers. We therefore provide
a set of tools for accessing and editing St. Louis Metropolitan Police
Department (SLMPD) crime data, which are available on their website in csv form.
The categorization tools that are provided will work with any police department
that uses 5 and 6 digit numeric codes to identify specific crimes.
Description: Provides a set of tools for creating yearly data sets of St. Louis
Metropolitan Police Department (SLMPD) crime data, which are available from
January 2008 onward as monthly CSV releases on their website
(<http:www.slmpd.org/Crimereports.shtml>). Once data are validated and created
(monthly data releases have varying numbers of columns
as well as different column names and formats), 'compstatr' also provides
functions for categorizing and mapping crimes in St. Louis. The categorization
tools that are provided will also work with any police department that uses 5
and 6 digit numeric codes to identify specific crimes. These data provide researchers
and policy makers detailed data for St. Louis, which in the last several years
has had some of the highest or the highest violent crime rates in the United States.
Depends: R (>= 3.3)
License: GPL-3
URL: https://github.com/slu-openGIS/compstatr
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Generated by roxygen2: do not edit by hand

export(cs_address)
export(cs_collapse)
export(cs_combine)
export(cs_crime)
export(cs_crime_cat)
export(cs_example)
export(cs_extract_month)
export(cs_filter_count)
export(cs_filter_crime)
Expand All @@ -28,6 +30,7 @@ importFrom(dplyr,is.tbl)
importFrom(dplyr,mutate)
importFrom(dplyr,rename)
importFrom(dplyr,select)
importFrom(fs,file_copy)
importFrom(fs,file_move)
importFrom(lubridate,date)
importFrom(lubridate,parse_date_time)
Expand All @@ -51,4 +54,5 @@ importFrom(stringr,str_c)
importFrom(stringr,str_detect)
importFrom(stringr,str_replace)
importFrom(stringr,str_sub)
importFrom(stringr,str_trim)
importFrom(tidyr,separate)
15 changes: 15 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# compstatr 0.1.1

* **initial CRAN submission**
* add Zenodo release badge to `README`
* update installation instructions on `README` and on the `pkgdown` site
* add `cran-comments.md`
* in response to initial feedback from Matthias Sterrer (CRAN):
* add `cs_example()` function for creating a sample year worth of `.csv` files
* add executable examples for `cs_prep_year()`, `cs_load_year()`, and `cs_projectXY`
* clarify `DESCRIPTION` for package
* in response to additional feedback from Swetlana Herbrandt (CRAN):
* added a link to the `DESCRIPTION` file to the SLMPD data source
* ensured that the examples in `example.R` and `create.R` read from and write to a temporary directory
* add a function `cs_address()` to facilitate concatenation of street addresses prior to geocoding

# compstatr 0.1.0

* Added a `NEWS.md` file to track changes to the package.
Expand Down
93 changes: 93 additions & 0 deletions R/address.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#' Create a Single Address Field
#'
#' @description The street address data in SLMPD releases (either \code{ILEADSAddress} and
#' \code{ILEADSStreet} or \code{CADAddress} and \code{CADStreet}) are stored in separate
#' columns. In order to faciliate geocoding, this function combines the fields and
#' removes inappropriate characters in the address fields.
#'
#' @usage cs_address(.data, address, street, newVar)
#'
#' @param .data A tibble or data frame
#' @param address Name of address number variable (typically either \code{ILEADSAddress}
#' or \code{CADAddress})
#' @param street Name of street name variable (typically either \code{ILEADSStreet}
#' or \code{CADStreet})
#' @param newVar Name of new variable to store concatenated address
#'
#' @return A copy of the object with a character vector that contains the concatenated
#' street address data.
#'
#' @examples
#' # load example data
#' testData <- january2018
#'
#' # add concatenated address variable
#' testData <- cs_address(testData, address = ILEADSAddress, street = ILEADSStreet, newVar = address)
#'
#' @importFrom dplyr mutate
#' @importFrom rlang :=
#' @importFrom rlang quo
#' @importFrom rlang enquo
#' @importFrom rlang sym
#' @importFrom stringr str_c
#' @importFrom stringr str_detect
#' @importFrom stringr str_replace
#' @importFrom stringr str_trim
#'
#' @export
cs_address <- function(.data, address, street, newVar){

# check for missing parameters
if (missing(.data)) {
stop("An existing data frame with crime data must be specified for '.data'.")
}

if (missing(address)) {
stop("The column containing address numbers must be specified for 'address'.")
}

if (missing(street)) {
stop("The column containing street names must be specified for 'street'.")
}

if (missing(newVar)) {
stop("A new variable name must be specified for 'newVar'.")
}

#save parameters to list
paramList <- as.list(match.call())

#quote input variables
if (!is.character(paramList$address)) {
addressQ <- rlang::enquo(address)
} else if (is.character(paramList$address)) {
addressQ <- rlang::quo(!! rlang::sym(address))
}

if (!is.character(paramList$street)) {
streetQ <- rlang::enquo(street)
} else if (is.character(paramList$street)) {
streetQ <- rlang::quo(!! rlang::sym(street))
}

if (!is.character(paramList$newVar)) {
newVarQ <- rlang::enquo(newVar)
} else if (is.character(paramList$newVar)) {
newVarQ <- rlang::quo(!! rlang::sym(newVar))
}

newVarQN <- rlang::quo_name(rlang::enquo(newVar))

# concatenate address
.data <- dplyr::mutate(.data, !!newVarQN := stringr::str_c(!!addressQ, !!streetQ, sep = " "))

# clean address
.data <- dplyr::mutate(.data, !!newVarQN := ifelse(stringr::str_detect(string = !!newVarQ, pattern = "^[0\\b]") == TRUE,
stringr::str_replace(string = !!newVarQ, pattern = "^[0\\b]", replacement = ""),
address))
.data <- dplyr::mutate(.data, !!newVarQN := stringr::str_trim(!!newVarQ))

# return output
return(.data)

}
2 changes: 1 addition & 1 deletion R/categorize.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
#' @param newVar Name of output variable to be created with logical data
#' @param crime A string describing the crime type to be identified
#'
#' @return A cope of the object with a logical vector that is \code{TRUE} if the given crime matches
#' @return A copy of the object with a logical vector that is \code{TRUE} if the given crime matches
#' the category given in the function.
#'
#' @examples
Expand Down
54 changes: 48 additions & 6 deletions R/create.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,36 @@
#' specified path.
#'
#' @examples
#' \dontrun{
#' cs_prep_year(path = "data/raw/2008")
#' }
#' # create temporary directory
#' tmpdir <- tempdir()
#' fs::dir_create(paste0(tmpdir,"/data/"))
#'
#' # load sample files into temporary directory
#' cs_example(path = paste0(tmpdir,"/data/"))
#'
#' # list files
#' list.files(paste0(tmpdir,"/data/"))
#'
#' # prep sample files
#' cs_prep_year(path = paste0(tmpdir,"/data/"))
#'
#' # list files again
#' list.files(paste0(tmpdir,"/data/"))
#'
#' # delete data
#' fs::dir_delete(paste0(tmpdir,"/data/"))
#'
#' # create temporary directory
#' fs::dir_create(paste0(tmpdir,"/data/"))
#'
#' # load sample files into temporary directory
#' cs_example(path = paste0(tmpdir,"/data/"))
#'
#' # prep sample files
#' cs_prep_year(path = paste0(tmpdir,"/data/"), verbose = TRUE)
#'
#' # delete data again
#' fs::dir_delete(paste0(tmpdir,"/data/"))
#'
#' @importFrom dplyr as_tibble
#' @importFrom dplyr filter
Expand Down Expand Up @@ -141,9 +168,24 @@ cs_edit_filename <- function(path, file){
#' of crime data stored within a list.
#'
#' @examples
#' \dontrun{
#' yearList08 <- cs_load_year(path = "data/raw/2008")
#' }
#' # create temporary directory
#' tmpdir <- tempdir()
#' fs::dir_create(paste0(tmpdir,"/data/"))
#'
#' # load sample files into temporary directory
#' cs_example(path = paste0(tmpdir,"/data/"))
#'
#' # prep sample files
#' cs_prep_year(path = paste0(tmpdir,"/data/"))
#'
#' # load sample files
#' yearList17 <- cs_load_year(path = paste0(tmpdir,"/data/"))
#'
#' # delete data
#' fs::dir_delete(paste0(tmpdir,"/data/"))
#'
#' # print year-list object
#' yearList17
#'
#' @importFrom dplyr %>%
#' @importFrom purrr map
Expand Down
79 changes: 79 additions & 0 deletions R/example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#' Load Example Files
#'
#' @description Adds a sample set of twelve files, one for each month of 2017,
#' to the specified path. These are not full data files; each file has twenty
#' observations. They can be used to practice preparing, loading, standardizing,
#' and collapsing data.
#'
#' @param path File path where example data should be placed
#' @param overwrite Overwrite files if they exist. If this is FALSE and the file exists
#' an error will be thrown.
#'
#' @examples
#' # create temporary directory
#' tmpdir <- tempdir()
#' fs::dir_create(paste0(tmpdir,"/data/"))
#'
#' # load sample files into temporary directory
#' cs_example(path = paste0(tmpdir,"/data/"))
#'
#' # list files
#' list.files(paste0(tmpdir,"/data/"))
#'
#' # delete data
#' fs::dir_delete(paste0(tmpdir,"/data/"))
#'
#' @importFrom fs file_copy
#'
#' @export
cs_example <- function(path, overwrite = FALSE){

# copy file, january
fs::file_copy(system.file("extdata", "January2017.csv.html", package = "compstatr", mustWork = TRUE),
paste0(path,"/January2017.CSV.html"), overwrite = overwrite)

# copy file, february
fs::file_copy(system.file("extdata", "February2017.csv.html", package = "compstatr", mustWork = TRUE),
paste0(path,"/February2017.CSV.html"), overwrite = overwrite)

# copy file, march
fs::file_copy(system.file("extdata", "March2017.csv.html", package = "compstatr", mustWork = TRUE),
paste0(path,"/March2017.CSV.html"), overwrite = overwrite)

# copy file, april
fs::file_copy(system.file("extdata", "April2017.csv.html", package = "compstatr", mustWork = TRUE),
paste0(path,"/April2017.CSV.html"), overwrite = overwrite)

# copy file, may
fs::file_copy(system.file("extdata", "May2017.csv.html", package = "compstatr", mustWork = TRUE),
paste0(path,"/May2017.CSV.html"), overwrite = overwrite)

# copy file, june
fs::file_copy(system.file("extdata", "June2017.csv.html", package = "compstatr", mustWork = TRUE),
paste0(path,"/June2017.CSV.html"), overwrite = overwrite)

# copy file, july
fs::file_copy(system.file("extdata", "July2017.csv.html", package = "compstatr", mustWork = TRUE),
paste0(path,"/July2017.CSV.html"), overwrite = overwrite)

# copy file, august
fs::file_copy(system.file("extdata", "August2017.csv.html", package = "compstatr", mustWork = TRUE),
paste0(path,"/August2017.CSV.html"), overwrite = overwrite)

# copy file, september
fs::file_copy(system.file("extdata", "September2017.csv.html", package = "compstatr", mustWork = TRUE),
paste0(path,"/September2017.CSV.html"), overwrite = overwrite)

# copy file, october
fs::file_copy(system.file("extdata", "October2017.csv.html", package = "compstatr", mustWork = TRUE),
paste0(path,"/October2017.CSV.html"), overwrite = overwrite)

# copy file, november
fs::file_copy(system.file("extdata", "November2017.csv.html", package = "compstatr", mustWork = TRUE),
paste0(path,"/November2017.CSV.html"), overwrite = overwrite)

# copy file, december
fs::file_copy(system.file("extdata", "December2017.csv.html", package = "compstatr", mustWork = TRUE),
paste0(path,"/December2017.CSV.html"), overwrite = overwrite)

}
1 change: 1 addition & 0 deletions R/spatial.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ cs_missingXY <- function(.data, varX, varY, newVar){
#'
#' @return A \code{sf} object with the crime data projected for mapping.
#'
#' @examples
#' # load example data
#' testData <- january2018
#'
Expand Down
10 changes: 8 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ The City's crime data, however, are difficult to work with and present a number
We therefore provide a set of tools for accessing, preparing, editing, and mapping St. Louis [Metropolitan Police Department](http://www.slmpd.org) (SLMPD) crime data, which are available [on their website](http://www.slmpd.org/Crimereports.shtml) as `.csv` files. The categorization tools that are provided will work with any police department that uses 5 and 6 digit numeric codes to identify specific crimes.

## Installation
You can install compstatr from Github with `remotes`:
The easiest way to get `compstatr` is to install it from CRAN:

```{r gh-installation, eval = FALSE}
``` r
install.packages("compstatr")
```

The development version of `compstatr` can be accessed from GitHub with `remotes`:

```r
# install.packages("remotes")
remotes::install_github("slu-openGIS/compstatr")
```
Expand Down
2 changes: 2 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ reference:
- title: "Spatial Data"
desc: "Wrangle and Project Crimes"
contents:
- cs_address
- cs_replace0
- cs_missingXY
- cs_projectXY

- title: "Sample Data"
desc: "Example Data for Practicing Wrangling and Mapping"
contents:
- cs_example
- january2018
Loading

0 comments on commit 96c2036

Please sign in to comment.