Indrajeet Patil
Indrajeet Patil

Reputation: 4879

converting call to equation in r markdown

I have written the ggstatsplot package to carry out some statistical analysis. The package functions (in the development version) can either return a plot or a call that contains statistical details displayed in the subtitle of the plot.

Here is an example of a plot as a return:

# setup
set.seed(123)

# plot
(p <- ggstatsplot::ggbetweenstats(
  data = mtcars,
  x = am, 
  y = wt,
  return = "plot",
  messages = FALSE
))

# checking class
class(p)
#> [1] "gg"     "ggplot"

Here is an example of a call as a return:

# call
(p_call <- ggstatsplot::ggbetweenstats(
  data = mtcars,
  x = am, 
  y = wt,
  return = "subtitle",
  messages = FALSE
))
#> paste(NULL, italic("t"), "(", "29.23", ") = ", "5.49", ", ", 
#>     italic("p"), " = ", "< 0.001", ", ", italic("g"), " = ", 
#>     "1.89", ", CI"["95%"], " [", "1.10", ", ", "2.83", "]", ", ", 
#>     italic("n"), " = ", 32L)

# checking class
class(p_call)
#> [1] "call"

Based on a user request, my question is if there is any way either to print the call in the R Markdown document or to convert this call to a latex equation?

I am not that familiar using R Markdown and I tried the following but it produces an error:

enter image description here

For reproducibility, here is my session information:

