Skip to content

Commit

Permalink
✨ introduces commaAnd()
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Cazelles committed Dec 4, 2020
1 parent a40b4ff commit 9cdf11b
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export(applyString)
export(assignClass2df)
export(assignIds)
export(categorize)
export(commaAnd)
export(dfTemplate)
export(dfTemplateMatch)
export(duplicateCol)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# inSilecoMisc (devel)

* New function `commaAnd()` to collpase elements of a vector and add element separators.
* Add code of conduct.
* Add contributing.

Expand Down
15 changes: 15 additions & 0 deletions R/commaAnd.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#' Paste element and add elememt separators.
#'
#' @param x vector of to be coerced to character strings (see [paste()]).
#' @param comma element separator.
#' @param and last element separator (for 3 elements in `x` and more).
#'
#' @export
#' @examples
#' commaAnd(c("Judith", "Peter", "Rebecca"))

commaAnd <- function(x, comma = ", ", and = " and ") {
if (length(x) > 1) {
paste0(paste(x[-length(x)], collapse = comma), and, x[length(x)])
} else x
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ On top of the [tour vignette](http://insileco.github.io/inSilecoMisc/articles/ov

3. https://insileco.github.io/2020/04/21/insilecomisc-0.4.0-part-2/2/

4. https://insileco.github.io/2020/11/24/continuous-integration-for-r-projects-from-travis-ci-to-github-actions-step-by-step/


## Code of Conduct

Expand Down
21 changes: 21 additions & 0 deletions man/commaAnd.Rd

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

10 changes: 10 additions & 0 deletions tests/testthat/test-commaAnd.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
context("commaAnd")


test_that("commaAnd works", {
expect_equal(commaAnd(c("J")), "J")
expect_equal(commaAnd(c("J", "P")), "J and P")
expect_equal(commaAnd(c("J", "P", "R")), "J, P and R")
expect_equal(commaAnd(c("J", "P", "R", "M")), "J, P, R and M")
expect_equal(commaAnd(c("J", "P", "R"), " and ", " and finally "), "J and P and finally R")
})
8 changes: 8 additions & 0 deletions vignettes/overview.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@ getDigits(c("a1", "032hdje2832"))
```


## Collapse elements of a vector and add element separators


```{r commaAnd}
commaAnd(c("Judith", "Peter", "Rebecca", "Eric"))
```




# Vectors manipulations
Expand Down

0 comments on commit 9cdf11b

Please sign in to comment.