Skip to content

Commit

Permalink
✨ rename
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Cazelles committed May 21, 2021
1 parent 9cdf11b commit 2912552
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: inSilecoMisc
Title: inSileco Miscellaneous Functions
Date: 2020-11-22
Date: 2021-05-21
Version: 0.6.0.9000
Authors@R: c(
person("Kevin", "Cazelles", email = "kevin.cazelles@gmail.com", comment = c(ORCID = "0000-0001-6619-9874"), role = c("aut", "cre")),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export(msgWarning)
export(multiMatch)
export(packagesUsed)
export(readYamlHeader)
export(rename)
export(scaleWithin)
export(seqCol)
export(seqRg)
Expand Down
7 changes: 4 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# inSilecoMisc (devel)

* New function `commaAnd()` to collpase elements of a vector and add element separators.
* New function `rename()` to rename data frames, list, etc.
* New function `commaAnd()` to collapse elements of a vector and add element separators.
* Add code of conduct.
* Add contributing.
* Add contributing guidelines.


# inSilecoMisc 0.6.0
Expand All @@ -18,7 +19,7 @@
data frame according to the template (see #20).
* add `stopwatch()` and `timer()`.
* add `seqRg()` that generates a regular sequence based on the range of a vector.
* add `seqRow()` and ``seqCol()` that generate a regular sequences based on the number of rows and columns (respectively) of a data frame (or a matrix).
* add `seqRow()` and `seqCol()` that generate a regular sequences based on the number of rows and columns (respectively) of a data frame (or a matrix).


# inSilecoMisc 0.4.0
Expand Down
27 changes: 27 additions & 0 deletions R/rename.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#' Rename object
#'
#' This function finds old names and replace them with new ones.
#'
#' @param x an object from which names can be extracted see [names()].
#' @param old a vector of character strings of old names.
#' @param new a vector of character strings of new names.
#'
#' @export
#' @examples
#' tb <- data.frame(var1 = 2, var2 = "B")
#' rename(tb, "var1", "uptake")
#' rename(tb, c("var1", "var2"), c("uptake", "type"))

rename <- function(x, old, new) {
stopifnot(length(new) == length(old))
out <- x
for (i in seq_along(old)) {
if (sum(names(out) == old[i])) {
names(out)[names(out) == old[i]] <- new[i]
} else {
msgWarning(old[i], "not a valid name.")
}
}
out
}

23 changes: 23 additions & 0 deletions man/rename.Rd

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

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

tb <- data.frame(var1 = 2, var2 = "B")
res1 <- rename(tb, c("var1", "var2"), c("uptake", "type"))
res2 <- rename(tb, c("var"), c("cool"))


test_that("renamed as exected", {
expect_equal(names(res1), c("uptake", "type"))
expect_equal(names(res2), names(tb))
expect_error(rename(tb, "var1", c("up", "take")))
})

0 comments on commit 2912552

Please sign in to comment.