options(width = 300)
library(ggstatsplot)
sessioninfo::session_info()
#> - Session info -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#>  setting  value                                    
#>  version  R version 3.6.0 alpha (2019-03-29 r76300)
#>  os       Windows 10 x64                           
#>  system   x86_64, mingw32                          
#>  ui       RTerm                                    
#>  language (EN)                                     
#>  collate  English_United States.1252               
#>  ctype    English_United States.1252               
#>  tz       America/New_York                         
#>  date     2019-06-12                               
#> 
#> - Packages -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#>  package       * version     date       lib source                                  
#>  abind           1.4-5       2016-07-21 [1] CRAN (R 3.5.0)                          
#>  assertthat      0.2.1       2019-03-21 [1] CRAN (R 3.6.0)                          
#>  backports       1.1.4       2019-04-10 [1] CRAN (R 3.6.0)                          
#>  BayesFactor     0.9.12-4.2  2018-05-19 [1] CRAN (R 3.5.1)                          
#>  bayestestR      0.2.0       2019-05-29 [1] CRAN (R 3.6.0)                          
#>  boot            1.3-21      2019-03-01 [2] CRAN (R 3.6.0)                          
#>  broom           0.5.2.9001  2019-05-29 [1] local                                   
#>  broom.mixed     0.2.4.9000  2019-03-14 [1] Github (bbolker/broom.mixed@c2de407)    
#>  broomExtra      0.0.3.9000  2019-05-20 [1] local                                   
#>  car             3.0-3       2019-05-27 [1] CRAN (R 3.6.0)                          
#>  carData         3.0-2       2018-09-30 [1] CRAN (R 3.5.1)                          
#>  cellranger      1.1.0       2016-07-27 [1] CRAN (R 3.5.1)                          
#>  cli             1.1.0       2019-03-19 [1] CRAN (R 3.6.0)                          
#>  cluster         2.0.7-1     2018-04-13 [2] CRAN (R 3.6.0)                          
#>  coda            0.19-2      2018-10-08 [1] CRAN (R 3.5.1)                          
#>  codetools       0.2-16      2018-12-24 [1] CRAN (R 3.5.2)                          
#>  coin            1.3-0       2019-03-08 [1] CRAN (R 3.5.2)                          
#>  colorspace      1.4-1       2019-03-18 [1] CRAN (R 3.6.0)                          
#>  cowplot         0.9.99      2019-06-09 [1] Github (wilkelab/cowplot@7520d81)       
#>  crayon          1.3.4       2017-09-16 [1] CRAN (R 3.5.1)                          
#>  curl            3.3         2019-01-10 [1] CRAN (R 3.6.0)                          
#>  data.table      1.12.2      2019-04-07 [1] CRAN (R 3.5.3)                          
#>  DEoptimR        1.0-8       2016-11-19 [1] CRAN (R 3.5.0)                          
#>  DescTools       0.99.28     2019-03-17 [1] CRAN (R 3.6.0)                          
#>  digest          0.6.19      2019-05-20 [1] CRAN (R 3.6.0)                          
#>  dplyr           0.8.1       2019-05-14 [1] CRAN (R 3.6.0)                          
#>  ellipsis        0.1.0       2019-02-19 [1] CRAN (R 3.6.0)                          
#>  emmeans         1.3.5       2019-06-10 [1] CRAN (R 3.6.0)                          
#>  EMT             1.1         2013-01-29 [1] CRAN (R 3.5.2)                          
#>  estimability    1.3         2018-02-11 [1] CRAN (R 3.5.0)                          
#>  evaluate        0.14        2019-05-28 [1] CRAN (R 3.6.0)                          
#>  expm            0.999-4     2019-03-21 [1] CRAN (R 3.6.0)                          
#>  ez              4.4-0       2016-11-02 [1] CRAN (R 3.5.3)                          
#>  fit.models      0.5-14      2017-04-06 [1] CRAN (R 3.5.1)                          
#>  forcats         0.4.0       2019-02-17 [1] CRAN (R 3.5.2)                          
#>  foreign         0.8-71      2018-07-20 [2] CRAN (R 3.6.0)                          
#>  generics        0.0.2       2019-03-05 [1] Github (r-lib/generics@c15ac43)         
#>  ggcorrplot      0.1.3       2019-05-19 [1] CRAN (R 3.6.0)                          
#>  ggExtra         0.8         2019-02-23 [1] Github (daattali/ggExtra@dd74c0f)       
#>  ggplot2         3.2.0.9000  2019-06-05 [1] Github (tidyverse/ggplot2@b560662)      
#>  ggrepel         0.8.1       2019-05-07 [1] CRAN (R 3.6.0)                          
#>  ggsignif        0.5.0       2019-02-20 [1] CRAN (R 3.6.0)                          
#>  ggstatsplot   * 0.0.10.9000 2019-06-12 [1] local                                   
#>  glue            1.3.1       2019-03-12 [1] CRAN (R 3.6.0)                          
#>  groupedstats    0.0.7.9000  2019-06-05 [1] local                                   
#>  gtable          0.3.0       2019-03-25 [1] CRAN (R 3.6.0)                          
#>  gtools          3.8.1       2018-06-26 [1] CRAN (R 3.5.0)                          
#>  haven           2.1.0       2019-02-19 [1] CRAN (R 3.6.0)                          
#>  highr           0.8         2019-03-20 [1] CRAN (R 3.6.0)                          
#>  hms             0.4.2       2018-03-10 [1] CRAN (R 3.5.1)                          
#>  htmltools       0.3.6       2017-04-28 [1] CRAN (R 3.5.1)                          
#>  httpuv          1.5.1       2019-04-05 [1] CRAN (R 3.6.0)                          
#>  insight         0.3.0       2019-05-11 [1] CRAN (R 3.6.0)                          
#>  jmv             0.9.6.1     2019-04-22 [1] CRAN (R 3.6.0)                          
#>  jmvcore         1.0.0       2019-05-28 [1] CRAN (R 3.6.0)                          
#>  knitr           1.23        2019-05-18 [1] CRAN (R 3.6.0)                          
#>  LaplacesDemon   16.1.1      2018-06-30 [1] CRAN (R 3.5.1)                          
#>  later           0.8.0       2019-02-11 [1] CRAN (R 3.6.0)                          
#>  lattice         0.20-38     2018-11-04 [2] CRAN (R 3.6.0)                          
#>  lazyeval        0.2.2       2019-03-15 [1] CRAN (R 3.6.0)                          
#>  libcoin         1.0-4       2019-02-28 [1] CRAN (R 3.5.2)                          
#>  lme4            1.1-21      2019-03-05 [1] CRAN (R 3.6.0)                          
#>  lmtest          0.9-37      2019-04-30 [1] CRAN (R 3.6.0)                          
#>  logspline       2.1.12      2019-02-05 [1] CRAN (R 3.5.2)                          
#>  magrittr        1.5         2014-11-22 [1] CRAN (R 3.5.1)                          
#>  manipulate      1.0.1       2014-12-24 [1] CRAN (R 3.5.0)                          
#>  MASS            7.3-51.4    2019-03-31 [1] CRAN (R 3.6.0)                          
#>  Matrix          1.2-17      2019-03-22 [1] CRAN (R 3.6.0)                          
#>  MatrixModels    0.4-1       2015-08-22 [1] CRAN (R 3.5.1)                          
#>  matrixStats     0.54.0      2018-07-23 [1] CRAN (R 3.5.1)                          
#>  mc2d            0.1-18      2017-03-06 [1] CRAN (R 3.5.1)                          
#>  metaBMA         0.3.9       2017-08-04 [1] CRAN (R 3.5.3)                          
#>  metafor         2.1-0       2019-05-14 [1] CRAN (R 3.6.0)                          
#>  mgcv            1.8-28      2019-03-21 [1] CRAN (R 3.6.0)                          
#>  mime            0.7         2019-06-11 [1] CRAN (R 3.6.0)                          
#>  miniUI          0.1.1.1     2018-05-18 [1] CRAN (R 3.5.1)                          
#>  minqa           1.2.4       2014-10-09 [1] CRAN (R 3.5.1)                          
#>  mnormt          1.5-5       2016-10-15 [1] CRAN (R 3.5.0)                          
#>  modelr          0.1.4       2019-02-18 [1] CRAN (R 3.6.0)                          
#>  modeltools      0.2-22      2018-07-16 [1] CRAN (R 3.5.1)                          
#>  multcomp        1.4-10      2019-03-05 [1] CRAN (R 3.5.2)                          
#>  multcompView    0.1-7       2015-07-31 [1] CRAN (R 3.5.1)                          
#>  munsell         0.5.0       2018-06-12 [1] CRAN (R 3.5.1)                          
#>  mvtnorm         1.0-10      2019-03-05 [1] CRAN (R 3.5.2)                          
#>  nlme            3.1-137     2018-04-07 [2] CRAN (R 3.6.0)                          
#>  nloptr          1.2.1       2018-10-03 [1] CRAN (R 3.5.1)                          
#>  nortest         1.0-4       2015-07-30 [1] CRAN (R 3.5.2)                          
#>  openxlsx        4.1.0.1     2019-05-28 [1] CRAN (R 3.6.0)                          
#>  paletteer       0.2.1.9000  2019-03-25 [1] Github (EmilHvitfeldt/paletteer@38cdb34)
#>  pbapply         1.4-0       2019-02-05 [1] CRAN (R 3.6.0)                          
#>  pcaPP           1.9-73      2018-01-14 [1] CRAN (R 3.5.0)                          
#>  performance     0.2.0       2019-06-04 [1] CRAN (R 3.6.0)                          
#>  pillar          1.4.1       2019-05-28 [1] CRAN (R 3.6.0)                          
#>  pkgconfig       2.0.2       2018-08-16 [1] CRAN (R 3.5.1)                          
#>  plyr            1.8.4       2016-06-08 [1] CRAN (R 3.5.1)                          
#>  promises        1.0.1       2018-04-13 [1] CRAN (R 3.5.1)                          
#>  psych           1.8.12      2019-01-12 [1] CRAN (R 3.6.0)                          
#>  purrr           0.3.2       2019-03-15 [1] CRAN (R 3.6.0)                          
#>  purrrlyr        0.0.5       2019-03-15 [1] CRAN (R 3.6.0)                          
#>  R6              2.4.0       2019-02-14 [1] CRAN (R 3.6.0)                          
#>  rcompanion      2.2.1       2019-05-29 [1] CRAN (R 3.6.0)                          
#>  Rcpp            1.0.1       2019-03-17 [1] CRAN (R 3.6.0)                          
#>  readxl          1.3.1       2019-03-13 [1] CRAN (R 3.6.0)                          
#>  reshape         0.8.8       2018-10-23 [1] CRAN (R 3.6.0)                          
#>  reshape2        1.4.3       2017-12-11 [1] CRAN (R 3.5.1)                          
#>  rio             0.5.16      2018-11-26 [1] CRAN (R 3.6.0)                          
#>  rjson           0.2.20      2018-06-08 [1] CRAN (R 3.5.0)                          
#>  rlang           0.3.4       2019-04-07 [1] CRAN (R 3.6.0)                          
#>  rmarkdown       1.13        2019-05-22 [1] CRAN (R 3.6.0)                          
#>  robust          0.4-18      2017-04-27 [1] CRAN (R 3.5.1)                          
#>  robustbase      0.93-5      2019-05-12 [1] CRAN (R 3.6.0)                          
#>  rrcov           1.4-7       2018-11-15 [1] CRAN (R 3.6.0)                          
#>  rstudioapi      0.10        2019-03-19 [1] CRAN (R 3.6.0)                          
#>  runjags         2.0.4-2     2016-07-25 [1] CRAN (R 3.5.2)                          
#>  sandwich        2.5-1       2019-04-06 [1] CRAN (R 3.6.0)                          
#>  scales          1.0.0       2018-08-09 [1] CRAN (R 3.5.1)                          
#>  sessioninfo     1.1.1       2018-11-05 [1] CRAN (R 3.6.0)                          
#>  shiny           1.3.2       2019-04-22 [1] CRAN (R 3.6.0)                          
#>  sjlabelled      1.1.0       2019-06-06 [1] CRAN (R 3.6.0)                          
#>  sjmisc          2.8.0       2019-06-11 [1] CRAN (R 3.6.0)                          
#>  sjstats         0.17.5      2019-06-04 [1] CRAN (R 3.6.0)                          
#>  skimr           1.0.6       2019-05-27 [1] CRAN (R 3.6.0)                          
#>  stringi         1.4.3       2019-03-12 [1] CRAN (R 3.6.0)                          
#>  stringr         1.4.0       2019-02-10 [1] CRAN (R 3.6.0)                          
#>  survival        2.43-3      2018-11-26 [2] CRAN (R 3.6.0)                          
#>  TH.data         1.0-10      2019-01-21 [1] CRAN (R 3.5.2)                          
#>  tibble          2.1.3       2019-06-06 [1] CRAN (R 3.6.0)                          
#>  tidyr           0.8.3       2019-03-01 [1] CRAN (R 3.6.0)                          
#>  tidyselect      0.2.5       2018-10-11 [1] CRAN (R 3.5.1)                          
#>  TMB             1.7.15      2018-11-09 [1] CRAN (R 3.6.0)                          
#>  withr           2.1.2       2018-03-15 [1] CRAN (R 3.5.1)                          
#>  WRS2            1.0-0       2019-06-05 [1] R-Forge (R 3.6.0)                       
#>  xfun            0.7         2019-05-14 [1] CRAN (R 3.6.0)                          
#>  xtable          1.8-4       2019-04-21 [1] CRAN (R 3.5.3)                          
#>  yaml            2.2.0       2018-07-25 [1] CRAN (R 3.5.1)                          
#>  zip             2.0.2       2019-05-13 [1] CRAN (R 3.6.0)                          
#>  zoo             1.8-6       2019-05-28 [1] CRAN (R 3.6.0)                          
#> 
#> [1] C:/Users/inp099/Documents/R/win-library/3.6
#> [2] C:/Program Files/R/R-3.6.0alpha/library

