-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature request: reporting results in rmarkdown
#1
Comments
This is a starting point for whatever the ultimate solution might look like. The final form of the function will need to cover all kinds of objects that are going to pop up in the calls containing plotmath characters. format_markdown <- function(expr) {
# 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(expr)
}
# works
format_markdown(
ggstatsplot::gghistostats(
data = iris,
x = Sepal.Length,
test.value = 7,
output = "subtitle"
)
)
#> [1] "*t*(149) = -17.11, *p* = < 0.001, *g* = -1.39, CI~95%~ [-1.63, -1.17], *n* = 150"
format_markdown(
ggstatsplot::ggdotplotstats(
data = iris,
y = Species,
x = Sepal.Length,
type = "np",
output = "subtitle"
)
)
#> Warning in wilcox.test.default(x = data$x, alternative = "two.sided",
#> na.action = na.omit, : requested conf.level not achievable
#> Error in paste(NULL, "log"["e"](italic("V")), " = ", "1.79", ", ", italic("p"), : attempt to apply non-function Created on 2019-06-17 by the reprex package (v0.3.0) |
Dear Indrajeet, |
rmarkdown
Yes please, this would be awesome for report writing. I've check the code inside some of your packages and it seems that you could pull it out smoothly. |
Working from the shoulders of the previous example code, this function maybe can accomplish the task related to #104 format_markdown <- function(expr) {
# trasnform expression to be able to modify it
expr <- as.list(x = as.list(expr)[[1]])
# replace invalid patterns to be evaluated
expr <- gsub(")[", ") * \"\"[", expr, fixed = TRUE)
expr <- gsub("](", "] * list2(", expr, fixed = TRUE)
# transform to again to expression to evaluate it
expr <- lapply(expr, str2lang)
expr <- as.call(expr)
# global variables; could be changed for anything else
p <- "p"; CI <- "CI"; chi <- "*X*";
mu <- "*mu*"; log <- "log"; BF <- "BF";
e <- "e"; epsilon <- "Epsilon"; R <- "R";
HDI <- "HDI"; xi <- "xi"; omega <- "Omega";
# list works pasting expressions
list <- function(...) paste(..., sep = ", ")
# `list2` works the same but only for expressions with >= 1 parameter(s)
list2 <- function(...) paste0("(", paste(..., sep = ", "), ")")
# italic text just has stars around it
italic <- function(s) paste0("*", s, "*")
# wide hat has no effect on final output
widehat <- function(s) as.character(s)
# single subscripts are entered using subsetting
`[` <- function(main, subscript) paste0(main, "~", subscript, "~")
# single superscript are entered using symbol in both sides
`^` <- function(main, superscript) paste0(main, "^", superscript, "^")
# this symbol will concatenate
`*` <- function(lhs, rhs) paste0(lhs, rhs, collapse = ", ")
# replace to equal sign
`==` <- function(lhs, rhs) paste0(lhs, " = ", rhs)
# suppress formula behaviour
`~` <- function(lhs, rhs) paste0(lhs, rhs)
# evaluate the expression to produce a string
eval(expr)
} With this, a got the next outputs: gghistostatsformat_markdown(
ggstatsplot::gghistostats(
data = iris,
x = Sepal.Length,
test.value = 7,
output = "subtitle"
)
)
#> [1] "*t*~Student~(149) = -17.11, *p* = 5.56e-37, *g*~Hedges~ = -1.39, CI~95%~[-1.62, -1.17], *n*~obs~ = 150" ggdotplotstatsformat_markdown(
ggstatsplot::ggdotplotstats(
data = iris,
y = Species,
x = Sepal.Length,
type = "np",
output = "subtitle"
)
)
#> [1] "*V*~Wilcoxon~ = 6.00, *p* = 0.25, *r*~biserial~^rank^ = 1.00, CI~95%~[1.00, 1.00], *n*~obs~ = 3" ggbetweenstatsformat_markdown(
ggstatsplot::ggbetweenstats(
data = iris,
x = Species,
y = Sepal.Length,
type = "b",
output = "subtitle"
)
)
#> [1] "log~e~BF~01~ = -65.10, *R^2^*~Bayesian~^posterior^ = 0.61, CI~95%~^HDI^[0.54, 0.67], *r*~Cauchy~^JZS^ = 0.71" format_markdown(
ggstatsplot::ggbetweenstats(
data = iris,
x = Species,
y = Sepal.Length,
type = "r",
output = "subtitle"
)
)
#> [1] "*F*~trimmed-means~(2, 53.84) = 111.95, *p* = 0.00, xi = 0.85, CI~95%~[0.77, 0.91], *n*~obs~ = 150" format_markdown(
ggstatsplot::ggbetweenstats(
data = iris,
x = Species,
y = Sepal.Length,
type = "p",
output = "subtitle"
)
)
#> [1] "*F*~Welch~(2, 92.21) = 138.91, *p* = 1.51e-28, Omega~p~^2^ = 0.74, CI~95%~[0.67, 1.00], *n*~obs~ = 150" |
In response to issue IndrajeetPatil#1 this function try to solve the need of a parser function for expression objects returned by any of `{ggtatsplot}` or `{statsExpressions}` functions for reporting purposes in R Markdown
I have prepared a Pull Request with minor modifications to the original code to avoid R-CMD-check notes, as well as to work with the output from |
First of all, thanks for developing these amazing packages! This feature would still be immensely helpful to get a solution to! Most results section in journal papers requires results to be written within the text, and this could provide a perfect way to have reproducible results section, as the stats would update in case the underlying data does. IMO this should be a quite high priority. :-)
As an aside, also note the new |
@roaldarbol It's uncanny that you wrote this today, since it's only yesterday I subscribed to the Thanks for the detailed heads-up, though. This is definitely something I am going to follow closely, and hopefully it becomes easier to support LaTeX equations in marquee, which would also make it easier to support them in |
Haha, uncanny timing indeed! Fingers crossed they manage to enable it soon! :-D |
For example-
Also see:
https://easystats.github.io/report/
The text was updated successfully, but these errors were encountered: