install logo correctly

This commit is contained in:
Jared Knowles
2020-07-06 11:48:10 -04:00
parent f5f288974f
commit 20f65a7126
32 changed files with 582 additions and 95 deletions
+13 -3
View File
@@ -4,11 +4,21 @@ Title: Utilities Functions for Civilytics
Version: 0.1.0
Author: Jared E. Knowles <jared@civilytics.com>
Maintainer: Jared E. Knowles <jared@civilytics.com>
Description: More about what it does (maybe more than one line)
Use four spaces when indenting paragraphs within the Description.
Description: House R functions for Civilytics Consulting LLC
This package implements a variety of useful functions for creating and
branding analyses produced by Civilytics Consulting LLC.
License: LICENSE
Depends:
R (>= 2.15.1)
Imports:
ggplot2,
jpeg,
png,
stringr,
gridExtra,
grid
Encoding: UTF-8
LazyData: true
Suggests:
testthat
RoxygenNote: 7.0.2
RoxygenNote: 7.1.0
+10
View File
@@ -2,18 +2,28 @@
export(add_logo)
export(add_logo_ga)
export(countCleanr)
export(countDots)
export(countNA)
export(dbSafeNames)
export(findDots)
export(get_png)
export(has_caption)
export(make_logo_grob)
export(measure_caption)
export(na_zero)
export(nvals)
export(plot_jpeg)
export(pretty_count)
export(pretty_per)
export(safe_max)
export(simpleCap)
export(theme_civilytics)
import(ggplot2)
importFrom(ggplot2,theme)
importFrom(graphics,plot)
importFrom(graphics,rasterImage)
importFrom(grid,grid.draw)
importFrom(grid,rasterGrob)
importFrom(gridExtra,arrangeGrob)
importFrom(jpeg,readJPEG)
+122
View File
@@ -0,0 +1,122 @@
# 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(class(x) == "character"){
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
#'
#' @examples
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
#' @export
#'
#' @examples
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
#'
#' @examples
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(class(x) == "character")
s <- strsplit(x, " ")[[1]]
paste(toupper(substring(s, 1,1)), substring(s, 2),
sep = "", collapse = " ")
}
+49 -30
View File
@@ -1,19 +1,19 @@
#' Title
#' Plot a jpeg image as a raster
#'
#' @param path
#' @param add
#' @param upscale
#'
#' @return
#' @param path a character representing the relative or absolute path to the image
#' @param add a logical, TRUE to add the image to an existing plot, FALSE creates a new plot
#' @param upscale a logical, TRUE upscales the resolution of the graphic by a factor of 3, FALSE
#' preserves the resolution
#' @return a rasterImage
#' @export
#' @importFrom jpeg readJPEG
#' @importFrom graphics rasterImage
#' @examples
#' img <- readJPEG(system.file("img","Rlogo.jpg",package="jpeg"))
#' img <- system.file("img","Civilytics Consulting Logo.jpg",package="civilytics")
#' plot_jpeg(img)
plot_jpeg <- function(path, add=FALSE, upscale = TRUE)
{
require('jpeg')
jpg = readJPEG(path, native=T) # read the file
jpg = readJPEG(path, native = T) # read the file
res = dim(jpg)[2:1] # get the resolution, [x, y]
if (upscale){
res <- res * 3
@@ -26,9 +26,9 @@ plot_jpeg <- function(path, add=FALSE, upscale = TRUE)
}
#' Title
#' Plot a PNG file as a rasterGrob for inclusion in ggplot2
#'
#' @param filename
#' @param filename a character with file path to a png file
#'
#' @return
#' @export
@@ -36,16 +36,17 @@ plot_jpeg <- function(path, add=FALSE, upscale = TRUE)
#' @importFrom grid rasterGrob
#'
#' @examples
#'
get_png <- function(filename) {
grid::rasterGrob(png::readPNG(filename), interpolate = TRUE)
}
#' Title
#' Add a logo to a ggplot2 object
#'
#' @param plot
#' @param logo
#' @param margin_param
#' @param plot a ggplot2 grob
#' @param logo a logo grob created by make_logo_grob()
#' @param margin_param a numeric specifying what margin to add or subtract to align the logo
#'
#' @return
#' @importFrom ggplot2 theme
@@ -73,15 +74,18 @@ add_logo <- function(plot, logo, margin_param = NULL) {
padding = unit(0.1, "line"))
}
#' Title
#' Measure a ggplot2 object caption
#'
#' @param gg
#' @param gg a ggplot object
#'
#' @return
#' @return a numeric value stating the number of lines to be added or subtracted to align a logo with
#' the caption
#' @importFrom stringr str_count
#' @export
#'
#' @examples
#' p1 <- qplot(mpg, wt, data = mtcars)
#' measure_caption(p1) # Should equal 1 since no caption is required
measure_caption <- function(gg) {
if (has_caption(gg)) {
stringr::str_count(gg$labels$caption, pattern = "\n") + 1
@@ -91,32 +95,43 @@ measure_caption <- function(gg) {
}
#' Title
#' Test whether a ggplot2 object has a caption
#'
#' @param gg
#' @param gg a gg object from ggplot2
#'
#' @return
#' @return a logical, TRUE if a caption exists and FALSE if it does not
#' @import ggplot2
#' @export
#'
#' @examples
#' p1 <- ggplot2::qplot(mpg, wt, data = mtcars)
#' has_caption(p1) # FALSE
has_caption <- function(gg) {
any(names(gg$labels) == "caption")
}
#' Title
#' Add a logo to a ggplot2 object
#'
#' @param plot_list
#' @param logo
#' @param nrow
#' @param widths
#' @param margin_param
#' @param plot_list a list containing ggplot2 objects
#' @param logo a grob containing the logo created with `make_logo_grob`
#' @param nrow an integer, default = 1, for the number of rows to align the plots in
#' @param widths an optional vector the same length as plot_list with the widths for each plot
#' @param margin_param a number giving the adjustment up or down to help manually align logo and captions
#'
#' @return
#' @note The resulting object needs to be drawn to the screen using grid.draw()
#' @importFrom gridExtra arrangeGrob
#' @importFrom ggplot2 theme
#' @importFrom grid grid.draw
#' @export
#'
#' @examples
#' library(ggplot2); library(grid)
#' tmp_plot <- ggplot(mtcars) + aes(x = hp, y = disp) + geom_point() + theme_civilytics()
#' tmp_logo <- make_logo_grob()
#' plot_and_logo <- add_logo(tmp_plot, tmp_logo)
#' grid.draw(plot_and_logo)
#' dev.off()
add_logo_ga <- function(plot_list, logo, nrow = 1, widths = NULL, margin_param = NULL) {
# Change position of logo depending on if plot has a caption
if (!is.null(margin_param)) {
@@ -146,14 +161,18 @@ add_logo_ga <- function(plot_list, logo, nrow = 1, widths = NULL, margin_param =
#' Get a Civilytics Logo grob
#'
#' @return
#' @return a gg object which contains the logo file stored as a Grob suitable for manipulating in
#' grid
#' @export
#'
#' @import ggplot2
#' @examples
#' logo <- make_logo_grob()
#' class(logo) # gg
make_logo_grob <- function() {
logo_grob <- ggplot(mapping = aes(x = 0:1, y = 1)) +
theme_void() +
annotation_custom(get_png(system.file("img", "civilytics_logo.png",
package="civilytics")), xmin = 0.825, xmax = 1)
package="civilytics")), xmin= 0.7, xmax = 1)
logo_grob
}
+9 -12
View File
@@ -1,17 +1,14 @@
#' Title
#' Make the Civilytics plot theme
#'
#' @param font_size
#' @param font_family
#' @param line_size
#' @param rel_small
#' @param rel_tiny
#' @param rel_large
#'
#' @return
#' @param font_size default 14, a number representing the base font for the theme
#' @param font_family default "", a character for the font family to use in the theme
#' @param line_size default 0.5, the line size to use for the theme
#' @param rel_small default 12/14, the scale factor to create a small font from the base font_size
#' @param rel_tiny default 11/14, the scale factor to create a tiny font from the base font_size
#' @param rel_large default 16/14, the scale factor to create a large font from the base font_size
#' @importFrom graphics plot
#' @return a ggplot2 theme object suitable for combining with ggplot objects to theme them
#' @export
#'
#' @examples
theme_civilytics <-
function (font_size = 14,
font_family = "",
+13 -8
View File
@@ -8,33 +8,38 @@
#'
#' @examples
#' safe_max(1:10)
#' safe_max(c(1:10, NA)) # 10
#' safe_max(c(1:10, NA))# 10
safe_max <- function(x) {
max(x, na.rm = TRUE)
if (all(is.na(x))) {
return(NA)
} else {
max(x, na.rm = TRUE)
}
}
#' Prettify proportion data
#' Make data expresed as proportions print out as prettily formatted characters with percentages
#'
#' @param x a numeric vector of proportions to convert to percentages
#' @param ndigit a vector of length 1 indicating the number of digits to display in the percent
#' @param ndigit a vector of length 1 indicating the number of digits to display in the percent, by
#' default it represents the digits after the decimal point in the percentage form
#'
#' @return
#' @return a character vector with a % attached
#' @export
#'
#' @examples
#' pretty_per(0.2, ndigit = 1)
#' pretty_per(c(0.2, 0.332423, 0.4, 0.342342), ndigit = 2)
pretty_per <- function(x, ndigit = 1) {
x <- format(round(x, digits = 3) * 100, nsmall = ndigit)
if (any(x >= 100) & !all(is.na(x))) {
message("Values over 100 found, did you mean to use proportions?")
}
x <- format(round(x, digits = ndigit + 2) * 100, nsmall = ndigit)
x <- paste0(x, "%")
x[x == "NA%"] <- " - "
x <- trimws(x)
return(x)
}
options(knitr.kable.NA = "-")
#' Zero out missing values
+1
View File
@@ -18,3 +18,4 @@ StripTrailingWhitespace: Yes
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
Binary file not shown.

Before

Width:  |  Height:  |  Size: 50 KiB

Before

Width:  |  Height:  |  Size: 217 KiB

After

Width:  |  Height:  |  Size: 217 KiB

Before

Width:  |  Height:  |  Size: 89 KiB

After

Width:  |  Height:  |  Size: 89 KiB

+7 -3
View File
@@ -2,16 +2,20 @@
% Please edit documentation in R/logo.R
\name{add_logo}
\alias{add_logo}
\title{Title}
\title{Add a logo to a ggplot2 object}
\usage{
add_logo(plot, logo, margin_param = NULL)
}
\arguments{
\item{margin_param}{}
\item{plot}{a ggplot2 grob}
\item{logo}{a logo grob created by make_logo_grob()}
\item{margin_param}{a numeric specifying what margin to add or subtract to align the logo}
}
\value{
}
\description{
Title
Add a logo to a ggplot2 object
}
+22 -3
View File
@@ -2,16 +2,35 @@
% Please edit documentation in R/logo.R
\name{add_logo_ga}
\alias{add_logo_ga}
\title{Title}
\title{Add a logo to a ggplot2 object}
\usage{
add_logo_ga(plot_list, logo, nrow = 1, widths = NULL, margin_param = NULL)
}
\arguments{
\item{margin_param}{}
\item{plot_list}{a list containing ggplot2 objects}
\item{logo}{a grob containing the logo created with `make_logo_grob`}
\item{nrow}{an integer, default = 1, for the number of rows to align the plots in}
\item{widths}{an optional vector the same length as plot_list with the widths for each plot}
\item{margin_param}{a number giving the adjustment up or down to help manually align logo and captions}
}
\value{
}
\description{
Title
Add a logo to a ggplot2 object
}
\note{
The resulting object needs to be drawn to the screen using grid.draw()
}
\examples{
library(ggplot2); library(grid)
tmp_plot <- ggplot(mtcars) + aes(x = hp, y = disp) + geom_point() + theme_civilytics()
tmp_logo <- make_logo_grob()
plot_and_logo <- add_logo(tmp_plot, tmp_logo)
grid.draw(plot_and_logo)
dev.off()
}
+17
View File
@@ -0,0 +1,17 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/db.R
\name{countCleanr}
\alias{countCleanr}
\title{Clean unreported data counts in UCR data}
\usage{
countCleanr(x)
}
\arguments{
\item{x}{a column of data, if character, substitute and conver to numeric}
}
\value{
a numeric column of data
}
\description{
Clean unreported data counts in UCR data
}
+17
View File
@@ -0,0 +1,17 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/db.R
\name{countDots}
\alias{countDots}
\title{Count the number of single period entries in a vector}
\usage{
countDots(x)
}
\arguments{
\item{x}{a character vector}
}
\value{
}
\description{
Count the number of single period entries in a vector
}
+21
View File
@@ -0,0 +1,21 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/db.R
\name{countNA}
\alias{countNA}
\title{Count number of missing values in a vector}
\usage{
countNA(x)
}
\arguments{
\item{x}{a vector of values}
}
\value{
an integer equal to the number of NA entries in the vector
}
\description{
Count number of missing values in a vector
}
\examples{
countNA(letters) # = 0
countNA(c(letters, NA)) # = 1
}
+17
View File
@@ -0,0 +1,17 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/db.R
\name{dbSafeNames}
\alias{dbSafeNames}
\title{Make names safe for inclusion in a database}
\usage{
dbSafeNames(names)
}
\arguments{
\item{names}{a character vector of possible column or table names}
}
\value{
a vector the same length as the input vector with clean names
}
\description{
Make names safe for inclusion in a database
}
+17
View File
@@ -0,0 +1,17 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/db.R
\name{findDots}
\alias{findDots}
\title{Find columns in a dataframe that have values with a "."}
\usage{
findDots(data)
}
\arguments{
\item{data}{a dataframe}
}
\value{
the names of columns in the dataframe with one or more entries equal to "."
}
\description{
Find columns in a dataframe that have values with a "."
}
+6 -3
View File
@@ -2,16 +2,19 @@
% Please edit documentation in R/logo.R
\name{get_png}
\alias{get_png}
\title{Title}
\title{Plot a PNG file as a rasterGrob for inclusion in ggplot2}
\usage{
get_png(filename)
}
\arguments{
\item{filename}{}
\item{filename}{a character with file path to a png file}
}
\value{
}
\description{
Title
Plot a PNG file as a rasterGrob for inclusion in ggplot2
}
\examples{
}
+8 -4
View File
@@ -2,16 +2,20 @@
% Please edit documentation in R/logo.R
\name{has_caption}
\alias{has_caption}
\title{Title}
\title{Test whether a ggplot2 object has a caption}
\usage{
has_caption(gg)
}
\arguments{
\item{gg}{}
\item{gg}{a gg object from ggplot2}
}
\value{
a logical, TRUE if a caption exists and FALSE if it does not
}
\description{
Title
Test whether a ggplot2 object has a caption
}
\examples{
p1 <- ggplot2::qplot(mpg, wt, data = mtcars)
has_caption(p1) # FALSE
}
-12
View File
@@ -1,12 +0,0 @@
\name{hello}
\alias{hello}
\title{Hello, World!}
\usage{
hello()
}
\description{
Prints 'Hello, world!'.
}
\examples{
hello()
}
+6 -1
View File
@@ -7,8 +7,13 @@
make_logo_grob()
}
\value{
a gg object which contains the logo file stored as a Grob suitable for manipulating in
grid
}
\description{
Get a Civilytics Logo grob
}
\examples{
logo <- make_logo_grob()
class(logo) # gg
}
+9 -4
View File
@@ -2,16 +2,21 @@
% Please edit documentation in R/logo.R
\name{measure_caption}
\alias{measure_caption}
\title{Title}
\title{Measure a ggplot2 object caption}
\usage{
measure_caption(gg)
}
\arguments{
\item{gg}{}
\item{gg}{a ggplot object}
}
\value{
a numeric value stating the number of lines to be added or subtracted to align a logo with
the caption
}
\description{
Title
Measure a ggplot2 object caption
}
\examples{
p1 <- qplot(mpg, wt, data = mtcars)
measure_caption(p1) # Should equal 1 since no caption is required
}
+21
View File
@@ -0,0 +1,21 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/db.R
\name{nvals}
\alias{nvals}
\title{Count the number of unique values in a vector}
\usage{
nvals(x)
}
\arguments{
\item{x}{a vector of values}
}
\value{
an integer equal to the number of unique values in a vector
}
\description{
Count the number of unique values in a vector
}
\examples{
nvals(letters) # = 26
nvals(state.abb) # = 50
}
+11 -5
View File
@@ -2,19 +2,25 @@
% Please edit documentation in R/logo.R
\name{plot_jpeg}
\alias{plot_jpeg}
\title{Title}
\title{Plot a jpeg image as a raster}
\usage{
plot_jpeg(path, add = FALSE, upscale = TRUE)
}
\arguments{
\item{upscale}{}
\item{path}{a character representing the relative or absolute path to the image}
\item{add}{a logical, TRUE to add the image to an existing plot, FALSE creates a new plot}
\item{upscale}{a logical, TRUE upscales the resolution of the graphic by a factor of 3, FALSE
preserves the resolution}
}
\value{
a rasterImage
}
\description{
Title
Plot a jpeg image as a raster
}
\examples{
img <- readJPEG(system.file("img","Rlogo.jpg",package="jpeg"))
img <- system.file("img","Civilytics Consulting Logo.jpg",package="civilytics")
plot_jpeg(img)
}
+3 -2
View File
@@ -10,10 +10,11 @@ pretty_per(x, ndigit = 1)
\arguments{
\item{x}{a numeric vector of proportions to convert to percentages}
\item{ndigit}{a vector of length 1 indicating the number of digits to display in the percent}
\item{ndigit}{a vector of length 1 indicating the number of digits to display in the percent, by
default it represents the digits after the decimal point in the percentage form}
}
\value{
a character vector with a % attached
}
\description{
Prettify proportion data
+1 -1
View File
@@ -19,5 +19,5 @@ Take the max but ignore missing values
}
\examples{
safe_max(1:10)
safe_max(c(1:10, NA)) # 10
safe_max(c(1:10, NA))# 10
}
+21
View File
@@ -0,0 +1,21 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/db.R
\name{simpleCap}
\alias{simpleCap}
\title{Capitalize a character string after each space}
\usage{
simpleCap(x)
}
\arguments{
\item{x}{a character string}
}
\value{
a vector of characters with all values after a space capitalized
}
\description{
Capitalize a character string after each space
}
\examples{
my_string <- c("Happy school", "Easy school", "cool School", "big school")
simpleCap(my_string)
}
+14 -4
View File
@@ -2,7 +2,7 @@
% Please edit documentation in R/theme.R
\name{theme_civilytics}
\alias{theme_civilytics}
\title{Title}
\title{Make the Civilytics plot theme}
\usage{
theme_civilytics(
font_size = 14,
@@ -14,11 +14,21 @@ theme_civilytics(
)
}
\arguments{
\item{rel_large}{}
\item{font_size}{default 14, a number representing the base font for the theme}
\item{font_family}{default "", a character for the font family to use in the theme}
\item{line_size}{default 0.5, the line size to use for the theme}
\item{rel_small}{default 12/14, the scale factor to create a small font from the base font_size}
\item{rel_tiny}{default 11/14, the scale factor to create a tiny font from the base font_size}
\item{rel_large}{default 16/14, the scale factor to create a large font from the base font_size}
}
\value{
a ggplot2 theme object suitable for combining with ggplot objects to theme them
}
\description{
Title
Make the Civilytics plot theme
}
+82
View File
@@ -0,0 +1,82 @@
# Test db
context("DB Utilities - safe Cap")
test_that("simpleCap has proper error for non-character data", {
expect_error(simpleCap(2))
expect_error(simpleCap(TRUE))
expect_error(simpleCap(factor("cat")))
})
test_that("simpleCap capitalizes characters", {
expect_equal(simpleCap("my car"), "My Car")
expect_equal(simpleCap("car"), "Car")
expect_equal(simpleCap("car_2"), "Car_2")
})
context("DB Utilities - Count NA")
test_that("countNA works on character vectors", {
expect_equal(countNA(c("A", "B", NA, "C")), 1)
expect_equal(countNA(rep(NA_character_, 100)), 100)
expect_equal(countNA(c("car", "dog", "house", NA, NA, NA, NA)), 4)
})
test_that("countNA returns 0 when no NAs are found", {
expect_equal(countNA(rnorm(100)), 0)
expect_equal(countNA(LETTERS), 0)
expect_equal(countNA(state.abb), 0)
})
context("DB Utilities - nvals")
test_that("nvals works for character values", {
expect_equal(nvals(LETTERS), 26)
expect_equal(nvals(state.abb), 50)
})
test_that("nvals works with missing values", {
expect_equal(nvals(c(letters, NA)), 27)
expect_equal(nvals(c(letters, NA, NA)), 27)
expect_equal(nvals(rep(NA, 100)), 1)
})
context("DB Utilities - dbSafeNames")
test_that("dbsafenames does not change all lowercase", {
char <- "alligator"
expect_equal(dbSafeNames(char), char)
char <- "hotdog"
expect_equal(dbSafeNames(char), char)
})
test_that("dbsafenames replaces spaces", {
char_old <- "hot dog"
char_new <- "hot_dog"
expect_equal(dbSafeNames(char_old), char_new)
char_old <- "alligator cat"
char_new <- "alligator_cat"
expect_equal(dbSafeNames(char_old), char_new)
})
test_that("dbsafenames replaces periods", {
char_old <- "hot.dog"
char_new <- "hot_dog"
expect_equal(dbSafeNames(char_old), char_new)
char_old <- "alligator.cat"
char_new <- "alligator_cat"
expect_equal(dbSafeNames(char_old), char_new)
})
test_that("dbsafenames replaces forces nocaps", {
char_old <- "HoTDoG"
char_new <- "hotdog"
expect_equal(dbSafeNames(char_old), char_new)
char_old <- "AlLiGaTorCaT"
char_new <- "alligatorcat"
expect_equal(dbSafeNames(char_old), char_new)
})
+12
View File
@@ -0,0 +1,12 @@
# context("Histograms")
#
# library(ggplot2)
# disp_hist_base <- function() hist(mtcars$disp)
# disp_hist_ggplot <- ggplot(mtcars, aes(disp)) + geom_histogram()
#
# test_that({
# skip("Not currently working")
# vdiffr::expect_doppelganger("Base graphics histogram", disp_hist_base)
# vdiffr::expect_doppelganger("ggplot2 histogram", disp_hist_ggplot)
#
# })
+53
View File
@@ -0,0 +1,53 @@
# Test utils
context("Test Utilities - Safe Max")
test_that("safe_max excludes NAs", {
samp_a <- c(1:10, NA)
expect_equal(safe_max(samp_a), 10)
expect_equal(safe_max(samp_a), max(samp_a, na.rm = TRUE))
expect_equal(safe_max(c("cat", "box", "dog")), "dog")
expect_equal(safe_max(c("cat", "box", "dog")), max(c("cat", "box", "dog"), na.rm = TRUE))
})
test_that("safe_max handles all NAs", {
expect_equal(safe_max(rep(NA, 100)), NA)
expect_equal(safe_max(NULL), NA)
})
context("Test Utilities - Pretty Per")
test_that("pretty_per respects rounding", {
expect_identical(pretty_per(0.2), "20.0%")
expect_identical(pretty_per(0.2432522), "24.3%")
expect_identical(pretty_per(0.2432522, ndigit = 3), "24.325%")
})
test_that("pretty_per handles NAs", {
expect_equivalent(pretty_per(rep(NA, 5)), c("-", "-", "-", "-", "-"))
})
test_that("pretty_per deals with large numbers", {
expect_message(pretty_per(c(0.2, 0.5, 0.1, 110)))
})
context("Test Utilities - NA Zero")
test_that("Function works when no NAs present", {
expect_equivalent(na_zero(1:10), 1:10)
expect_equivalent(na_zero(letters), letters)
})
test_that("Function subs out NAs in numeric vectors with 0", {
expect_equivalent(na_zero(c(1:10, NA)), c(1:10, 0))
})
context("Test Utilities - Pretty Count")