boB Rudis
7 years ago
9 changed files with 204 additions and 6 deletions
@ -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 |
|||
|
|||
} |
After Width: | Height: | Size: 70 KiB |
@ -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) |
|||
} |
@ -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…
Reference in new issue