/
nicethings.Rmd
58 lines (40 loc) · 2.04 KB
/
nicethings.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
---
title: "nicethings"
author: "Simon Thelwall"
date: "`r nicethings::yaml_today()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{nicethings}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
# 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.
```{r, echo=FALSE}
library(nicethings)
```
## Regression output
### The problem
Consider a regression model, from which we want to print the output into a paragraph.
```{r, echo=TRUE}
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])`