Browse Source

spell check ggplot2 labels

tags/0.8.0
boB Rudis 7 years ago
parent
commit
8a36e89b35
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 5
      DESCRIPTION
  2. 7
      NAMESPACE
  3. 67
      R/check.r
  4. 20
      R/hrbrthemes-package.R
  5. 30
      README.Rmd
  6. 20
      README.md
  7. BIN
      README_files/figure-markdown_github/unnamed-chunk-10-1.png
  8. 50
      man/gg_check.Rd
  9. 11
      man/hrbrthemes-exports.Rd

5
DESCRIPTION

@ -22,5 +22,8 @@ Imports:
ggplot2 (>= 2.1.0),
grid,
scales,
extrafont
extrafont,
hunspell,
stringi,
purrr
RoxygenNote: 6.0.0

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

67
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
}

20
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

30
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}

20
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) %>%
<img src="README_files/figure-markdown_github/unnamed-chunk-9-1.png" width="672" />
### 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()
```
<img src="README_files/figure-markdown_github/unnamed-chunk-10-1.png" width="672" />
### 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/")

BIN
README_files/figure-markdown_github/unnamed-chunk-10-1.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

50
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)
}

11
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
}
Loading…
Cancel
Save