-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wb_add_chartsheet
- Loading branch information
Showing
7 changed files
with
132 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
|
||
#' takes colour and returns color | ||
#' @param ... ... | ||
#' @returns void. assigns an object in the parent frame | ||
#' @keywords internal | ||
#' @noRd | ||
standardize_color_names <- function(..., return = FALSE) { | ||
|
||
# since R 4.1.0: ...names() | ||
args <- list(...) | ||
if (return) { | ||
args <- args[[1]] | ||
} | ||
got <- names(args) | ||
# can be Color or color | ||
got_color <- which(grepl("colour", tolower(got))) | ||
|
||
if (length(got_color)) { | ||
for (got_col in got_color) { | ||
color <- got[got_col] | ||
name_color <- stringi::stri_replace_all_fixed(color, "olour", "olor") | ||
|
||
if (return) { | ||
names(args)[got_col] <- name_color | ||
} else { | ||
# since R 3.5.0: ...elt(got_col) | ||
value_color <- args[[got_col]] | ||
assign(name_color, value_color, parent.frame()) | ||
} | ||
} | ||
} | ||
|
||
if (return) args | ||
} | ||
|
||
#' takes camelCase and returns camel_case | ||
#' @param ... ... | ||
#' @returns void. assigns an object in the parent frame | ||
#' @keywords internal | ||
#' @noRd | ||
standardize_case_names <- function(..., return = FALSE) { | ||
|
||
# since R 4.1.0: ...names() | ||
args <- list(...) | ||
if (return) { | ||
args <- args[[1]] | ||
} | ||
got <- names(args) | ||
|
||
regex <- "(\\G(?!^)|\\b[a-zA-Z][a-z]*)([A-Z][a-z]*|\\d+)" | ||
|
||
got_camel_cases <- which(grepl(regex, got, perl = TRUE)) | ||
|
||
if (length(got_camel_cases)) { | ||
for (got_camel_case in got_camel_cases) { | ||
camel_case <- got[got_camel_case] | ||
name_camel_case <- gsub( | ||
pattern = regex, | ||
replacement = "\\L\\1_\\2", | ||
x = camel_case, | ||
perl = TRUE | ||
) | ||
# since R 3.5.0: ...elt(got_col) | ||
if (return) { | ||
names(args)[got_camel_case] <- name_camel_case | ||
} else { | ||
value_camel_calse <- args[[got_camel_case]] | ||
assign(name_camel_case, value_camel_calse, parent.frame()) | ||
} | ||
} | ||
} | ||
|
||
if (return) args | ||
|
||
} | ||
|
||
#' takes camelCase and colour returns camel_case and color | ||
#' @param ... ... | ||
#' @returns void. assigns an object in the parent frame | ||
#' @keywords internal | ||
#' @noRd | ||
standardize <- function(...) { | ||
|
||
nms <- list(...) | ||
|
||
rtns <- standardize_color_names(nms, return = TRUE) | ||
rtns <- standardize_case_names(rtns, return = TRUE) | ||
|
||
nms <- names(rtns) | ||
for (i in seq_along(nms)) { | ||
assign(nms[[i]], rtns[[i]], parent.frame()) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
test_that("standardize works", { | ||
|
||
color <- NULL | ||
standardize_color_names(colour = "green") | ||
expect_equal(get("color"), "green") | ||
|
||
tabColor <- NULL | ||
standardize_color_names(tabColour = "green") | ||
expect_equal(get("tabColor"), "green") | ||
|
||
camelCase <- NULL | ||
standardize_case_names(camelCase = "green") | ||
expect_equal(get("camel_case"), "green") | ||
|
||
tab_color <- NULL | ||
standardize(tabColour = "green") | ||
expect_equal(get("tab_color"), "green") | ||
|
||
}) |