nicethings

This package draws together a bunch of functions I've written to facilitate nicely formatting values calculated in R for use in Rmarkdown reports.

Using values calculated in R for printing can be long-winded. Often to get values into a format that is nice to read involves multiple nested functions. This makes inline code difficult to write and difficult to read. nicethings aims to solve this by providing simple wrappers for common tasks.

library(nicethings)

Regression output

The problem

Consider a regression model, from which we want to print the output into a paragraph.

library(broom)
data("mtcars")
m1 <- lm(mpg ~ cyl + wt, data = mtcars)
m1_tidy <- broom::tidy(m1, conf.int = TRUE)
m1_tidy

Say we want to print the estimate for the effect of weight on the mpg, with the 95% confidence intervals. To do this using base R the code might look something like this:

Vehicle weight was significantly associated with decreased fuel efficiency r nice_inline_r("paste0(round(m1_tidy[3,2], 1), \" (95% CI:\", round(m1_tidy[3,6], 1), \"-\", round(m1_tidy[3,7], 1), \")\")")

which would render as:

Vehicle weight was significantly associated with decreased fuel efficiency r paste0(round(m1_tidy[3,2], 1), " (95% CI:", round(m1_tidy[3,6], 1), "-", round(m1_tidy[3,7], 1), ")").

This is true of various things in R: percentages, p-values, for example.

A solution

A nicer approach is to wrap these up into a function and do something like:

Vehicle weight was significantly associated with decreased fuel efficiency r nice_inline_r("nice_estimate(m1_tidy[3,2], m1_tidy[3,6], m1_tidy[3,7])")

which would render as:

Vehicle weight was significantly associated with decreased fuel efficiency r nice_estimate(m1_tidy[3,2], m1_tidy[3,6], m1_tidy[3,7])



simonthelwall/nicethings documentation built on Feb. 27, 2023, 3:15 p.m.