Skip to content

Commit

Permalink
added ff versions that return functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Rinker committed Mar 3, 2017
1 parent 59e5a3b commit e3ac863
Show file tree
Hide file tree
Showing 36 changed files with 397 additions and 48 deletions.
20 changes: 19 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,22 @@ Date: 2017-03-03
License: GPL-2
LazyData: TRUE
Roxygen: list(wrap = FALSE)
RoxygenNote: 5.0.1
RoxygenNote: 6.0.1
Collate:
'utils.R'
'f_affix.R'
'f_bills.R'
'f_comma.R'
'f_dollar.R'
'f_month.R'
'f_num.R'
'f_ordinal.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'
'numform-package.R'
20 changes: 20 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ export(f_sign)
export(f_suffix)
export(f_thous)
export(f_weekday)
export(ff_affix)
export(ff_bills)
export(ff_comma)
export(ff_dollar)
export(ff_mean_sd)
export(ff_mills)
export(ff_num)
export(ff_num_percent)
export(ff_ordinal)
export(ff_parenthesis)
export(ff_percent)
export(ff_prefix)
export(ff_prop2percent)
export(ff_pval)
export(ff_sign)
export(ff_suffix)
export(ff_thous)
export(ffv_num_percent)
export(ffv_percent)
export(ffv_percent_diff)
export(fv_num_percent)
export(fv_percent)
export(fv_percent_diff)
19 changes: 19 additions & 0 deletions R/f_affix.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,33 @@ f_affix <- function(x, prefix = "", suffix = "", ...){
paste0(prefix, x, suffix)
}


#' @export
#' @include utils.R
#' @rdname f_affix
ff_affix <- functionize(f_affix)

#' @export
#' @rdname f_affix
f_prefix <- function(x, prefix = "$", ...){
paste0(prefix, x)
}

#' @export
#' @include utils.R
#' @rdname f_affix
ff_prefix <- functionize(f_prefix)

#' @export
#' @rdname f_affix
f_suffix <- function(x, suffix = "%", ...){
paste0(x, suffix)
}

#' @export
#' @include utils.R
#' @rdname f_affix
ff_suffix <- functionize(f_suffix)



