Skip to content

Commit

Permalink
added additional.numeric to alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
trinker committed Aug 11, 2017
1 parent 955d7ee commit f58f519
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 88 deletions.
14 changes: 9 additions & 5 deletions R/alignment.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#' @param left A value to print for left aligned columns.
#' @param right A value to print for right aligned columns. If \code{left = "l"}
#' \code{right} will default to \code{"r"} otherwise defaults to \code{"right"}.
#' @param additional.numeric An additional regex to consider as numeric. To turn
#' off this feature use \code{additional.numeric = NULL}.
#' @param sep A string to collapse the vector on.
#' @param \ldots ignored.
#' @return Returns a vector of lefts and rights or a string (if \code{sep} is not
Expand Down Expand Up @@ -71,26 +73,28 @@
#' xtable(align = c('', alignment(CO, 'l|', 'r|'))) %>%
#' print(include.rownames = FALSE)
#' }
alignment <- function(x, left = 'left', right = ifelse(left == 'l', 'r', 'right'), sep = NULL, ...){
alignment <- function(x, left = 'left', right = ifelse(left == 'l', 'r', 'right'),
additional.numeric = '^(<b>(&ndash;|\\+)</b>)$', sep = NULL, ...){

stopifnot(is.data.frame(x))

out <- ifelse(right_align(x), right, left)
out <- ifelse(right_align(x, additional.numeric = additional.numeric), right, left)

if (!is.null(sep)) out <- paste(out, collapse = sep)

out
}


right_align <- function(df){
right_align <- function(df, additional.numeric = NULL){
unname(unlist(lapply(df, function(x){
x <- as.character(x)
grepl('^(((\\$)?[0-9.,+-]+( ?%|[KMB])?)|([0-9/:.-T ]{5,}))$', rm_na(x)[1])
if (!is.null(additional.numeric)) numregex <- paste(paste0('(', unlist(c(additional.numeric, additional.numeric)), ')'), collapse = "|")
grepl(numregex, rm_na(x)[1])
})))
}



numregex <- '(^(((\\$)?[0-9.,+-]+( ?%|[KMB])?)|([0-9/:.-T ]{5,}))$)'


164 changes: 82 additions & 82 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ functions (see [Plotting](#plotting) for usage).

<!-- html table generated in R 3.4.1 by xtable 1.8-2 package -->

<!-- Thu Aug 10 23:07:33 2017 -->
<!-- Thu Aug 10 23:18:49 2017 -->

<table>

Expand Down Expand Up @@ -469,15 +469,15 @@ alignment.
<tr class="header">

<th align="left">Team</th>
<th align="right">Year</th>
<th align="right">YearStart</th>
<th align="right">Won</th>
<th align="right">Lost</th>
<th align="right">WinLossRate</th>
<th align="right">PropWon</th>
<th align="right">PropLost</th>
<th align="right">%ΔWinLoss</th>
<th align="left">ΔWinLoss</th>
<th align="left">Year</th>
<th align="left">YearStart</th>
<th align="left">Won</th>
<th align="left">Lost</th>
<th align="left">WinLossRate</th>
<th align="left">PropWon</th>
<th align="left">PropLost</th>
<th align="left">%ΔWinLoss</th>
<th align="right">ΔWinLoss</th>
</tr>

</thead>
Expand All @@ -487,113 +487,113 @@ alignment.
<tr class="odd">

<td align="left">West Coast</td>
<td align="right">2012</td>
<td align="right">$2.0M</td>
<td align="right">$350K</td>
<td align="right">$190K</td>
<td align="right">1.9</td>
<td align="right">17%</td>
<td align="right">9%</td>
<td align="right">0%</td>
<td align="left"><b>+</b></td>
<td align="left">2012</td>
<td align="left">$2.0M</td>
<td align="left">$350K</td>
<td align="left">$190K</td>
<td align="left">1.9</td>
<td align="left">17%</td>
<td align="left">9%</td>
<td align="left">0%</td>
<td align="right"><b>+</b></td>
</tr>

<tr class="even">

<td align="left"></td>
<td align="right">2013</td>
<td align="right">$1.8M</td>
<td align="right">$600K</td>
<td align="right">$370K</td>
<td align="right">1.6</td>
<td align="right">33%</td>
<td align="right">20%</td>
<td align="right">-13%</td>
<td align="left"><b>+</b></td>
<td align="left">2013</td>
<td align="left">$1.8M</td>
<td align="left">$600K</td>
<td align="left">$370K</td>
<td align="left">1.6</td>
<td align="left">33%</td>
<td align="left">20%</td>
<td align="left">-13%</td>
<td align="right"><b>+</b></td>
</tr>

<tr class="odd">

<td align="left"></td>
<td align="right">2014</td>
<td align="right">$ .6M</td>
<td align="right">$550K</td>
<td align="right">$300K</td>
<td align="right">1.8</td>
<td align="right">87%</td>
<td align="right">48%</td>
<td align="right">11%</td>
<td align="left"><b>+</b></td>
<td align="left">2014</td>
<td align="left">$ .6M</td>
<td align="left">$550K</td>
<td align="left">$300K</td>
<td align="left">1.8</td>
<td align="left">87%</td>
<td align="left">48%</td>
<td align="left">11%</td>
<td align="right"><b>+</b></td>
</tr>

<tr class="even">

<td align="left"></td>
<td align="right">2015</td>
<td align="right">$1.4M</td>
<td align="right">$420K</td>
<td align="right">$270K</td>
<td align="right">1.6</td>
<td align="right">30%</td>
<td align="right">19%</td>
<td align="right">-13%</td>
<td align="left"><b>+</b></td>
<td align="left">2015</td>
<td align="left">$1.4M</td>
<td align="left">$420K</td>
<td align="left">$270K</td>
<td align="left">1.6</td>
<td align="left">30%</td>
<td align="left">19%</td>
<td align="left">-13%</td>
<td align="right"><b>+</b></td>
</tr>

<tr class="odd">

<td align="left">East Coast</td>
<td align="right">2012</td>
<td align="right">$2.3M</td>
<td align="right">$210K</td>
<td align="right">$420K</td>
<td align="right">.5</td>
<td align="right">9%</td>
<td align="right">18%</td>
<td align="right">0%</td>
<td align="left"><b>–</b></td>
<td align="left">2012</td>
<td align="left">$2.3M</td>
<td align="left">$210K</td>
<td align="left">$420K</td>
<td align="left">.5</td>
<td align="left">9%</td>
<td align="left">18%</td>
<td align="left">0%</td>
<td align="right"><b>–</b></td>
</tr>

<tr class="even">

<td align="left"></td>
<td align="right">2013</td>
<td align="right">$2.4M</td>
<td align="right">$360K</td>
<td align="right">$390K</td>
<td align="right">.9</td>
<td align="right">15%</td>
<td align="right">16%</td>
<td align="right">86%</td>
<td align="left"><b>–</b></td>
<td align="left">2013</td>
<td align="left">$2.4M</td>
<td align="left">$360K</td>
<td align="left">$390K</td>
<td align="left">.9</td>
<td align="left">15%</td>
<td align="left">16%</td>
<td align="left">86%</td>
<td align="right"><b>–</b></td>
</tr>

<tr class="odd">

<td align="left"></td>
<td align="right">2014</td>
<td align="right">$ .8M</td>
<td align="right">$590K</td>
<td align="right">$ 70K</td>
<td align="right">8.4</td>
<td align="right">74%</td>
<td align="right">9%</td>
<td align="right">811%</td>
<td align="left"><b>+</b></td>
<td align="left">2014</td>
<td align="left">$ .8M</td>
<td align="left">$590K</td>
<td align="left">$ 70K</td>
<td align="left">8.4</td>
<td align="left">74%</td>
<td align="left">9%</td>
<td align="left">811%</td>
<td align="right"><b>+</b></td>
</tr>

<tr class="even">

<td align="left"></td>
<td align="right">2015</td>
<td align="right">$1.6M</td>
<td align="right">$500K</td>
<td align="right">$420K</td>
<td align="right">1.2</td>
<td align="right">30%</td>
<td align="right">26%</td>
<td align="right">-86%</td>
<td align="left"><b>+</b></td>
<td align="left">2015</td>
<td align="left">$1.6M</td>
<td align="left">$500K</td>
<td align="left">$420K</td>
<td align="left">1.2</td>
<td align="left">30%</td>
<td align="left">26%</td>
<td align="left">-86%</td>
<td align="right"><b>+</b></td>
</tr>

</tbody>
Expand Down
5 changes: 4 additions & 1 deletion man/alignment.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f58f519

Please sign in to comment.