From 007fce7977070e7e1db850b246a80588ff453953 Mon Sep 17 00:00:00 2001 From: Matt Dray <18232097+matt-dray@users.noreply.github.com> Date: Sun, 28 Apr 2024 22:53:29 +0100 Subject: [PATCH] Remove purrr dependency --- DESCRIPTION | 1 - NEWS.md | 2 +- R/translate.R | 49 +++++++++++++++++++++++++++++++++---------------- README.Rmd | 26 +++++++++++++------------- README.md | 6 +++--- 5 files changed, 50 insertions(+), 34 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index b6c1740..faa28c1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -18,7 +18,6 @@ Language: en-GB Depends: R (>= 2.10) Imports: - purrr (>= 0.3.4), rlang (>= 0.4.7), utils Suggests: diff --git a/NEWS.md b/NEWS.md index 410b128..ac17d0d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # r2eng 0.0.0.9006 -* Removed {lintr} dependency. +* Removed {lintr} and {purrr} dependenies. # r2eng 0.0.0.9005 diff --git a/R/translate.R b/R/translate.R index 5a83026..16e3a7e 100644 --- a/R/translate.R +++ b/R/translate.R @@ -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") } @@ -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 = " ")) @@ -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) @@ -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 @@ -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 @@ -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) { diff --git a/README.Rmd b/README.Rmd index 1de1853..a4eca54 100644 --- a/README.Rmd +++ b/README.Rmd @@ -20,9 +20,9 @@ knitr::opts_chunk$set( ɑː ˈtuː /eng/ -[![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) @@ -30,16 +30,16 @@ 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 @@ -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). @@ -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: @@ -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. @@ -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. diff --git a/README.md b/README.md index 0322be9..3105ac4 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -65,7 +65,7 @@ target="_blank">non-standard evaluation, 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 ```