From 8f5f7edad3506ead84c7a6036cb114d808d8dd77 Mon Sep 17 00:00:00 2001 From: gbganalyst Date: Mon, 10 Apr 2023 15:49:50 +0100 Subject: [PATCH] Updated the README file --- .Rbuildignore | 1 + DESCRIPTION | 5 +++- NAMESPACE | 7 +++++ NEWS.md | 8 +++++- R/length_omit_na.R | 6 +++- R/str_englue.R | 29 +++++++++++++++++++ R/str_extract_part.R | 10 +++++-- R/str_left.R | 6 +++- R/str_mid.R | 14 ++++++++- R/str_right.R | 6 +++- R/str_rm_whitespace.R | 10 ++++++- R/str_split_extract.R | 7 +++++ README.Rmd | 24 ++++++++++++++-- README.md | 30 +++++++++++++++++-- Rplot.png | Bin 0 -> 485 bytes cran-comments.md | 8 +++--- man/figures/README-example_6-1.png | Bin 0 -> 6245 bytes man/str_englue.Rd | 38 +++++++++++++++++++++++++ man/str_extract_part.Rd | 8 +++--- tests/testthat/test-str_extract_part.R | 4 +-- tests/testthat/test-test-str_englue.R | 7 +++++ vignettes/forstringr.Rmd | 31 +++++++++++++++++--- 22 files changed, 231 insertions(+), 28 deletions(-) create mode 100644 R/str_englue.R create mode 100644 Rplot.png create mode 100644 man/figures/README-example_6-1.png create mode 100644 man/str_englue.Rd create mode 100644 tests/testthat/test-test-str_englue.R diff --git a/.Rbuildignore b/.Rbuildignore index 07da2ee..cc35b52 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -6,3 +6,4 @@ ^cran-comments.md$ ^\.github$ ^CRAN-SUBMISSION$ +Rplot.png diff --git a/DESCRIPTION b/DESCRIPTION index d8aa091..7e5d705 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: forstringr Title: String Manipulation Package for Those Familiar with 'Microsoft Excel' -Version: 0.0.1 +Version: 0.1.1 Authors@R: c( person("Ezekiel", "Ogundepo", , email = "gbganalyst@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-3974-2733")), @@ -25,6 +25,9 @@ Suggests: VignetteBuilder: knitr Imports: dplyr, + ggplot2, + glue, + rlang, stringr, tidyselect Config/testthat/edition: 3 diff --git a/NAMESPACE b/NAMESPACE index 599a780..c783ce3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,13 +1,20 @@ # Generated by roxygen2: do not edit by hand export(length_omit_na) +export(str_englue) export(str_extract_part) export(str_left) export(str_mid) export(str_right) export(str_rm_whitespace_df) export(str_split_extract) +import(rlang) importFrom(dplyr,"%>%") importFrom(dplyr,across) importFrom(dplyr,mutate) +importFrom(ggplot2,aes) +importFrom(ggplot2,geom_histogram) +importFrom(ggplot2,ggplot) +importFrom(ggplot2,labs) +importFrom(glue,glue) importFrom(stats,na.omit) diff --git a/NEWS.md b/NEWS.md index 5d15269..3b4501d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,9 +1,15 @@ # What is New in *forstringr* +## 0.1.1 (2023-04-10) + +- Added `str_englue()`, an alias of `rlang::englue()`. + +- Added meaningful error messages for all the functions in the forstringr package. + ## 0.0.1 (2022-08-29) Added package logo, `community_data` (data containing whitespaces), `length_omit_na()`, and `str_extract_part()` functions, and modified unit tests. ## 0.0.0.9 (2022-07-20) -The development version of forstringr is now on Githhub. \ No newline at end of file +The development version of forstringr is now on Githhub. diff --git a/R/length_omit_na.R b/R/length_omit_na.R index 5c588d3..b4764c3 100644 --- a/R/length_omit_na.R +++ b/R/length_omit_na.R @@ -18,5 +18,9 @@ #' length(ethnicity) #' length_omit_na <- function(x) { - length(na.omit(x)) + if (missing(x)) { + stop("argument 'x' is missing, with no default") + } else { + length(na.omit(x)) + } } diff --git a/R/str_englue.R b/R/str_englue.R new file mode 100644 index 0000000..f11a7ba --- /dev/null +++ b/R/str_englue.R @@ -0,0 +1,29 @@ +#' Dynamic plot labels using glue operators +#' @description +#' `str_englue()` helps you solve the labeling problem during plotting. For example, any value wrapped in `{ }` will be inserted into the string and it can also understands embracing, `{{ }}`, which automatically inserts a given variable name. +# Suppress R CMD check note for glue package (not use but imported for rland) +#' @importFrom glue glue +#' @inheritParams rlang::englue +#' @importFrom ggplot2 ggplot +#' @importFrom ggplot2 aes +#' @importFrom ggplot2 geom_histogram +#' @importFrom ggplot2 labs +#' @import rlang +#' +#' @seealso +#' [rlang::englue()] +#' +#' @export +#' +#' @examples +#' histogram_plot <- function(df, var, binwidth) { +#' df |> +#' ggplot(aes(x = {{ var }})) + +#' geom_histogram(binwidth = binwidth) + +#' labs(title = str_englue("A histogram of {{var}} with binwidth {binwidth}")) +#'} + +#' iris |> histogram_plot(Sepal.Length, binwidth = 0.1) + +str_englue <- function(x, env, error_call, error_arg) englue(x, env = caller_env(), error_call = current_env(), error_arg = "x") + diff --git a/R/str_extract_part.R b/R/str_extract_part.R index b57ac9f..0e169c8 100644 --- a/R/str_extract_part.R +++ b/R/str_extract_part.R @@ -10,7 +10,7 @@ #' @return A subset of the input vector. #' @seealso #' -#' [str_split_extract()] which splits up a string into pieces and extract the results using a specific index position. +#' [str_split_extract()] which splits up a string into pieces and extracts the results using a specified index position. #' #' @export #' @@ -25,11 +25,15 @@ #' #' str_extract_part(c("$159", "$587", "$897"), before = FALSE, pattern = "$") #' -str_extract_part <- function(string, before = TRUE, pattern) { +str_extract_part <- function(string, pattern, before = TRUE) { before <- before + if (missing(string)) { + stop("argument 'string' is missing, with no default") + } + if (missing(pattern)) { - stop("argument `pattern` is missing ") + stop("argument 'pattern' is missing, with no default") } esc_punt <- c("?", "$", "(", ")", "+", ".", "^", "*", "|", "[", "]", "_", "\\", "/", "s") diff --git a/R/str_left.R b/R/str_left.R index 78608ef..b35dee5 100644 --- a/R/str_left.R +++ b/R/str_left.R @@ -19,5 +19,9 @@ #' str_left(c("Female", "Male", "Male", "Female")) #' str_left <- function(string, n = 1) { - substr(string, 1, n) + if (missing(string)) { + stop("argument 'string' is missing, with no default") + } else { + substr(string, 1, n) + } } diff --git a/R/str_mid.R b/R/str_mid.R index 2ff47b7..83a0589 100644 --- a/R/str_mid.R +++ b/R/str_mid.R @@ -17,5 +17,17 @@ #' str_mid("Oyo Ibadan", 5, 6) #' str_mid <- function(string, start, n) { - substr(x = string, start = start, stop = start + n - 1) + if (missing(string)) { + stop("argument 'string' is missing, with no default") + } + if (missing(start)) { + stop("argument 'start' is missing, with no default! Please provide the string location to start extracting from") + } + if (missing(n)) { + stop("argument 'n' is missing, with no default! Please provide the string location to extract to") + } else { + substr(x = string, start = start, stop = start + n - 1) + } } + + diff --git a/R/str_right.R b/R/str_right.R index f4f960d..cd2e614 100644 --- a/R/str_right.R +++ b/R/str_right.R @@ -17,5 +17,9 @@ #' str_right("Sale Price", n = 5) #' str_right <- function(string, n = 1) { - substr(string, nchar(string) - (n - 1), nchar(string)) + if (missing(string)) { + stop("argument 'string' is missing, with no default") + } else { + substr(string, nchar(string) - (n - 1), nchar(string)) + } } diff --git a/R/str_rm_whitespace.R b/R/str_rm_whitespace.R index b73f9e5..78065d1 100644 --- a/R/str_rm_whitespace.R +++ b/R/str_rm_whitespace.R @@ -15,5 +15,13 @@ #' str_rm_whitespace_df(richest_in_nigeria) #' str_rm_whitespace_df <- function(df) { - df %>% mutate(across(tidyselect::vars_select_helpers$where(is.character), stringr::str_squish)) + if (missing(df)) { + stop("argument 'df' is missing, with no default") + } + + if (!is.data.frame(df)) { + stop("'df' must be a data frame object") + } else { + df %>% mutate(across(tidyselect::vars_select_helpers$where(is.character), stringr::str_squish)) + } } diff --git a/R/str_split_extract.R b/R/str_split_extract.R index 5df25c4..f7cf3ca 100644 --- a/R/str_split_extract.R +++ b/R/str_split_extract.R @@ -15,6 +15,13 @@ #' str_split_extract(code, "-", 4) #' str_split_extract <- function(string, pattern, position) { + if (missing(string)) { + stop("argument 'string' is missing, with no default") + } + if (missing(pattern)) { + stop("argument 'pattern' is missing, with no default") + } + if (missing(position)) { stop("Please specify the index of vector you want to extract.") } else { diff --git a/README.Rmd b/README.Rmd index 20eadef..dfd39ae 100644 --- a/README.Rmd +++ b/README.Rmd @@ -112,15 +112,35 @@ first_name Extract strings before or after a given pattern. For example: ```{r example5b} -first_name <- str_extract_part(top_10_richest_nig, before = TRUE, pattern = " ") +first_name <- str_extract_part(top_10_richest_nig, pattern = " ", before = TRUE) first_name revenue <- c("$159", "$587", "$891", "$207", "$793") -str_extract_part(revenue, before = FALSE, pattern = "$") +str_extract_part(revenue, pattern = "$", before = FALSE) ``` +## `str_englue()` + +You can dynamically label ggplot2 plots with the glue operators `[` or `{{}}` using `str_englue()`. For example, any value wrapped in `{ }` will be inserted into the string and you automatically inserts a given variable name using `{{ }}`. + +It is important to note that `str_englue()` must be used inside a function. `str_englue("{{ var }}")` defuses the argument `var` and transforms it to a string using the default name operation. + +```{r example_6, warning=FALSE, fig.width= 5.6, fig.height= 5} +library(ggplot2) + +histogram_plot <- function(df, var, binwidth) { + df |> + ggplot(aes(x = {{ var }})) + + geom_histogram(binwidth = binwidth) + + labs(title = str_englue("A histogram of {{var}} with binwidth {binwidth}")) +} + +iris |> histogram_plot(Sepal.Length, binwidth = 0.1) +``` + + ## `str_rm_whitespace_df()` Extra spaces are accidentally entered when working with survey data, and problems can arise when evaluating such data because of extra spaces. Therefore, the function `str_rm_whitespace_df()` eliminates your data frame unnecessary leading, trailing, or other whitespaces. diff --git a/README.md b/README.md index 6ebfeb6..c60a358 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,7 @@ first_name Extract strings before or after a given pattern. For example: ``` r -first_name <- str_extract_part(top_10_richest_nig, before = TRUE, pattern = " ") +first_name <- str_extract_part(top_10_richest_nig, pattern = " ", before = TRUE) first_name #> [1] "Aliko" "Mike" "Femi" "Arthur" "Abdulsamad" @@ -135,10 +135,36 @@ first_name revenue <- c("$159", "$587", "$891", "$207", "$793") -str_extract_part(revenue, before = FALSE, pattern = "$") +str_extract_part(revenue, pattern = "$", before = FALSE) #> [1] "159" "587" "891" "207" "793" ``` +## `str_englue()` + +You can dynamically label ggplot2 plots with the glue operators `[` or +`{{}}` using `str_englue()`. For example, any value wrapped in `{ }` +will be inserted into the string and you automatically inserts a given +variable name using `{{ }}`. + +It is important to note that `str_englue()` must be used inside a +function. `str_englue("{{ var }}")` defuses the argument `var` and +transforms it to a string using the default name operation. + +``` r +library(ggplot2) + +histogram_plot <- function(df, var, binwidth) { + df |> + ggplot(aes(x = {{ var }})) + + geom_histogram(binwidth = binwidth) + + labs(title = str_englue("A histogram of {{var}} with binwidth {binwidth}")) +} + +iris |> histogram_plot(Sepal.Length, binwidth = 0.1) +``` + + + ## `str_rm_whitespace_df()` Extra spaces are accidentally entered when working with survey data, and diff --git a/Rplot.png b/Rplot.png new file mode 100644 index 0000000000000000000000000000000000000000..540032bb8155fb971fee08f80854df808093e836 GIT binary patch literal 485 zcmeAS@N?(olHy`uVBq!ia0vp^KsI9y2Q!d$@WHL;K&m&uC&U#WBDx6^L&H97FOzV7?2 z*VVN^qgnay|MSHyH%%7lyY+7KS3mpKca}*mV=2SK{6r%j!JkJMd*+69yu9SfF)MrG z)6D^#lP<<3-nn=GWYnz^%YEPecA0T^Rk=0S0lN$fu@VVCzegC_ei^mU{$@`@IH_re1r;O`VP;X6=Ow5HP zTUx_gCVb$&U~})Lv;O3tlj}Ht?utJY^(^e#rBm(yj(soQ``6Z~vU$xuh7}BX+SXy( z`F_O*lD8kvvUOool3MdlQEs!5bTOlx*F2r(Nz-SC_KL|rSiQ@F`BT@-<8k+v@PVSs M)78&qol`;+0KRVHSpWb4 literal 0 HcmV?d00001 diff --git a/cran-comments.md b/cran-comments.md index e3f5d0d..855183e 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -1,10 +1,10 @@ -## Resubmission -This is a resubmission. In this version I have: +## New version -* Converted the DESCRIPTION title to title case. +This is a new version submission. In this version I have: -* Single quoted software names in the Title and Description fields of the DESCRIPTIOn file. +- Added `str_englue()`, an alias of `rlang::englue()`. +- Added meaningful error messages for all the functions in the forstringr package ## R CMD check results 0 errors | 0 warnings | 0 notes diff --git a/man/figures/README-example_6-1.png b/man/figures/README-example_6-1.png new file mode 100644 index 0000000000000000000000000000000000000000..24fd0634b23737599634122ad1e401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t5HXPCO=@lRRsqm-U(vkADIlR2t z*2UBvd2dDS7~$kOB+^vR-*TlpPN`nMtJA9I{=g=FAVZlOD-J%35z+2{AGh86_^(!p z4_20#G`W|LLp#w!r{BBHsMj4CIjS#klcPEBApcbhTTnPz<+>L6)@A*VhnMikXn!C( zpkkg;nK`-8EDi>_P2e;d7N^f>hg}NGrfWU>DvP$t2ps0w2ZZke$xunSn(TZ>nv1l= zK)4^d-{GmbrwH_Dqx-%q8wLYB=Cc|_LA$zIjVV#GG9{z3RR3E}RVRZi@TPg@vk2jo zMutq#V%ub5mvehs6K_OsXU9%wW|y0qDN2{|)~mO+v-^ + ggplot(aes(x = {{ var }})) + + geom_histogram(binwidth = binwidth) + + labs(title = str_englue("A histogram of {{var}} with binwidth {binwidth}")) +} +iris |> histogram_plot(Sepal.Length, binwidth = 0.1) +} +\seealso{ +\code{\link[rlang:englue]{rlang::englue()}} +} diff --git a/man/str_extract_part.Rd b/man/str_extract_part.Rd index 3540b40..205ff3f 100644 --- a/man/str_extract_part.Rd +++ b/man/str_extract_part.Rd @@ -4,14 +4,14 @@ \alias{str_extract_part} \title{Extract strings before or after a given pattern} \usage{ -str_extract_part(string, before = TRUE, pattern) +str_extract_part(string, pattern, before = TRUE) } \arguments{ \item{string}{A character vector.} -\item{before}{The position in the string to extract from. If TRUE, the extract will occur before the pattern; if FALSE, it will happen after the pattern.} - \item{pattern}{Pattern to look for.} + +\item{before}{The position in the string to extract from. If TRUE, the extract will occur before the pattern; if FALSE, it will happen after the pattern.} } \value{ A subset of the input vector. @@ -32,5 +32,5 @@ str_extract_part(c("$159", "$587", "$897"), before = FALSE, pattern = "$") } \seealso{ -\code{\link[=str_split_extract]{str_split_extract()}} which splits up a string into pieces and extract the results using a specific index position. +\code{\link[=str_split_extract]{str_split_extract()}} which splits up a string into pieces and extracts the results using a specified index position. } diff --git a/tests/testthat/test-str_extract_part.R b/tests/testthat/test-str_extract_part.R index dd41d86..4151e67 100644 --- a/tests/testthat/test-str_extract_part.R +++ b/tests/testthat/test-str_extract_part.R @@ -1,13 +1,13 @@ test_that("str_extract_part() returns only strings before the % sign", { expect_equal( - str_extract_part(c("5%", "92%", "75%"), TRUE, "%"), + str_extract_part(string = c("5%", "92%", "75%"), pattern = "%", before = TRUE), c("5", "92", "75") ) }) test_that("str_extract_part() return only strings after -", { expect_equal( - str_extract_part(c("Good-Morning", "Good-Afternoon"), FALSE, "-"), + str_extract_part(string = c("Good-Morning", "Good-Afternoon"), pattern = "-", before = FALSE), c("Morning", "Afternoon") ) }) diff --git a/tests/testthat/test-test-str_englue.R b/tests/testthat/test-test-str_englue.R new file mode 100644 index 0000000..ae859c2 --- /dev/null +++ b/tests/testthat/test-test-str_englue.R @@ -0,0 +1,7 @@ +g <- function(var) str_englue("{{ var }}") + + +test_that("str_englue() automatically inserts a given variable name using a glue operator {{}}", { + expect_equal(g(cyl), "cyl") +}) + diff --git a/vignettes/forstringr.Rmd b/vignettes/forstringr.Rmd index 40e3417..87e45fa 100644 --- a/vignettes/forstringr.Rmd +++ b/vignettes/forstringr.Rmd @@ -3,8 +3,10 @@ title: "String Manipulation Package for Those Familiar with Microsoft Excel" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{String Manipulation Package for Those Familiar with Microsoft Excel} - %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} + %\VignetteEngine{knitr::rmarkdown} +editor_options: + chunk_output_type: console --- ```{r, include = FALSE} @@ -20,7 +22,7 @@ The goal of 'forstringr' is to enable complex string manipulation in R, especial You can install `forstringr` package from [CRAN](https://cran.r-project.org/) with: -```{r eval=FALSE} +```{r eval = FALSE} install.packages("forstringr") ``` @@ -102,13 +104,34 @@ first_name Extract strings before or after a given pattern. For example: ```{r example5b} -first_name <- str_extract_part(top_10_richest_nig, before = TRUE, pattern = " ") +first_name <- str_extract_part(top_10_richest_nig, pattern = " ", before = TRUE) first_name revenue <- c("$159", "$587", "$891", "$207", "$793") -str_extract_part(revenue, before = FALSE, pattern = "$") +str_extract_part(revenue, pattern = "$", before = FALSE) +``` + + +## `str_englue()` + +You can dynamically label ggplot2 plots with the glue operators `[` or `{{}}` using `str_englue()`. For example, any value wrapped in `{ }` will be inserted into the string and you automatically inserts a given variable name using `{{ }}`. + +It is important to note that `str_englue()` must be used inside a function. `str_englue("{{ var }}")` defuses the argument `var` and transforms it to a string using the default name operation. + + +```{r example 6, warning=FALSE, fig.width= 5.6, fig.height= 5} +library(ggplot2) + +histogram_plot <- function(df, var, binwidth) { + df |> + ggplot(aes(x = {{ var }})) + + geom_histogram(binwidth = binwidth) + + labs(title = str_englue("A histogram of {{var}} with binwidth {binwidth}")) +} + +iris |> histogram_plot(Sepal.Length, binwidth = 0.1) ``` ## `str_rm_whitespace_df()`