-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
123 lines (77 loc) · 4.42 KB
/
README.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# table.glue
<!-- badges: start -->
`r badger::badge_cran_checks("table.glue")`
`r badger::badge_cran_download("table.glue", "last-month", "green")`
[![DOI](https://zenodo.org/badge/291551786.svg)](https://zenodo.org/badge/latestdoi/291551786)
`r badger::badge_dependencies("table.glue")`
<!-- badges: end -->
<!-- Note:
for code coverage, go to
https://www.r-bloggers.com/2017/06/how-to-add-code-coverage-codecov-to-your-r-package/ and follow instructions
-->
The goal of `table.glue` is to give you more control over the presentation of your data and also simplify the process of rounding, formatting, and presenting your data. The main idea is to create rounding specifications (starting with `round_spec()`) that can be plugged in, directly or through global options, to the `table_glue()` and `table_value()` functions.
## Installation
You can install the released version of table.glue from [CRAN](https://CRAN.R-project.org) with:
``` r
install.packages("table.glue")
```
You can install the development version from [GitHub](https://github.com/) with:
``` r
# install.packages("remotes")
remotes::install_github("bcjaeger/table.glue")
```
## Example
Suppose we want to write a summary statement about the mean and standard deviation (SD) of a column in the `mtcars` data. We'll start by loading the `glue`, `table.glue`, and `magrittr` packages.
```{r}
library(glue) # for comparisons with table_glue
library(table.glue) # similar to glue but with built in rounding
library(magrittr) # make rounding specifications using %>%
```
### Use base R
The classic approach is to use base R functions `format`, `round`, and `paste`:
```{r}
col_name <- "mpg"
col_mean <- mean(mtcars[[col_name]])
col_sd <- sd(mtcars[[col_name]])
col_mean_pretty <- format(signif(col_mean, digits = 3), nsmall = 1)
col_sd_pretty <- format(signif(col_sd, digits = 3), nsmall = 1)
paste0("The mean (SD) of ", col_name, " is ",
col_mean_pretty, " (", col_sd_pretty, ") ")
```
This gets the job done! Still, the code may be a little hard to read for a user who isn't a grizzled `paste()` veteran. This is where the `glue` package is really useful.
### Use `glue()`
Instead of using `paste()`, `glue()` lets us write everything in one string, surrounding R object with curly brackets (i.e., "look at this {`R object`}" ) tells R that the `glue()` function should print the value of that R object rather than the raw string. For instance,
```{r}
glue("the mean (SD) of {col_name} is {col_mean_pretty} ({col_sd_pretty})")
```
This is certainly more readable and clean. The only thing `glue()` doesn't do is make the pretty versions of `col_mean` and `col_sd`. This is where `table.glue` comes in.
### Use `table_glue()`
The `table.glue` package lets you use `glue()` without having to make numbers pretty beforehand. For example, the code below uses `table_glue()`, one of the main functions in `table.glue`, to replicate the results we got from `glue()` but without using the pretty versions of `col_mean` and `col_sd`.
```{r example}
# notice that we are not using 'pretty' versions of col_mean and col_sd
table_glue("the mean (SD) of {col_name} is {col_mean} ({col_sd})")
```
### Summary
- `table_glue` applies a general rounding convention to all numeric data that are supplied in the input string.
- The goal is to combine the clean syntax of `glue()` with a convenient and generally acceptable rounding specification.
Hopefully, most of your rounding needs will be met without going any further than this. However, the rabbit hole does go deeper. Let's say you don't like the default rounding specification and you want to make your own. You can do that!
```{r}
rspec <- round_spec() %>% # make your own rounding specification
round_using_signif(digits = 2) # round to 2 significant digits
# table glue adopts all the rules given by your specification
table_glue("the mean (SD) of {col_name} is {col_mean} ({col_sd})",
rspec = rspec)
```
rounding specifications can also be passed into global options so that `table_glue()` and `table_value()` will use the specification automatically (see the [Setting a default rounding specification](https://bcjaeger.github.io/table.glue/articles/default_rounder.html) vignette)