Created on 2019-06-12 by the reprex package (v0.3.0)

Upvotes: 1

Views: 166

Answers (1)

user2554330
user2554330

Reputation: 44788

It is pretty easy to convert your example to Markdown code. This is nowhere near general, but it is kind of obvious how to extend it to handle other expressions.

The idea is to evaluate the plotmath expression to form a string that is Markdown. For example, use this function:

toMarkdown <- function(e) {
  # In plotmath, paste acts like paste0
  paste <- paste0

  # Italic text just has stars around it
  italic <- function(s) paste0("*", s, "*")

  # Single subscripts are entered using subsetting
  `[` <- function(main, subscript)  paste0(main, "~", subscript, "~")

  # Evaluate the expression to produce a string
  eval(e)
}

I haven't installed the devel version of ggstatsplot, but I can duplicate your p_call:

p_call <- quote(paste(NULL, italic("t"), "(", "29.23", ") = ", "5.49", ", ", 
     italic("p"), " = ", "< 0.001", ", ", italic("g"), " = ", 
     "1.89", ", CI"["95%"], " [", "1.10", ", ", "2.83", "]", ", ", 
     italic("n"), " = ", 32L))

If I run that through toMarkdown, I get this:

> toMarkdown(p_call)
[1] "*t*(29.23) = 5.49, *p* = < 0.001, *g* = 1.89, CI~95%~ [1.10, 2.83], *n* = 32"

If I put that inline in a Markdown document using r toMarkdown(p_call) (in backticks), I get this screenshot:

screenshot

As you are the author of ggstatsplot, you should know every function that might show up in your call objects, and can expand toMarkdown to handle them all. Feel free to include it in your package.

Upvotes: 2

Related Questions