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.
Consider a regression model, from which we want to print the output into a paragraph.
## Warning: package 'broom' was built under R version 4.1.3
data("mtcars")
m1 <- lm(mpg ~ cyl + wt, data = mtcars)
m1_tidy <- broom::tidy(m1, conf.int = TRUE)
m1_tidy
## # A tibble: 3 x 7
## term estimate std.error statistic p.value conf.low conf.high
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 39.7 1.71 23.1 3.04e-20 36.2 43.2
## 2 cyl -1.51 0.415 -3.64 1.06e- 3 -2.36 -0.660
## 3 wt -3.19 0.757 -4.22 2.22e- 4 -4.74 -1.64
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 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 -3.2 (95% CI:-4.7–1.6).
This is true of various things in R: percentages, p-values, for example.
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_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 -3.2 (95% CI:-4.7–1.6)