Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/nutterb/pixiedust

Tables So Beautifully Fine-Tuned You Will Believe It's Magic.
https://github.com/nutterb/pixiedust

Last synced: about 2 months ago
JSON representation

Tables So Beautifully Fine-Tuned You Will Believe It's Magic.

Awesome Lists containing this project

README

        

---
output:
md_document:
variant: markdown_github
---

```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```

# pixiedust
After tidying up your analyses with the `broom` package, go ahead and grab the `pixiedust`. Customize your table output and write it to markdown, HTML, LaTeX, or even just the console. `pixiedust` makes it easy to customize the appearance of your tables in all of these formats by adding any number of "sprinkles", much in the same way you can add layers to a `ggplot`.

```{r}
fit <- lm(mpg ~ qsec + factor(am) + wt + factor(gear), data = mtcars)
library(pixiedust)
dust(fit) %>%
sprinkle(col = 2:4, round = 3) %>%
sprinkle(col = 5, fn = quote(pvalString(value))) %>%
sprinkle_colnames(term = "Term",
estimate = "Estimate",
std.error = "SE",
statistic = "T-statistic",
p.value = "P-value") %>%
sprinkle_print_method("console")
```

### Customizing with Sprinkles

Tables can be customized by row, column, or even by a single cell by adding sprinkles to the `dust` object. The table below shows the currently planned and implemented sprinkles. In the "implemented" column, an 'x' indicates a customization that has been implemented, while a blank cell suggests that the customization is planned but has not yet been implemented. In the remaining columns, an 'x' indicates that the sprinkle is already implemented for the output format; an 'o' indicates that implementation is planned but not yet completed; and a blank cell indicates that the sprinkle will not be implemented (usually because the output format doesn't support the option).

```{r, echo=FALSE}
Sprinkles <- read.csv("inst/sprinkles.csv",
stringsAsFactors=FALSE)
Sprinkles[,-1] <- lapply(Sprinkles[-1], stringr::str_trim)

dust(Sprinkles) %>%
sprinkle(cols=1, halign = "left") %>%
sprinkle(cols=2:6, halign = "center") %>%
sprinkle_print_method("markdown")
```

### A Brief Example

To demonstrate, let's look at a simple linear model. We build the model and generate the standard summary.

```{r}
fit <- lm(mpg ~ qsec + factor(am) + wt + factor(gear), data = mtcars)

summary(fit)
```

While the summary is informative and useful, it is full of "stats-speak" and isn't necessarily in a format that is suitable for publication or submission to a client. The `broom` package provides the summary in tidy format that, serendipitously, it a lot closer to what we would want for formal reports.

```{r}
library(broom)
tidy(fit)
```

It has been observed by some, however, that even this summary isn't quite ready for publication. There are too many decimal places, the p-value employ scientific notation, and column titles like "statistic" don't specify what type of statistic. These kinds of details aren't the purview of `broom`, however, as `broom` is focused on tidying the results of a model for further analysis (particularly with respect to comparing slightly varying models).

The `pixiedust` package diverts from `broom`'s mission here and provides the ability to customize the `broom` output for presentation. The initial `dust` object returns a table that is similar to the `broom` output.

```{r}
library(pixiedust)
dust(fit) %>%
sprinkle_print_method("console")
```

Where `pixiedust` shows its strength is the ease of which these tables can be customized. The code below rounds the columns `estimate`, `std.error`, and `statistic` to three decimal places each, and then formats the `p.value` into a format that happens to be one that I like.

```{r}
x <- dust(fit) %>%
sprinkle(col = 2:4, round = 3) %>%
sprinkle(col = 5, fn = quote(pvalString(value))) %>%
sprinkle_print_method("console")
x
```

Now we're almost there! Let's change up the column names, and while we're add it, let's add some "bold" markers to the statistically significant terms in order to make them stand out some (I say "bold" because the console output doesn't show up in bold, but with the markdown tags for bold text. In a rendered table, the text would actually be rendered in bold).

```{r}
x <- x %>%
sprinkle(col = c("estimate", "p.value"),
row = c(2, 4),
bold = TRUE) %>%
sprinkle_colnames(term = "Term",
estimate = "Estimate",
std.error = "SE",
statistic = "T-statistic",
p.value = "P-value") %>%
sprinkle_print_method("console")

x
```

## A cool, free tip!

The markdown output from `pixiedust` is somewhat limited due to the limitations of `Rmarkdown` itself. If/when more features become available for `Rmarkdown` output, I'll be sure to include them. But what can you do if you _really_ want all of the flexibility of the HTML tables but need the MS Word document?

With a little help from the `Gmisc` package, you can have the best of both worlds. `Gmisc` isn't available on CRAN yet, but if you're willing to install it from GitHub, you can render a `docx` file. Install `Gmisc` with

`install.packages("Gmisc")`

Then use in your YAML header

```
---
output: Gmisc::docx_document
---
```

When you knit your document, it knits as an HTML file, but I've had no problems with the rendering when I right-click the file and open with MS Word.

Read more at http://gforge.se/2014/07/fast-track-publishing-using-rmarkdown/ (but note that this blog post was written about the `Grmd` package before it was moved into the `Gmisc` package).