Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove purrr dependency #35

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Language: en-GB
Depends:
R (>= 2.10)
Imports:
purrr (>= 0.3.4),
rlang (>= 0.4.7),
utils
Suggests:
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# r2eng 0.0.0.9006

* Removed {lintr} dependency.
* Removed {lintr} and {purrr} dependenies.

# r2eng 0.0.0.9005

Expand Down
49 changes: 33 additions & 16 deletions R/translate.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#' @rdname translate
#' @export
translate_string <- function(
expression, speak = TRUE, function_call_end = "of "
expression,
speak = TRUE,
function_call_end = "of "
) {

if (!is.character(expression) & length(expression != 1)) {
stop("The 'expression' argument must be a character string.\n")
}

if (!is.logical(speak)) {
stop("The 'speak' argument must be TRUE or FALSE.\n")
}

if (!is.character(function_call_end) & length(function_call_end != 1)) {
stop("The 'function_call_end' argument must be a character string.\n")
}
Expand Down Expand Up @@ -42,30 +46,43 @@ translate_string <- function(
#' translate(variable <- 1)
#' }
translate <- function(expression, speak = TRUE, function_call_end = "of ") {

quoted_expression <- substitute(expression)

if (!is.logical(speak)) {
stop("The 'speak' argument must be TRUE or FALSE.\n")
}

if (!is.character(function_call_end) & length(function_call_end != 1)) {
stop("The 'function_call_end' argument must be a character string.\n")
}
return(.convert_quoted_expression(
quoted_expression, speak = speak, function_call_end = function_call_end)

.convert_quoted_expression(
quoted_expression,
speak = speak,
function_call_end = function_call_end
)

}

.convert_quoted_expression <- function(
quoted_expression, speak = TRUE, function_call_end = "of "
quoted_expression,
speak = TRUE,
function_call_end = "of "
) {

trees <- .convert_expr_tree(deparse(quoted_expression))
trees_nrow <- nrow(trees)

eng_vec <- purrr::map2_chr(
trees$token,
trees$text,
.translate,
function_call_end = function_call_end
)
eng_vec <- vector("character", length = trees_nrow)

for (i in seq_len(trees_nrow)) {
eng_vec[[i]] <- .translate(
token = trees[i, "token"],
text = trees[i, "text"],
function_call_end = function_call_end
)
}

eng_expression <- gsub(" +", " ", paste0(eng_vec, collapse = " "))

Expand All @@ -82,7 +99,7 @@ translate <- function(expression, speak = TRUE, function_call_end = "of ") {
class(results) <- append(class(results), "r2eng")

if (speak) {
speak(results)
speak(results)
}

return(results)
Expand All @@ -98,7 +115,7 @@ translate <- function(expression, speak = TRUE, function_call_end = "of ") {
#' @return Nothing.
#' @export
speak <- function(r2eng, ...) {
UseMethod("speak", r2eng)
UseMethod("speak", r2eng)
}

#' Evaluate expression in r2eng object
Expand All @@ -111,13 +128,13 @@ speak <- function(r2eng, ...) {
#' @return Nothing.
#' @export
evaluate <- function(r2eng, ...) {
UseMethod("evaluate", r2eng)
UseMethod("evaluate", r2eng)
}

#' @rdname evaluate
#' @export
evaluate.r2eng <- function(r2eng, envir = parent.frame(), ...) {
eval(r2eng$quoted_expression, envir = envir)
eval(r2eng$quoted_expression, envir = envir)
}

#' @rdname speak
Expand All @@ -140,8 +157,8 @@ speak.r2eng <- function(r2eng, ...) {
#' @return Nothing.
#' @export
print.r2eng <- function(x, ...) {
cat(paste0("Original expression: ", x$r_expression, "\n"))
cat(paste0("English expression:", x$eng_expression, "\n"))
cat(paste0("Original expression: ", x$r_expression, "\n"))
cat(paste0("English expression:", x$eng_expression, "\n"))
}

.convert_expr_tree <- function(expression) {
Expand Down
26 changes: 13 additions & 13 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@ knitr::opts_chunk$set(
ɑː ˈtuː /eng/

<!-- badges: start -->
[![Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public.](https://www.repostatus.org/badges/latest/wip.svg){target='_blank'}](https://www.repostatus.org/#wip)
[![R build status](https://github.com/matt-dray/r2eng/workflows/R-CMD-check/badge.svg){target='_blank'}](https://github.com/matt-dray/r2eng/actions)
[![codecov](https://codecov.io/gh/matt-dray/r2eng/branch/master/graph/badge.svg){target='_blank'}](https://codecov.io/gh/matt-dray/r2eng)
[![Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public.](https://www.repostatus.org/badges/latest/wip.svg)](https://www.repostatus.org/#wip)
[![R build status](https://github.com/matt-dray/r2eng/workflows/R-CMD-check/badge.svg)](https://github.com/matt-dray/r2eng/actions)
[![codecov](https://codecov.io/gh/matt-dray/r2eng/branch/master/graph/badge.svg)](https://codecov.io/gh/matt-dray/r2eng)
[![Launch Rstudio Binder](http://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/matt-dray/try-r2eng/master?urlpath=rstudio)
<!-- badges: end -->

Make R speakable!

The goal of {r2eng} (as in 'R to English') is to take an R expression and 'translate' it to an English sentence.

The package is intended to:
The package is intended (optimistically) to:

* improve communication between teachers and learners
* make R discussions in English more accessible to non-English speakers
* provide an extra audio evaluation tool for users who are blind or have low vision
* be of interest to any R user that's curious about how R expressions might be vocalised

The project was inspired by [Amelia McNamara](https://twitter.com/AmeliaMN){target='_blank'}'s useR! 2020 talk called 'Speaking R' ([YouTube](https://www.youtube.com/watch?v=ckW9sSdIVAc){target='_blank'}, [slides](https://www.amelia.mn/SpeakingR/#1){target='_blank'}).
The project was inspired by [Amelia McNamara](https://twitter.com/AmeliaMN)'s useR! 2020 talk called 'Speaking R' ([YouTube](https://www.youtube.com/watch?v=ckW9sSdIVAc), [slides](https://www.amelia.mn/SpeakingR/#1)).

This project is a work in progress and highly opinionated. Contributions are welcome, but please see the [Code of Conduct](#conduct){target='_blank'}.
This project is a work in progress and highly opinionated. Contributions are welcome, but please see the [Code of Conduct](#conduct).

## Installation

Expand All @@ -49,14 +49,14 @@ You can install the development version of {r2eng} from GitHub with:
remotes::install_github("matt-dray/r2eng")
```

This package depends on {purrr} and {rlang}.
This package depends on {rlang}.

## Examples

The main function in the package is `translate()`. It uses [non-standard evaluation](http://adv-r.had.co.nz/Computing-on-the-language.html){target='_blank'}, so you pass it a bare R expression like this:
The main function in the package is `translate()`. It uses [non-standard evaluation](http://adv-r.had.co.nz/Computing-on-the-language.html), so you pass it a bare R expression like this:

```{r example-basic}
r2eng::translate(variable <- 1, speak = TRUE)
r2eng::translate(variable <- 1, speak = FALSE)
```

Set `speak = TRUE` for a system call that will read the English sentence out loud (macOS only).
Expand Down Expand Up @@ -135,7 +135,7 @@ r2eng::translate(

The `translate()` function understands the meaning of `=` when used for assignment versus specifying arguments, but feeding an expression such as `x = c(1, 2, 3)` would confuse `translate()` that you want to pass an argument `c(1, 2, 3)` to the parameter `x`.

This is because `translate()` uses [non-standard evaluation](http://adv-r.had.co.nz/Computing-on-the-language.html){target='_blank'}.
This is because `translate()` uses [non-standard evaluation](http://adv-r.had.co.nz/Computing-on-the-language.html).

In such cases, you must use `translate_string()` instead:

Expand All @@ -152,7 +152,7 @@ Another exceptional case for `translate_string()` is when piping and expression:

## RStudio addin

Installing this package also installs an [RStudio addin](https://rstudio.github.io/rstudioaddins/){target='_blank'}.
Installing this package also installs an [RStudio addin](https://rstudio.github.io/rstudioaddins/).

Select an R expression in the editor and then under 'Addins', go to 'Speak R Expression In English' under 'R2ENG'. The selected text will be spoken by your computer.

Expand All @@ -164,6 +164,6 @@ Another Addin function is also available, 'Print R Expression In English', which

## Contributions and Code of Conduct {#conduct}

Contributions are welcome from everyone. Please first [add an issue](https://github.com/matt-dray/r2eng/issues){target='_blank'} if a relevant one one doesn't already exist.
Contributions are welcome from everyone. Please first [add an issue](https://github.com/matt-dray/r2eng/issues) if a relevant one one doesn't already exist.

Please note that the {r2eng} project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html){target='_blank'}. By contributing to this project, you agree to abide by its terms.
Please note that the {r2eng} project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Make R speakable!
The goal of {r2eng} (as in ‘R to English’) is to take an R expression
and ‘translate’ it to an English sentence.

The package is intended to:
The package is intended (optimistically) to:

- improve communication between teachers and learners
- make R discussions in English more accessible to non-English speakers
Expand Down Expand Up @@ -55,7 +55,7 @@ You can install the development version of {r2eng} from GitHub with:
remotes::install_github("matt-dray/r2eng")
```

This package depends on {purrr} and {rlang}.
This package depends on {rlang}.

## Examples

Expand All @@ -65,7 +65,7 @@ target="_blank">non-standard evaluation</a>, so you pass it a bare R
expression like this:

``` r
r2eng::translate(variable <- 1, speak = TRUE)
r2eng::translate(variable <- 1, speak = FALSE)
# Original expression: variable <- 1
# English expression: variable gets 1
```
Expand Down