diff --git a/DESCRIPTION b/DESCRIPTION index 007f627..5903dcf 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -22,5 +22,8 @@ Imports: ggplot2 (>= 2.1.0), grid, scales, - extrafont + extrafont, + hunspell, + stringi, + purrr RoxygenNote: 6.0.0 diff --git a/NAMESPACE b/NAMESPACE index 72846a1..8ce6000 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,8 +1,10 @@ # Generated by roxygen2: do not edit by hand +export("%>%") export(font_an) export(font_rc) export(font_rc_light) +export(gg_check) export(import_roboto_condensed) export(ipsum_pal) export(scale_color_ipsum) @@ -17,4 +19,9 @@ export(update_geom_font_defaults) import(extrafont) import(ggplot2) import(grid) +import(hunspell) import(scales) +import(stringi) +importFrom(purrr,"%>%") +importFrom(purrr,map) +importFrom(purrr,walk) diff --git a/R/check.r b/R/check.r new file mode 100644 index 0000000..79a0c6a --- /dev/null +++ b/R/check.r @@ -0,0 +1,67 @@ +#' Spell check ggplot2 plot labels +#' +#' This function can be used in a piping context or standalone. Current +#' functionality only looks for misspelled words in the labels of ggplot2 objects. +#' When misspelled words are found, a message is printed with the words and the +#' label that they are in. No messages will be printed if there are no misspelled +#' words. +#' +#' @md +#' @param gg ggplot2 object +#' @param dict a dictionary object or string which can be passed to [hunspell::dictionary] +#' @param ignore character vector with additional approved words added to the dictionary +#' @return the object that was passed in +#' @export +#' @examples +#' df <- data.frame(x=c(20, 25, 30), y=c(4, 4, 4), txt=c("One", "Two", "Three")) +#' +#' # piping +#' ggplot(mtcars, aes(mpg, wt)) + +#' geom_point() + +#' geom_text(data=df, aes(x=x, y=y, label=txt)) + +#' labs(x="This is some txt", y="This is more text", +#' title="Thisy is a titlle", +#' subtitle="This is a subtitley", +#' caption="This is a captien") %>% +#' gg_check() +#' +#' # not piping +#' ggplot(mtcars, aes(mpg, wt)) + +#' geom_point() + +#' geom_text(data=df, aes(x=x, y=y, label=txt)) + +#' labs(x="This is some txt", y="This is more text", +#' title="Thisy is a titlle", +#' subtitle="This is a subtitley", +#' caption="This is a captien") -> gg +#' +#' gg_check(gg) +gg_check <- function(gg, dict = hunspell::dictionary("en_US"), ignore = hunspell::en_stats) { + + if (inherits(gg, "labels")) { + lbl <- gg + } else if ("labels" %in% names(gg)) { + lbl <- gg$labels + } else { + return(gg) + } + + if (length(lbl) > 0) { + + purrr::walk(names(lbl), function(lab) { + + words <- stri_extract_all_words(lbl[[lab]]) + words <- unlist(words) + words <- purrr::discard(hunspell(words, "text"), ~length(.)==0) + + if (length(words) > 0) { + message(sprintf("Possible misspelled words in [%s]: (%s)", + lab, paste0(words, collapse=", "))) + } + + }) + + } + + gg + +} diff --git a/R/hrbrthemes-package.R b/R/hrbrthemes-package.R index b62282f..8f22ca3 100644 --- a/R/hrbrthemes-package.R +++ b/R/hrbrthemes-package.R @@ -15,8 +15,20 @@ #' @name hrbrthemes #' @docType package #' @author Bob Rudis (bob@@rud.is) -#' @import ggplot2 -#' @import grid -#' @import scales -#' @import extrafont +#' @import ggplot2 grid scales extrafont hunspell stringi +#' @importFrom purrr %>% map walk +NULL + +#' hrbrthemes exported operators +#' +#' The following functions are imported and then re-exported +#' from the hrbrthemes package to enable use of the magrittr +#' pipe operator with no additional library calls +#' +#' @name hrbrthemes-exports +NULL + +#' @name %>% +#' @export +#' @rdname hrbrthemes-exports NULL diff --git a/README.Rmd b/README.Rmd index 7b3cbe5..520f584 100644 --- a/README.Rmd +++ b/README.Rmd @@ -11,6 +11,7 @@ The following functions are implemented/objects are exported: - `theme_ipsum` : Arial Narrow-based theme - `theme_ipsum_rc` : Roboto Condensed-based theme +- `gg_check`: Spell check ggplot2 plot labels - `update_geom_font_defaults`: Update matching font defaults for text geoms (the default is — unsurprisingly — Arial Narrow) - `scale_x_comma` / `scale_y_comma` : Comma format for axis text and `expand=c(0,0)` (you need to set limits) - `scale_x_percent` / `scale_y_percent` : Percent format for axis text and `expand=c(0,0)` (you need to set limits) @@ -125,6 +126,35 @@ count(mpg, class) %>% theme_ipsum_rc(grid="X") ``` +### Spellcheck ggplot2 labels + +```{r fig.retina=2, message=TRUE} +df <- data.frame(x=c(20, 25, 30), y=c(4, 4, 4), txt=c("One", "Two", "Three")) + +# piped + +ggplot(mtcars, aes(mpg, wt)) + + geom_point() + + geom_text(data=df, aes(x=x, y=y, label=txt)) + + labs(x="This is some txt", y="This is more text", + title="Thisy is a titlle", + subtitle="This is a subtitley", + caption="This is a captien") %>% + gg_check() + +# not-piped + +ggplot(mtcars, aes(mpg, wt)) + + geom_point() + + geom_text(data=df, aes(x=x, y=y, label=txt)) + + labs(x="This is some txt", y="This is more text", + title="Thisy is a titlle", + subtitle="This is a subtitley", + caption="This is a captien") -> gg + +gg_check(gg) +``` + ### Test Results ```{r message=FALSE, warning=FALSE, error=FALSE} diff --git a/README.md b/README.md index 1ed1977..d9bf35d 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ The following functions are implemented/objects are exported: - `theme_ipsum` : Arial Narrow-based theme - `theme_ipsum_rc` : Roboto Condensed-based theme +- `gg_check`: Spell check ggplot2 plot labels - `update_geom_font_defaults`: Update matching font defaults for text geoms (the default is — unsurprisingly — Arial Narrow) - `scale_x_comma` / `scale_y_comma` : Comma format for axis text and `expand=c(0,0)` (you need to set limits) - `scale_x_percent` / `scale_y_percent` : Percent format for axis text and `expand=c(0,0)` (you need to set limits) @@ -133,6 +134,23 @@ count(mpg, class) %>% +### Spellcheck ggplot2 labels + +``` r +df <- data.frame(x=c(20, 25, 30), y=c(4, 4, 4), txt=c("One", "Two", "Three")) + +ggplot(mtcars, aes(mpg, wt)) + + geom_point() + + geom_text(data=df, aes(x=x, y=y, label=txt)) + + labs(x="This is some txt", y="This is more text", + title="Thisy is a titlle", + subtitle="This is a subtitley", + caption="This is a captien") %>% + gg_check() +``` + + + ### Test Results ``` r @@ -142,7 +160,7 @@ library(testthat) date() ``` - ## [1] "Sat Feb 11 17:10:08 2017" + ## [1] "Sat Feb 11 20:33:00 2017" ``` r test_dir("tests/") diff --git a/README_files/figure-markdown_github/unnamed-chunk-10-1.png b/README_files/figure-markdown_github/unnamed-chunk-10-1.png new file mode 100644 index 0000000..bf7ad96 Binary files /dev/null and b/README_files/figure-markdown_github/unnamed-chunk-10-1.png differ diff --git a/man/gg_check.Rd b/man/gg_check.Rd new file mode 100644 index 0000000..eefe4bb --- /dev/null +++ b/man/gg_check.Rd @@ -0,0 +1,50 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/check.r +\name{gg_check} +\alias{gg_check} +\title{Spell check ggplot2 plot labels} +\usage{ +gg_check(gg, dict = hunspell::dictionary("en_US"), + ignore = hunspell::en_stats) +} +\arguments{ +\item{gg}{ggplot2 object} + +\item{dict}{a dictionary object or string which can be passed to \link[hunspell:dictionary]{hunspell::dictionary}} + +\item{ignore}{character vector with additional approved words added to the dictionary} +} +\value{ +the object that was passed in +} +\description{ +This function can be used in a piping context or standalone. Current +functionality only looks for misspelled words in the labels of ggplot2 objects. +When misspelled words are found, a message is printed with the words and the +label that they are in. No messages will be printed if there are no misspelled +words. +} +\examples{ +df <- data.frame(x=c(20, 25, 30), y=c(4, 4, 4), txt=c("One", "Two", "Three")) + +# piping +ggplot(mtcars, aes(mpg, wt)) + + geom_point() + + geom_text(data=df, aes(x=x, y=y, label=txt)) + + labs(x="This is some txt", y="This is more text", + title="Thisy is a titlle", + subtitle="This is a subtitley", + caption="This is a captien") \%>\% + gg_check() + +# not piping +ggplot(mtcars, aes(mpg, wt)) + + geom_point() + + geom_text(data=df, aes(x=x, y=y, label=txt)) + + labs(x="This is some txt", y="This is more text", + title="Thisy is a titlle", + subtitle="This is a subtitley", + caption="This is a captien") -> gg + +gg_check(gg) +} diff --git a/man/hrbrthemes-exports.Rd b/man/hrbrthemes-exports.Rd new file mode 100644 index 0000000..c5cce79 --- /dev/null +++ b/man/hrbrthemes-exports.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/hrbrthemes-package.R +\name{hrbrthemes-exports} +\alias{hrbrthemes-exports} +\alias{\%>\%} +\title{hrbrthemes exported operators} +\description{ +The following functions are imported and then re-exported +from the hrbrthemes package to enable use of the magrittr +pipe operator with no additional library calls +}