Skip to content

Commit

Permalink
Update comma and percent, update license
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyokc committed Apr 5, 2024
1 parent 6e6c09f commit c926654
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 742 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ coverage.*
init.sh
workflows.md
images
^LICENSE\.md$
33 changes: 28 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,37 @@ Version: 0.1.0
Date: 2023-07-05
Authors@R: person("anthonyokc", , , "anthonyokc@example.com", role = c("aut", "cre"))
Description: R package template with GitHub Actions workflows included.
License: Apache License 2.0 | file LICENSE
License: MIT + file LICENSE
URL: https://github.com/anthonyokc/anthonyverse/
BugReports: https://github.com/anthonyokc/anthonyverse/issues
Depends: R (>= 4.3.1)
Imports: dplyr, geojsonsf, ggplot2, graphics, grDevices, highcharter, htmltools, htmlwidgets, janitor,
jsonlite, lubridate, ojodb, purrr, rlang, scales, snakecase, stats, stringr, tidycensus,
tidyr, tigris, viridis
Suggests: knitr, testthat (>= 2.0)
Imports:
dplyr,
geojsonsf,
ggplot2,
graphics,
grDevices,
highcharter,
htmltools,
htmlwidgets,
janitor,
jsonlite,
lubridate,
ojodb,
purrr,
rlang,
scales,
snakecase,
stats,
stringr,
tidycensus,
tidyr,
tigris,
viridis
Suggests:
knitr,
testthat (>= 2.0),
pkgndep
VignetteBuilder: knitr
biocViews:
Encoding: UTF-8
Expand Down
676 changes: 2 additions & 674 deletions LICENSE

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# MIT License

