Skip to content

Commit

Permalink
* f_replace added as a ggplot2 scale gsub convenience function.
Browse files Browse the repository at this point in the history
* `f_title` added as a ggplot2 scale `tools::toTitleCase` convenience function.

* `f_title` added as a ggplot2 scale `strwrap` + `paste(collapse =TRUE)`
  convenience function.
  • Loading branch information
Tyler Rinker committed Aug 30, 2017
1 parent 8535525 commit 7d43a42
Show file tree
Hide file tree
Showing 14 changed files with 682 additions and 169 deletions.
43 changes: 35 additions & 8 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,44 @@ Title: Tools to Format Numbers for Publication
Version: 0.1.1
Authors@R: c(person("Tyler", "Rinker", email = "tyler.rinker@gmail.com", role = c("aut", "cre")))
Maintainer: Tyler Rinker <tyler.rinker@gmail.com>
Description: Format numbers for publication; includes the removal of leading zeros, standardization of number of digits, addition of affixes, and
a p-value formatter. These tools combine the functionality of several 'base' functions such as paste(), format(), and sprintf()
into specific use case functions that are named in a way that is consistent with usage, making their names easy to remember and
easy to deploy.
Description: Format numbers for publication; includes the removal of leading zeros, standardization of
number of digits, addition of affixes, and a p-value formatter. These tools combine the
functionality of several 'base' functions such as paste(), format(), and sprintf() into
specific use case functions that are named in a way that is consistent with usage, making their
names easy to remember and easy to deploy.
Depends: R (>= 3.2.0)
Suggests: testthat
Date: 2017-08-29
Date: 2017-08-30
License: GPL-2
LazyData: TRUE
Roxygen: list(wrap = FALSE)
RoxygenNote: 6.0.1
Collate: 'alignment.R' 'as_factor.R' 'f_12_hour.R' 'utils.R' 'f_affirm.R' 'f_affix.R' 'f_comma.R' 'f_date.R' 'f_denom.R' 'f_dollar.R'
'f_logical.R' 'f_month.R' 'f_num.R' 'f_ordinal.R' 'f_pad_zero.R' 'f_parenthesis.R' 'f_percent.R' 'f_pval.R' 'f_sign.R'
'f_weekday.R' 'fv_num_percent.R' 'fv_percent.R' 'fv_percent_diff.R' 'fv_runs.R' 'numform-package.R'
Collate:
'alignment.R'
'as_factor.R'
'f_12_hour.R'
'utils.R'
'f_affirm.R'
'f_affix.R'
'f_comma.R'
'f_date.R'
'f_denom.R'
'f_dollar.R'
'f_logical.R'
'f_month.R'
'f_num.R'
'f_ordinal.R'
'f_pad_zero.R'
'f_parenthesis.R'
'f_percent.R'
'f_pval.R'
'f_replace.R'
'f_sign.R'
'f_title.R'
'f_weekday.R'
'f_wrap.R'
'fv_num_percent.R'
'fv_percent.R'
'fv_percent_diff.R'
'fv_runs.R'
'numform-package.R'
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ export(f_percent)
export(f_prefix)
export(f_prop2percent)
export(f_pval)
export(f_replace)
export(f_sign)
export(f_suffix)
export(f_thous)
export(f_title)
export(f_weekday)
export(f_wrap)
export(ff_affirm)
export(ff_affix)
export(ff_bills)
Expand All @@ -57,9 +60,12 @@ export(ff_percent)
export(ff_prefix)
export(ff_prop2percent)
export(ff_pval)
export(ff_replace)
export(ff_sign)
export(ff_suffix)
export(ff_thous)
export(ff_title)
export(ff_wrap)
export(ffv_num_percent)
export(ffv_percent)
export(ffv_percent_diff)
Expand Down
7 changes: 7 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ NEW FEATURES
* `f_date` and `f_12_hour` added to convert dates and times into common in-text
form.

