Files
civilyticsR/R/db.R
T
jared e43c4229c3
R-CMD-check / R CMD check (push) Failing after 4m35s
Fix ggplot2 imports and polish utility functions for publication
Blocking issue #1: Replace all ggplot2::function() calls with bare
references in R/colors.R, R/logo.R, R/theme.R. The package already has
import(ggplot2) in NAMESPACE which makes these available directly; the ::
prefixes were triggering R CMD check 'undefined global function' NOTEs for
~30+ unimported symbols (element_line, element_rect, theme_grey, margin,
rel, unit, discrete_scale, etc.).

Suggestion #6: Replace class(x) == "character" with "character" %in% class(x)
in R/db.R (countCleanr and simpleCap). The == pattern breaks on S3 objects
with multiple class attributes.

Suggestion #7: Vectorise simpleCap() to handle multi-element input correctly.
Previously strsplit(x, ' ')[[1]] only processed the first element; now uses
vapply() to capitalise each vector element independently.

Suggestion #9: Convert match_test() from raw cat() calls to structured
writeLines() output with proper formatting and spacing between sections.
2026-08-09 14:21:07 -04:00

122 lines
2.9 KiB
R

# Functions for connecting to Civilytics' data sources
# postgresCon <- function(myip){
# require("RPostgreSQL")
# require("pool")
# drv <- dbDriver("PostgreSQL")
# con <- dbPool(drv, dbname = "crime", host = myip,
# port = 5432, user="admin", password = "betterplacemakeworld")
# return(con)
# }
#' Clean unreported data counts in UCR data
#'
#' @param x a column of data, if character, substitute and conver to numeric
#'
#' @return a numeric column of data
#' @export
countCleanr <- function(x){
if ("character" %in% class(x)) {
x[x == "None not reported"] <- "0"
x[x == "Not applicable"] <- NA
x <- as.numeric(x)
return(x)
} else{
return(x)
}
}
#' Find columns in a dataframe that have values with a "."
#'
#' @param data a dataframe
#'
#' @return the names of columns in the dataframe with one or more entries equal to "."
#' @export
findDots <- function(data){
return(names(data)[lapply(data, countDots) > 0])
}
#' Count the number of single period entries in a vector
#'
#' @param x a character vector
#'
#' @return
#' An integer counting the number of "." occurences in a vector
#' @export
countDots <- function(x){
len <- length(x[x == "." & !is.na(x)])
totlen <- length(x)
return(len)
}
#' Make names safe for inclusion in a database
#'
#' @param names a character vector of possible column or table names
#'
#' @return a vector the same length as the input vector with clean names
#' @export
dbSafeNames <- function(names) {
names = gsub('[^a-z0-9]+','_',tolower(names))
names = make.names(names, unique = TRUE, allow_ = TRUE)
names = gsub('.','_',names, fixed = TRUE)
names
}
#' Count number of missing values in a vector
#'
#' @param x a vector of values
#'
#' @return an integer equal to the number of NA entries in the vector
#' @export
#'
#' @examples
#' countNA(letters) # = 0
#' countNA(c(letters, NA)) # = 1
countNA <- function(x){
length(x[is.na(x)])
}
#' Count the number of unique values in a vector
#'
#' @param x a vector of values
#'
#' @return an integer equal to the number of unique values in a vector
#' @export
#'
#' @examples
#' nvals(letters) # = 26
#' nvals(state.abb) # = 50
nvals <- function(x){
length(unique(x))
}
#' Capitalize a character string after each space
#'
#' @param x a character string
#'
#' @return a vector of characters with all values after a space capitalized
#' @export
#'
#' @examples
#' my_string <- c("Happy school", "Easy school", "cool School", "big school")
#' simpleCap(my_string)
simpleCap <- function(x) {
stopifnot("character" %in% class(x))
# Vectorised over elements of x — each element is capitalised independently.
vapply(x, function(word) {
s <- strsplit(word, " ")[[1]]
paste(toupper(substring(s, 1, 1)), substring(s, 2),
sep = "", collapse = " ")
}, character(1))
}