Copyright (c) 2024 anthonyverse authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
38 changes: 21 additions & 17 deletions R/comma.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
#' @title FUNCTION_TITLE
#' @description FUNCTION_DESCRIPTION
#' @param x PARAM_DESCRIPTION
#' @param digits PARAM_DESCRIPTION, Default: 0
#' @param abs PARAM_DESCRIPTION, Default: FALSE
#' @param round_to_zero PARAM_DESCRIPTION, Default: FALSE
#' @param drop0trailing PARAM_DESCRIPTION, Default: FALSE
#' @return OUTPUT_DESCRIPTION
#' @details DETAILS
#' @title Convert a numeric value to a formatted string with commas
#' @description User inputs a numeric value and function converts it to a formatted string with commas.
#' @param x A numeric value to be converted to a formatted string with commas
#' @param digits Number of decimal places to round to, Default:
#' @param abs Take the absolute value of x before converting to a formatted string with commas, Default: FALSE
#' @param round_to_zero Ensures no number every rounds to zero. For example, 0.4 will always be displayed as 0.4 and not be rounded to zero, but 1.4 will still round to 1. Default: FALSE
#' @param drop_trailing_zeros Drop trailing zeros after the decimal point, Default: FALSE
#' @return A formatted string with commas
#' @details The function uses the `round_half_up` function from the janitor package to round the number to the specified number of decimal places. The `formatC` function is then used to format the number as a string with commas and the specified number of decimal places and trailing zeros.
#' @examples
#' \dontrun{
#' if(interactive()){
#' #EXAMPLE1
#' }
#' }
#' x <- 1234567.89
#' x
#' comma(x)
#' comma(x, digits = 3)
#' comma(x, digits = 3, drop_trailing_zeros = TRUE)
#' y <- 0.4
#' comma(y)
#' comma(y, round_to_zero = TRUE)
#' @seealso
#' \code{\link[janitor]{round_half_up}}
#' \code{\link[base]{formatC}}
#' @rdname comma
#' @export
#' @author Anthony Flores
Expand All @@ -23,9 +27,9 @@ comma <- function(x,
digits = 0,
abs = FALSE,
round_to_zero = FALSE,
drop0trailing = FALSE
drop_trailing_zeros = FALSE,
...
) {
.drop0trailing <- drop0trailing
.digits <- digits


Expand All @@ -49,7 +53,7 @@ comma <- function(x,
big.mark = ",",
format = "f",
digits = .digits,
drop0trailing = .drop0trailing
drop0trailing = drop_trailing_zeros
)
return(x)

Expand Down
34 changes: 18 additions & 16 deletions R/percent.R
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
#' @title FUNCTION_TITLE
#' @description FUNCTION_DESCRIPTION
#' @param x PARAM_DESCRIPTION
#' @param digits PARAM_DESCRIPTION, Default: 0
#' @param abs PARAM_DESCRIPTION, Default: FALSE
#' @param drop0trailing PARAM_DESCRIPTION, Default: FALSE
#' @return OUTPUT_DESCRIPTION
#' @details DETAILS
#' @title Convert a decimal to a percentage as a formatted string
#' @description User inputs a decimal number and functions converts it to a percentage as a formatted string.
#' @param x A numeric value to be converted to a percentage
#' @param digits Number of decimal places to round to, Default: 0
#' @param abs Take the absolute value of x before converting to a percentage, Default: FALSE
#' @param drop_trailing_zeros Drop trailing zeros after the decimal point, Default: FALSE
#' @param multiply_by_100 Multiply x by 100 before converting to a percentage, Default: TRUE
#' @param ... Additional arguments to be passed to the `formatC` function
#' @return A formatted string representing the percentage
#' @details The function uses the `round_half_up` function from the janitor package to round the number to the specified number of decimal places. The `formatC` function is then used to format the number as a percentage with the specified number of decimal places and trailing zeros.
#' @examples
#' \dontrun{
#' if(interactive()){
#' #EXAMPLE1
#' }
#' }
#' x <- 0.1235
#' x
#' percent(x)
#' percent(x, digits = 3)
#' percent(x, digits = 3, drop_trailing_zeros = FALSE)
#' @seealso
#' \code{\link[janitor]{round_half_up}}
#' \code{\link[base]{formatC}}
#' @rdname percent
#' @export
#' @author Anthony Flores
#' @importFrom janitor round_half_up
percent <- function(x,
digits = 0,
abs = FALSE,
drop0trailing = FALSE,
drop_trailing_zeros = FALSE,
multiply_by_100 = TRUE,
...
) {

.drop0trailing <- drop0trailing
.digits <- digits

if (abs) { x <- abs(x) }
Expand All @@ -39,7 +41,7 @@ percent <- function(x,
big.mark = ",",
format = "f",
digits = .digits,
drop0trailing = .drop0trailing
drop0trailing = drop_trailing_zeros
) |>
paste0("%")

Expand Down
41 changes: 26 additions & 15 deletions man/comma.Rd

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

33 changes: 19 additions & 14 deletions man/percent.Rd

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

2 changes: 1 addition & 1 deletion renv.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"R": {
"Version": "4.3.2",
"Version": "4.3.3",
"Repositories": [
{
"Name": "BioCsoft",
Expand Down
24 changes: 24 additions & 0 deletions tests/testthat/test-comma.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
test_that("comma works", {
x <- 1234567.89
expect_equal(
comma(x),
"1,234,568"
)
expect_equal(
comma(x, digits = 3),
"1,234,567.890"
)
expect_equal(
comma(x, digits = 3, drop_trailing_zeros = TRUE),
"1,234,567.89"
)
y <- 0.4
expect_equal(
comma(y),
"0.4"
)
expect_equal(
comma(y, round_to_zero = TRUE),
"0"
)
})
6 changes: 6 additions & 0 deletions tests/testthat/test-percent.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test_that("percent works", {
x <- 0.1235
expect_equal(percent(x), "12%")
expect_equal(percent(x, digits = 3), "12.350%")
expect_equal(percent(x, digits = 3, drop_trailing_zeros = TRUE), "12.35%")
})

0 comments on commit c926654

Please sign in to comment.