Skip to content

Commit

Permalink
Merge pull request #3 from gbganalyst/englue
Browse files Browse the repository at this point in the history
Added str_englue()
  • Loading branch information
gbganalyst authored Apr 10, 2023
2 parents 1e294a0 + 8f5f7ed commit af4bf64
Show file tree
Hide file tree
Showing 22 changed files with 231 additions and 28 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
^cran-comments.md$
^\.github$
^CRAN-SUBMISSION$
Rplot.png
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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")),
Expand All @@ -25,6 +25,9 @@ Suggests:
VignetteBuilder: knitr
Imports:
dplyr,
ggplot2,
glue,
rlang,
stringr,
tidyselect
Config/testthat/edition: 3
Expand Down
7 changes: 7 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 7 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -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.
The development version of forstringr is now on Githhub.
6 changes: 5 additions & 1 deletion R/length_omit_na.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
29 changes: 29 additions & 0 deletions R/str_englue.R
Original file line number Diff line number Diff line change
@@ -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")

10 changes: 7 additions & 3 deletions R/str_extract_part.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
#'
Expand All @@ -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")
Expand Down
6 changes: 5 additions & 1 deletion R/str_left.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
14 changes: 13 additions & 1 deletion R/str_mid.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}


6 changes: 5 additions & 1 deletion R/str_right.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
10 changes: 9 additions & 1 deletion R/str_rm_whitespace.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
7 changes: 7 additions & 0 deletions R/str_split_extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 22 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,44 @@ 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"
#> [6] "Cletus" "Orji Uzor" "ABC" "Jimoh" "Tony"

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)
```

<img src="man/figures/README-example_6-1.png" width="100%" />

## `str_rm_whitespace_df()`

Extra spaces are accidentally entered when working with survey data, and
Expand Down
Binary file added Rplot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Binary file added man/figures/README-example_6-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions man/str_englue.Rd

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

8 changes: 4 additions & 4 deletions man/str_extract_part.Rd

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

4 changes: 2 additions & 2 deletions tests/testthat/test-str_extract_part.R
Original file line number Diff line number Diff line change
@@ -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")
)
})
7 changes: 7 additions & 0 deletions tests/testthat/test-test-str_englue.R
Original file line number Diff line number Diff line change
@@ -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")
})

Loading

0 comments on commit af4bf64

Please sign in to comment.