25 changes: 22 additions & 3 deletions R/f_bills.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#' ggplot(aes(deals, revenue)) +
#' geom_point() +
#' geom_smooth() +
#' scale_y_continuous(label = function(x) x %>% f_thous(x, prefix = '$') )
#' scale_y_continuous(label = ff_thous(prefix = '$') )
#'
#' data_frame(
#' revenue = rnorm(10000, 500000, 50000),
Expand All @@ -61,12 +61,13 @@
#' thous = f_thous(revenue),
#' thous_dollars = f_thous(revenue, prefix = '$'),
#' abb_month = f_month(date),
#' abb_week = factor(f_weekday(date, distinct = TRUE), levels = c('Su', 'M', 'T', 'W', 'Th', 'F', 'S'))
#' abb_week = factor(f_weekday(date, distinct = TRUE),
#' levels = c('Su', 'M', 'T', 'W', 'Th', 'F', 'S'))
#' ) %T>%
#' print() %>%
#' ggplot(aes(abb_week, revenue)) +
#' geom_jitter(width = .2, height = 0, alpha = .2) +
#' scale_y_continuous(label = function(x) x %>% f_thous(x, prefix = '$'))+
#' scale_y_continuous(label = ff_thous(prefix = '$'))+
#' facet_wrap(~site)
#' }
f_bills <- function(x, relative = 0, digits = -9, prefix = "", ...) {
Expand All @@ -78,6 +79,13 @@ f_bills <- function(x, relative = 0, digits = -9, prefix = "", ...) {
}


#' @export
#' @include utils.R
#' @rdname number_abbreviation
ff_bills <- functionize(f_bills)



#' @export
#' @rdname number_abbreviation
f_mills <- function(x, relative = 0, digits = -6, prefix = "", ...) {
Expand All @@ -91,6 +99,13 @@ f_mills <- function(x, relative = 0, digits = -6, prefix = "", ...) {

}

#' @export
#' @include utils.R
#' @rdname number_abbreviation
ff_mills <- functionize(f_mills)



#' @export
#' @rdname number_abbreviation
f_thous <- function(x, relative = 0, digits = -3, prefix = "", ...) {
Expand All @@ -103,6 +118,10 @@ f_thous <- function(x, relative = 0, digits = -3, prefix = "", ...) {
paste0(prefix, ifelse(x == '.', '0K', x))
}

#' @export
#' @include utils.R
#' @rdname number_abbreviation
ff_thous <- functionize(f_thous)



Expand Down
7 changes: 7 additions & 0 deletions R/f_comma.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#' @param \ldots Other arguments passed to \code{\link[base]{prettyNum}}.
#' @return Returns a comma separted string of publication ready digits.
#' @export
#' @rdname f_comma
#' @seealso \code{\link[base]{prettyNum}}
#' @examples
#' set.seed(4)
Expand All @@ -24,4 +25,10 @@ f_comma <- function(x, mark = ",", ...) {
# )
}

#' @export
#' @include utils.R
#' @rdname f_comma
ff_comma <- functionize(f_comma)


# thousands_regex <- "(\\d)(?:(?=\\d+(?=[^\\d.]))(?=(?:[0-9]{3})+\\b)|(?=\\d+(?=\\.))(?=(?:[0-9]{3})+(?=\\.)))"
7 changes: 7 additions & 0 deletions R/f_dollar.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ f_dollar <- function(x, leading_zero = TRUE, digits = 2, ...) {

}


#' @export
#' @include utils.R
#' @rdname f_dollar
ff_dollar<- functionize(f_dollar)


5 changes: 5 additions & 0 deletions R/f_month.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,8 @@ f_month.Date <- function(x, ...) {
f_month.POSIXlt <- function(x, ...) {
toupper(gsub("(^.)(.+)", "\\1", as.character(format(x, "%b"))))
}





16 changes: 15 additions & 1 deletion R/f_num.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
#' digits beyond the decimal point to include.
#' @param p A string to paste at the begining of the output from \code{f_num}.
#' @param s A string to paste at the end of the output from \code{f_num}.
#' @param zero An option value to insert in for zero values.
#' @param zero An value to insert in for zero values.
#' @param \ldots ignored.
#' @return Returns a string of publication ready digits.
#' @export
#' @rdname f_num
#' @examples
#' f_num(c(0.0, 0, .2, -00.02, 1.122222, pi))
#' f_num(rnorm(10))
Expand All @@ -35,6 +36,12 @@
#'
#' mtcars %>%
#' mutate_if(.funs = f_num, is.int)
#'
#' df <- data.frame(x = -10:10, y = (-10:10)/10)
#'
#' ggplot(df, aes(x, y))+
#' geom_point() +
#' scale_y_continuous(labels = ff_num(zero = 0))
#' }
f_num <- function(x, digits = getOption("numformdigits"), p, s, zero = NULL, ...) {

Expand All @@ -56,3 +63,10 @@ f_num <- function(x, digits = getOption("numformdigits"), p, s, zero = NULL, ...
out
}


#' @export
#' @include utils.R
#' @rdname f_num
ff_num <- functionize(f_num)


9 changes: 9 additions & 0 deletions R/f_ordinal.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#' @param x A vector of numbers (or string equivalents).
#' @param \ldots ignored.
#' @return Returns a string vector with ordinal suffixes.
#' @rdname f_ordinal
#' @export
#' @examples
#' f_ordinal(1:10)
Expand All @@ -23,3 +24,11 @@ f_ordinal <- function(x, ...){

x
}


#' @export
#' @include utils.R
#' @rdname f_ordinal
ff_ordinal <- functionize(f_ordinal)


25 changes: 22 additions & 3 deletions R/f_parenthesis.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#' used for when the values are alrey percentages.
#' @param \ldots ignored.
#' @return Returns a vector of parenthesis combined strings using vector x and y.
#' @rdname f_paragraph
#' @rdname f_parenthesis
#' @export
#' @examples
#' f_parenthesis(
Expand Down Expand Up @@ -61,12 +61,18 @@ f_parenthesis <- function(x, y, sep = "", x_prefix = "", y_prefix = "", ...){
)
}

#' @export
#' @include utils.R
#' @rdname f_parenthesis
ff_parenthesis <- functionize(f_parenthesis)


#' Parenthesis Formatting of Two Vectors
#'
#' \code{f_mean_sd} - Wrapper for \code{f_parenthesis} optimized for formatting
#' vectors of means and standard deviations.
#'
#' @rdname f_paragraph
#' @rdname f_parenthesis
#' @export
f_mean_sd <- function(x, y, x_digits = 1, y_digits = x_digits, sep = "", ...) {
paste(
Expand All @@ -76,12 +82,18 @@ f_mean_sd <- function(x, y, x_digits = 1, y_digits = x_digits, sep = "", ...) {
)
}

#' @export
#' @include utils.R
#' @rdname f_parenthesis
ff_mean_sd <- functionize(f_mean_sd)


#' Parenthesis Formatting of Two Vectors
#'
#' \code{f_num_percent} - Wrapper for \code{f_parenthesis} optimized for formatting
#' vectors of numbers and percentages deviations.
#'
#' @rdname f_paragraph
#' @rdname f_parenthesis
#' @export
f_num_percent <- function(x, y, x_digits = 1, y_digits = x_digits, sep = "",
prop_fun = numform::f_prop2percent, ...) {
Expand All @@ -92,3 +104,10 @@ f_num_percent <- function(x, y, x_digits = 1, y_digits = x_digits, sep = "",
)
}


#' @export
#' @include utils.R
#' @rdname f_parenthesis
ff_num_percent <- functionize(f_num_percent)


26 changes: 26 additions & 0 deletions R/f_percent.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,30 @@
#' f_percent(c(30, 33.45, .1), 1)
#' f_percent(c(0.0, 0, .2, -00.02, 1.122222, pi))
#' f_prop2percent(c(.30, 1, 1.01, .33, .222, .01))
#'
#' \dontrun{
#' if (!require("pacman")) install.packages("pacman")
#' pacman::p_load(ggplot2, dplyr)
#'
#' mtcars %>%
#' count(cyl, gear) %>%
#' group_by(cyl) %>%
#' mutate(prop = n/sum(n)) %>%
#' ggplot(aes(gear, prop)) +
#' geom_bar(stat = 'identity') +
#' facet_wrap(~cyl, ncol = 1) +
#' scale_y_continuous(labels = ff_prop2percent(digits = 0))
#' }
f_percent <- function(x, digits = getOption("numformdigits"), ...) {

f_num(x, digits = digits, s="%", ...)
}

#' @export
#' @include utils.R
#' @rdname f_percent
ff_percent <- functionize(f_percent)


#' Format Percentages
#'
Expand All @@ -34,3 +53,10 @@ f_prop2percent <- function(x, digits = getOption("numformdigits"), ...) {

f_num(100*x, digits = digits, s="%", ...)
}


#' @export
#' @include utils.R
#' @rdname f_percent
ff_prop2percent <- functionize(f_prop2percent)

10 changes: 10 additions & 0 deletions R/f_pval.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#' digits beyond the decimal point to include.
#' @param \ldots Other values passed to \code{\link[numform]{f_num}}.
#' @return Returns a string of publication ready p-values.
#' @rdname f_pval
#' @export
#' @seealso \code{\link[numform]{f_num}}
#' @examples
Expand Down Expand Up @@ -52,3 +53,12 @@ f_pval <- function(x, alpha = getOption("numformalpha"),
sprintf("p %s %s", op, comp)

}


#' @export
#' @include utils.R
#' @rdname f_pval
ff_pval <- functionize(f_pval)



14 changes: 12 additions & 2 deletions R/f_sign.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@
#' ('-' == < 0, '+' == > 0, or '' == 0).
#'
#' @param x A vector of values.
#' @param zero An value to insert in for zero values.
#' @param \ldots ignored.
#' @return Returns a string of signs.
#' @export
#' @rdname f_sign
#' @seealso \code{\link[numform]{f_num}}
#' @examples
#' f_sign(c(-10, 0, 10))
f_sign <- function(x, ...) {
#' f_sign(c(-10, 0, 10), 0)
f_sign <- function(x, zero = NULL, ...) {

gsub("[01]", "", gsub("^1$", "+", as.character(sign(x))))
out <- gsub("[01]", "", gsub("^1$", "+", as.character(sign(x))))
if (!is.null(zero)) out[out == ""] <- zero
out

}


#' @export
#' @include utils.R
#' @rdname f_sign
ff_sign <- functionize(f_sign)

Loading

0 comments on commit e3ac863

Please sign in to comment.