Skip to content

Commit

Permalink
Add LaTeX math output format
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Gromer committed Aug 31, 2017
1 parent edfb50a commit 30c8c5a
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 24 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ importFrom(dplyr,data_frame)
importFrom(dplyr,left_join)
importFrom(magrittr,"%<>%")
importFrom(magrittr,"%>%")
importFrom(purrr,as_vector)
importFrom(purrr,flatten)
importFrom(purrr,map)
importFrom(purrr,map_chr)
Expand All @@ -33,4 +34,5 @@ importFrom(stats,sd)
importFrom(stats,setNames)
importFrom(stats,t.test)
importFrom(stringr,str_extract)
importFrom(stringr,str_split)
importFrom(stringr,str_trim)
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# apa 0.2.0.9000

## New features

* Add LaTeX math output format

## Bug fixes and minor improvements

* Fix error in `anova_apa` when specifying the `effect` argument

# apa 0.2.0

## New features
Expand Down
16 changes: 10 additions & 6 deletions R/anova_apa.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#' or \code{"ges"}).
#' @param format Character string specifying the output format. One of
#' \code{"text"}, \code{"markdown"}, \code{"rmarkdown"}, \code{html},
#' \code{"latex"}, \code{"docx"} or \code{"plotmath"}.
#' \code{"latex"}, \code{"latex_math"}, \code{"docx"} or \code{"plotmath"}.
#' @param info Logical indicating whether to print a message on the used test
#' (default is \code{FALSE})
#' @param print Logical indicating wheter to print the formatted output via
Expand Down Expand Up @@ -43,7 +43,7 @@ anova_apa <- function(x, effect = NULL,
"hf", "none"),
es = c("petasq", "pes", "getasq", "ges"),
format = c("text", "markdown", "rmarkdown", "html",
"latex", "docx", "plotmath"),
"latex", "latex_math", "docx", "plotmath"),
info = FALSE, print = TRUE)
{
sph_corr <- match.arg(sph_corr)
Expand Down Expand Up @@ -388,13 +388,17 @@ anova_apa_print <- function(tbl, effect, es_name, format, print)
tbl$statistic, ", ", fmt_symb("p", format), " ", tbl$p, ", ",
fmt_symb(es_name, format), " ", tbl$es)

if (format == "plotmath")
if (format == "latex")
{
return(anova_apa_print_plotmath(tbl, text, effect))
text <- map_chr(text, fmt_latex)
}
else if (format == "latex")
else if (format == "latex_math")
{
text <- map_chr(text, fmt_latex)
text <- map_chr(text, fmt_latex_math)
}
else if (format == "plotmath")
{
return(anova_apa_print_plotmath(tbl, text, effect))
}

