Skip to content

Commit

Permalink
added pc for pretty comma print and added s/e to f to allow starting …
Browse files Browse the repository at this point in the history
…and ending character additions.
  • Loading branch information
trinker committed Sep 26, 2015
1 parent ea05573 commit 56f715f
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 5 deletions.
9 changes: 6 additions & 3 deletions DESCRIPTION
@@ -1,12 +1,15 @@
Package: digit
Title: Tools to Format Numbers for Publication
Version: 0.0.1
Authors@R: c(person("Tyler", "Rinker", email = "tyler.rinker@gmail.com", role = c("aut", "cre")))
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, and a p-value formatter.
Description: Format numbers for publication; includes the removal of
leading zeros, standardization of number of digits, and a
p-value formatter.
Depends: R (>= 3.2.0)
Suggests: testthat
Date: 2015-05-13
Date: 2015-09-25
License: GPL-2
LazyData: TRUE
Roxygen: list(wrap = FALSE)
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -2,3 +2,4 @@

export(f)
export(p)
export(pc)
12 changes: 11 additions & 1 deletion R/f.R
Expand Up @@ -6,6 +6,8 @@
#' @param digits The number of digits to use. Defaults to 3. Can be set
#' globally via: \code{options(digit_digits = n)} where n is the number of
#' digits beyond the decimal point to include.
#' @param s A string to paste at the begining of the output from \code{f}.
#' @param e A string to paste at the end of the output from \code{f}.
#' @return Returns a string of publication ready digits.
#' @export
#' @seealso \code{\link[digit]{p}}
Expand All @@ -14,7 +16,13 @@
#' f(rnorm(10))
#' f(rnorm(20, 100, 200), 0)
#' f(c("-0.23", "0", ".23"))
f <- function(x, digits = getOption("digit_digits")) {
#'
#' ## Percents
#' f(c(30, 33.45, .1), 1, e="%")
#'
#' ## Money
#' f(c(30, 33.45, .1), 2, s="$")
f <- function(x, digits = getOption("digit_digits"), s, e) {

if (is.null(digits)) digits <- 3

Expand All @@ -28,6 +36,8 @@ f <- function(x, digits = getOption("digit_digits")) {
if (digits > 0) x <- sprintf(paste0("%.", digits, "f"), x)
out <- gsub("^0(?=\\.)|(?<=-)0", "", x, perl=TRUE)
out[out == "NA"] <- NA
if (!missing(s)) out <- paste0(s, out)
if (!missing(e)) out <- paste0(out, e)
out
}

18 changes: 18 additions & 0 deletions R/pc.R
@@ -0,0 +1,18 @@
#' Comma Format Large Integers
#'
#' Add commas to larger integers.
#'
#' @param x A vector opc numbers (or string equivalents).
#' @param big.mark The character to include every n places.
#' @param \ldots Other arguments passed to \code{\link[base]{prettyNum}}.
#' @return Returns a comma separted string of publication ready digits.
#' @export
#' @seealso \code{\link[base]{prettyNum}}
#' @examples
#' set.seed(4)
#' pc(sample(4:10, 5)^5)
#' ## pc(sample(4:10, 5)^5, ".")
pc <- function(x, big.mark = ",", ...) {

prettyNum(x, big.mark, ...)
}
12 changes: 11 additions & 1 deletion man/f.Rd
Expand Up @@ -4,14 +4,18 @@
\alias{f}
\title{Format Digits}
\usage{
f(x, digits = getOption("digit_digits"))
f(x, digits = getOption("digit_digits"), s, e)
}
\arguments{
\item{x}{A vector of numbers (or string equivalents).}

\item{digits}{The number of digits to use. Defaults to 3. Can be set
globally via: \code{options(digit_digits = n)} where n is the number of
digits beyond the decimal point to include.}

\item{s}{A string to paste at the begining of the output from \code{f}.}

\item{e}{A string to paste at the end of the output from \code{f}.}
}
\value{
Returns a string of publication ready digits.
Expand All @@ -24,6 +28,12 @@ f(c(0.0, 0, .2, -00.02, 1.122222, pi))
f(rnorm(10))
f(rnorm(20, 100, 200), 0)
f(c("-0.23", "0", ".23"))

## Percents
f(c(30, 33.45, .1), 1, e="\%")

## Money
f(c(30, 33.45, .1), 2, s="$")
}
\seealso{
\code{\link[digit]{p}}
Expand Down
30 changes: 30 additions & 0 deletions man/pc.Rd
@@ -0,0 +1,30 @@
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/pc.R
\name{pc}
\alias{pc}
\title{Comma Format Large Integers}
\usage{
pc(x, big.mark = ",", ...)
}
\arguments{
\item{x}{A vector opc numbers (or string equivalents).}

\item{big.mark}{The character to include every n places.}

\item{\ldots}{Other arguments passed to \code{\link[base]{prettyNum}}.}
}
\value{
Returns a comma separted string of publication ready digits.
}
\description{
Add commas to larger integers.
}
\examples{
set.seed(4)
pc(sample(4:10, 5)^5)
## pc(sample(4:10, 5)^5, ".")
}
\seealso{
\code{\link[base]{prettyNum}}
}

12 changes: 12 additions & 0 deletions tests/testthat/test-f.R
Expand Up @@ -13,3 +13,15 @@ test_that("f throws proper warnings.",{
expect_warning(f(.3, c(2, 3)))

})

test_that("f with s works",{

expect_true(all(grepl("%$", f(c(30, 33.45, .1), 1, e="%"))))

})


test_that("f with e works",{

expect_true(all(grepl("^\\$", f(c(30, 33.45, .1), 2, s="$"))))
})
8 changes: 8 additions & 0 deletions tests/testthat/test-pc.R
@@ -0,0 +1,8 @@
context("Checking pc")

test_that("pc ...",{

expect_true(nchar(pc(123456789)) == 11)

})

0 comments on commit 56f715f

Please sign in to comment.