* `f_replace` added as a ggplot2 scale `gsub` convenience function.

* `f_title` added as a ggplot2 scale `tools::toTitleCase` convenience function.

* `f_title` added as a ggplot2 scale `strwrap` + `paste(collapse =TRUE)`
convenience function.

MINOR FEATURES

* `f_sign` picks up `negative` and `positive` assignments allowing for more
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ numform 0.0.6 -
* `f_date` and `f_12_hour` added to convert dates and times into common in-text
form.

* `f_replace` added as a ggplot2 scale `gsub` convenience function.

* `f_title` added as a ggplot2 scale `tools::toTitleCase` convenience function.

* `f_title` added as a ggplot2 scale `strwrap` + `paste(collapse =TRUE)`
convenience function.

**MINOR FEATURES**

* `f_sign` picks up `negative` and `positive` assignments allowing for more
Expand Down
33 changes: 33 additions & 0 deletions R/f_replace.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#' Replace Characters in Strings
#'
#' A wrapper for \code{\link[base]{gsub}} for replacing substrings that is
#' useful for \pkg{ggplot2} scales. Useful for taking field names like
#' 'Cool_Variable' and turning it into 'Cool Variable'.
#'
#' @param x A vector of text strings.
#' @param pattern A character string defining search patterns.
#' @param replacement A character string defining replacement patterns.
#' @param \ldots Other arguments passed to \code{\link[base]{gsub}}.
#' @return Returns a string vector with characters replaced.
#' @rdname f_replace
#' @export
#' @seealso \code{\link[base]{strwrap}}
#' @examples
#' f_replace('Cool_Variable')
#' f_title(f_replace('cool_variable'))
#' f_replace('Cool_Variable', pattern = '([A-Z])', replacement = '\\L\\1')
#' cat(f_replace('really long label names are the pits',
#' pattern = '\\s', replace = '\n'))
f_replace <- function (x, pattern = '_', replacement = ' ', ...) {

gsub(pattern, replacement, x, perl = TRUE, ...)

}


#' @export
#' @include utils.R
#' @rdname f_replace
ff_replace <- functionize(f_replace)


111 changes: 111 additions & 0 deletions R/f_title.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#' Convert First Letter of Words to Title Case
#'
#' A wrapper for \code{\link[tools]{toTitleCase}} converting text to title case.
#'
#' @param x A vector of text strings.
#' @param \ldots ignored.
#' @return Returns a string vector with characters replaced.
#' @rdname f_title
#' @export
#' @seealso \code{\link[tools]{toTitleCase}}
#' @examples
#' f_title('i love this title')
#' f_title(f_title('Cool_Variable'))
#'
#' \dontrun{
#' library(tidyverse)
#'
#' set.seed(10)
#' dat <- data_frame(
#' level = c("not_involved", "somewhat_involved_single_group",
#' "somewhat_involved_multiple_groups", "very_involved_one_group",
#' "very_involved_multiple_groups"
#' ),
#' n = sample(1:10, length(level))
#' ) %>%
#' mutate(
#' level = factor(level, levels = unique(level)),
#' `%` = n/sum(n)
#' )
#'
#' gridExtra::grid.arrange(
#'
#' gridExtra::arrangeGrob(
#'
#' dat %>%
#' ggplot(aes(level, `%`)) +
#' geom_col() +
#' labs(title = 'Very Sad', y = NULL) +
#' theme(
#' axis.text = element_text(size = 7),
#' title = element_text(size = 9)
#' ),
#'
#' dat %>%
#' ggplot(aes(level, `%`)) +
#' geom_col() +
#' scale_x_discrete(labels = function(x) f_replace(x, '_', '\n')) +
#' scale_y_continuous(labels = ff_prop2percent(digits = 0)) +
#' labs(title = 'Underscore Split (Readable)', y = NULL) +
#' theme(
#' axis.text = element_text(size = 7),
#' title = element_text(size = 9)
#' ),
#'
#'
#' ncol = 2
#'
#' ),
#' gridExtra::arrangeGrob(
#'
#' dat %>%
#' ggplot(aes(level, `%`)) +
#' geom_col() +
#' scale_x_discrete(labels = function(x) f_title(f_replace(x))) +
#' scale_y_continuous(labels = ff_prop2percent(digits = 0)) +
#' labs(title = 'Underscore Replaced & Title (Capitalized Sadness)', y = NULL) +
#' theme(
#' axis.text = element_text(size = 7),
#' title = element_text(size = 9)
#' ),
#'
#' dat %>%
#' ggplot(aes(level, `%`)) +
#' geom_col() +
#' scale_x_discrete(labels = function(x) f_wrap(f_title(f_replace(x)))) +
#' scale_y_continuous(labels = ff_prop2percent(digits = 0)) +
#' labs(title = 'Underscore Replaced, Title, & Wrapped (Happy)', y = NULL) +
#' theme(
#' axis.text = element_text(size = 7),
#' title = element_text(size = 9)
#' ),
#'
#' ncol = 2
#'
#' ), ncol = 1
#'
#' )
#'
#' }
f_title <- function (x, ...) {

nas <- is.na(x)
out <- gsub('(^.)', '\\U\\1', tools::toTitleCase(x), perl = TRUE)

out[nas] <- NA
out

}