# cat to console
Expand Down
10 changes: 7 additions & 3 deletions R/chisq_apa.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' @param print_n Logical indicating whether to show sample size in text
#' @param format Character string specifying the output format. One of
#' \code{"text"}, \code{"markdown"}, \code{"rmarkdown"}, \code{html},
#' \code{"latex"}, \code{"docx"} or \code{"plotmath"}.
#' \code{"latex"}, \code{"latex_math"}, \code{"docx"} or \code{"plotmath"}.
#' @param info Logical indicating whether to print a message on the used test
#' (default is \code{FALSE})
#' @param print Logical indicating wheter to print the formatted output via
Expand All @@ -18,8 +18,8 @@
#' @export
chisq_apa <- function(x, print_n = FALSE, format = c("text", "markdown",
"rmarkdown", "html",
"latex", "docx",
"plotmath"),
"latex", "latex_math",
"docx", "plotmath"),
info = FALSE, print = TRUE)
{
format <- match.arg(format)
Expand Down Expand Up @@ -52,6 +52,10 @@ chisq_apa <- function(x, print_n = FALSE, format = c("text", "markdown",
{
text <- fmt_latex(text)
}
else if (format == "latex_math")
{
text <- fmt_latex_math(text)
}
else if (format == "plotmath")
{
# Convert text to an expression
Expand Down
8 changes: 6 additions & 2 deletions R/cor_apa.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @param x A call to \code{cor.test}
#' @param format Character string specifying the output format. One of
#' \code{"text"}, \code{"markdown"}, \code{"rmarkdown"}, \code{html},
#' \code{"latex"}, \code{"docx"} or \code{"plotmath"}.
#' \code{"latex"}, \code{"latex_math"}, \code{"docx"} or \code{"plotmath"}.
#' @param info Logical indicating whether to print a message on the used test
#' (default is \code{FALSE})
#' @param print Logical indicating wheter to print the formatted output via
Expand All @@ -23,7 +23,7 @@
#'
#' @export
cor_apa <- function(x, format = c("text", "markdown", "rmarkdown", "html",
"latex", "docx", "plotmath"),
"latex", "latex_math", "docx", "plotmath"),
info = FALSE, print = TRUE)
{
format <- match.arg(format)
Expand Down Expand Up @@ -58,6 +58,10 @@ cor_apa <- function(x, format = c("text", "markdown", "rmarkdown", "html",
{
text <- fmt_latex(text)
}
else if (format == "latex_math")
{
text <- fmt_latex_math(text)
}
else if (format == "plotmath")
{
# Convert text to an expression
Expand Down
9 changes: 7 additions & 2 deletions R/t_apa.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' samples or one sample t-test (cohen's d is reported for these test).
#' @param format Character string specifying the output format. One of
#' \code{"text"}, \code{"markdown"}, \code{"rmarkdown"}, \code{html},
#' \code{"latex"}, \code{"docx"} or \code{"plotmath"}.
#' \code{"latex"}, \code{"latex_math"}, \code{"docx"} or \code{"plotmath"}.
#' @param info Logical indicating whether to print a message on the used test
#' (default is \code{FALSE})
#' @param print Logical indicating wheter to print the formatted output via
Expand All @@ -22,7 +22,8 @@
#' @export
t_apa <- function(x, es = "cohens_d", format = c("text", "markdown",
"rmarkdown", "html", "latex",
"docx", "plotmath"),
"latex_math", "docx",
"plotmath"),
info = FALSE, print = TRUE)
{
format <- match.arg(format)
Expand Down Expand Up @@ -71,6 +72,10 @@ t_apa <- function(x, es = "cohens_d", format = c("text", "markdown",
{
text <- fmt_latex(text)
}
else if (format == "latex_math")
{
text <- fmt_latex_math(text)
}
else if (format == "plotmath")
{
# Convert text to an expression
Expand Down
33 changes: 33 additions & 0 deletions R/utils_format.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ fmt_symb <- function(x, format)
"spearman's" = "$r_s$",
"t" = "\\textit{t}")
}
else if (format == "latex_math")
{
switch(x,
"chisq" = "\\chi^2",
"cohens_d" = "d",
"F" = "F",
"getasq" = "\\eta^2_g",
"glass_delta" = "\\Delta",
"hedges_g" = "g",
"kendall's" = "r_\\tau",
"p" = "p",
"pearson's" = "r",
"petasq" = "\\eta^2_p",
"r" = "r",
"spearman's" = "r_s",
"t" = "t")
}
else if (format == "markdown")
{
switch(x,
Expand Down Expand Up @@ -231,6 +248,22 @@ fmt_latex <- function(x)
gsub("(, n)", ",~n", .)
}

# Format character strings for better LaTeX math mode printing
#' @importFrom magrittr %>%
#' @importFrom purrr as_vector map
#' @importFrom stringr str_split
fmt_latex_math <- function(text)
{
text %>%
# Split string at commas (but not if comma is in parenthesis, e.g. F(1, 50))
str_split(", (?![^(]*\\))") %>%
as_vector() %>%
# Put each piece in a math environment
map_chr(~ paste0("$", .x, "$")) %>%
# Add commas again
paste(collapse = ", ")
}

# Convert APA text to an expression in R's plotmath syntax
#' @importFrom stringr str_trim
fmt_plotmath <- function(text, ...)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

apa's functions format output of statistical tests according to guidelines of the APA (American Psychological Association), ready to copy-and-paste into manuscripts.

The idea of such formatters was introduced in the [schoRsch package](https://cran.r-project.org/package=schoRsch/). apa generalizes this idea by providing formatters for different output formats (text, Markdown, RMarkdown, HTML, LaTeX, docx and R's plotmath syntax).
The idea of such formatters was introduced in the [schoRsch package](https://cran.r-project.org/package=schoRsch/). apa generalizes this idea by providing formatters for different output formats (text, Markdown, RMarkdown, HTML, LaTeX, LaTeX inline math, docx and R's plotmath syntax).

Currently available formatters are:

Expand Down
6 changes: 3 additions & 3 deletions man/anova_apa.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions man/chisq_apa.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/cor_apa.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions man/t_apa.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion vignettes/introduction.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ Calling `cor_apa()` then returns a string ready to copy-and-paste into manuscrip
cor_apa(ct)
```

The `format` argument of `cor_apa()` allows you to specify the output format, which can be one of `"text"` (default), `"markdown"`, `"rmarkdown"`, `"html"`, `"latex"`, `"docx"` or `"plotmath"`.
The `format` argument of `cor_apa()` allows you to specify the output format, which can be one of `"text"` (default), `"markdown"`, `"rmarkdown"`, `"html"`, `"latex"`, `"latex_math"`, `"docx"` or `"plotmath"`.


```{r}
cor_apa(ct, format = "rmarkdown")
Expand Down

0 comments on commit 30c8c5a

Please sign in to comment.