#' @export
#' @include utils.R
#' @rdname f_title
ff_title <- functionize(f_title)







73 changes: 73 additions & 0 deletions R/f_wrap.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#' Wrap Strings
#'
#' Wrap strings by splitting n width, and paste collapsing with new line
#' characters.
#'
#' @param x A vector of text strings.
#' @param width A positive integer giving the target column for wrapping lines
#' in the output.
#' @param exdent A non-negative integer specifying the indentation of subsequent
#' lines in paragraphs.
#' @param indent A non-negative integer giving the indentation of the first line
#' in a paragraph.
#' @param \ldots Other arguments passed to \code{\link[base]{strwrap}}.
#' @return Returns a string vector with wrapped new line characters.
#' @rdname f_wrap
#' @export
#' @seealso \code{\link[base]{strwrap}}
#' @examples
#' cat(f_wrap('really long label names are the pits'))
#' cat(f_wrap('really long label names are the pits', width = 20, exdent = 2))
#'
#' \dontrun{
#' library(tidyverse); library(gridExtra)
#'
#' set.seed(10)
#' dat <- data_frame(
#' level = c('Not Involved', 'Somewhat Involved Single Group',
#' 'Somewhat Involved Multiple Groups', 'Very Involved One Group',
#' 'Very Involved Multiple Groups'
#' ),
#' n = sample(1:10, length(level))
#' ) %>%
#' mutate(
#' level = factor(level, levels = unique(level)),
#' `%` = n/sum(n)
#' )
#'
#' gridExtra::grid.arrange(
#' dat %>%
#' ggplot(aes(level, `%`)) +
#' geom_col() +
#' labs(title = 'Yucky Labels', y = NULL),
#'
#' dat %>%
#' ggplot(aes(level, `%`)) +
#' geom_col() +
#' scale_x_discrete(labels = f_wrap) +
#' scale_y_continuous(labels = ff_prop2percent(digits = 0)) +
#' labs(title = 'Happy Labels', y = NULL),
#'
#' ncol = 1, heights = c(.45, .55)
#' )
#'
#' }
f_wrap <- function (x, width = 15, exdent = 0, indent = 0, ...) {

nas <- is.na(x)
out <- unlist(lapply(x, function(y) {
paste(
strwrap(y, width = width, exdent = exdent, indent = indent, ...),
collapse = "\n"
)
}))
out[nas] <- NA
out

}


#' @export
#' @include utils.R
#' @rdname f_wrap
ff_wrap <- functionize(f_wrap)
Loading

0 comments on commit 7d43a42

Please sign in to comment.