## load packages
library(tidyverse)
library(tidytext)
library(here)
Let’s read in the Pam-bigram data (which you can access for your own amusement here).
## read csv file
pam_bigrams <- read_csv("data/pam_bigrams.csv")
Now we’ll use the sample_n() function from dplyr to get 15 (though you could use any number) observations/rows from our pamgram data. Note that the freq variable contains the number of times a given bigram occurred in the “PAM” corpus (which, as of this writing, includes all of Archer, seasons 1 - 7).
sample_n(pam_bigrams, 15)
## # A tibble: 15 x 3
## bigram freq speaker
## <chr> <int> <chr>
## 1 hypnotized people 1 PAM
## 2 wooden stakes 1 PAM
## 3 pop pow 1 PAM
## 4 good's happening 1 PAM
## 5 wh hey 2 PAM
## 6 chuckling fluid 1 PAM
## 7 equal shares 1 PAM
## 8 life advice 1 PAM
## 9 people fill 1 PAM
## 10 holy shit 7 PAM
## 11 bionic legs 1 PAM
## 12 sterile conditions 1 PAM
## 13 coca fields 1 PAM
## 14 fister roboto 1 PAM
## 15 hunh unh 1 PAM
Since pretty much every pair of words Pam utters is priceless, let’s check out some of the different ways you can display data frames as tables in R Markdown. If you’re looking at this in markdown (.md file), what you see may not match the descriptions. This makes sense, and is worth paying attention to for things like GitHub’s README.md files, and the like.
kableFirst, we might as well make use of the knitr::kable function, since we’re knitting this document anyhow. Note that the chunk option results='asis' needs to be set so that our output isn’t further processed by knitr when we pull our document together.1
library(knitr)
kable(sample_n(pam_bigrams, 15), caption = "kable pamgrams")
| bigram | freq | speaker |
|---|---|---|
| gummi bears | 1 | PAM |
| ike turner.i | 1 | PAM |
| god hates | 1 | PAM |
| boom ahem | 1 | PAM |
| frickin zod | 1 | PAM |
| suckin bullshit | 1 | PAM |
| tinker toy | 1 | PAM |
| claws rowr | 1 | PAM |
| inspiring story | 1 | PAM |
| leavin yer | 1 | PAM |
| gal’s love | 1 | PAM |
| pinky swear | 1 | PAM |
| yeah whatcha | 1 | PAM |
| ha ha | 6 | PAM |
| heard coming | 1 | PAM |
Ok, we’ve got bold headers, and lines between rows – basically, things are looking a little more table-y than the did before. The kable() function can take other formatting arguments, but we’re just gonna leave it at the defaults here.
panderpander is another R Pandoc writer. It supports four Pandoc formats for tables (which can be specified using pandoc.table2), and you may or may not want to add some information about that in your frontmatter/YAML. You can also make more adjustments to your table formatting using panderOptions().
library(pander)
## multiline table by default
pandoc.table(sample_n(pam_bigrams, 5))
##
## ------------------------------------
## bigram freq speaker
## ------------------- ------ ---------
## pam's hot 1 PAM
##
## separate sessions 1 PAM
##
## dicknuts hrmmmff 1 PAM
##
## kill decepticon 1 PAM
##
## dump time 1 PAM
## ------------------------------------
## simple tables
pandoc.table(sample_n(pam_bigrams, 5), style = "simple")
##
##
## bigram freq speaker
## ------------------ ------ ---------
## kills jermaine 1 PAM
## bodied woman 1 PAM
## dirty floor 1 PAM
## frazier's dumb 1 PAM
## explain compound 1 PAM
## grid tables
pandoc.table(sample_n(pam_bigrams, 5), style = "grid")
##
##
## +-----------------+------+---------+
## | bigram | freq | speaker |
## +=================+======+=========+
## | blow pam's | 1 | PAM |
## +-----------------+------+---------+
## | wan attorney | 1 | PAM |
## +-----------------+------+---------+
## | ass drowning | 1 | PAM |
## +-----------------+------+---------+
## | clock yer | 1 | PAM |
## +-----------------+------+---------+
## | happenin sunday | 1 | PAM |
## +-----------------+------+---------+
## rmarkdown tables (aka pipe table format)
pandoc.table(sample_n(pam_bigrams, 5), style = "rmarkdown")
##
##
## | bigram | freq | speaker |
## |:-------------:|:----:|:-------:|
## | quality dump | 1 | PAM |
## | ingrown hairs | 1 | PAM |
## | pacman jones | 2 | PAM |
## | ekblad jr | 1 | PAM |
## | wilfrid noyce | 1 | PAM |
There are a bunch of other options you can play around with as well, such as captions, cell highlighting, cell alignment, and width.
xtableThe xtable package has been around for a while, and it has a ton of formatting options for both LaTeX and HTML output. (Not to be confused with a tonne of options, which – how does one even measure formatting options in kilos?) The output defaults to latex, and, as with kable, we’ll need to be sure to include results="asis" in our chunk options.
library(xtable)
xpam <- xtable(sample_n(pam_bigrams, 15))
print(xpam, type="html")
| bigram | freq | speaker | |
|---|---|---|---|
| 1 | thousand divided | 1 | PAM |
| 2 | flow toilets | 1 | PAM |
| 3 | marley chuckles | 1 | PAM |
| 4 | cb handle | 1 | PAM |
| 5 | forfeit league | 1 | PAM |
| 6 | whatcha waiting | 1 | PAM |
| 7 | dump time | 1 | PAM |
| 8 | werewolf aw | 1 | PAM |
| 9 | krieger’s selling | 1 | PAM |
| 10 | cocaine online | 1 | PAM |
| 11 | balls taste | 1 | PAM |
| 12 | low flow | 1 | PAM |
| 13 | skinny lying | 1 | PAM |
| 14 | phuh phuh | 3 | PAM |
| 15 | downstairs neighbors | 1 | PAM |
Yeah, so, the default doesn’t look so awesome right now. However, this is really just an HTML table, so it can be easily styled as such (in addition to using the xtable parameters).
huxtableLike xtable, huxtable deals in both LaTeX and HTML, and has a ton of properties you can adjust to your liking (you can even use the magrittr-style pipe operator, %>%, to do so). The number format defaults to two decimal places, so we’ll set that manually here. In order for the column names to show up in your huxtable, they need to be added to the data frame itself. If you’re working in R Markdown, it will look like they show up twice, but that shouldn’t be the case once you’ve knit your document.
library(huxtable)
hpam <- hux(sample_n(pam_bigrams, 15)) %>%
huxtable::add_colnames()
number_format(hpam) <- 0
hpam
| bigram | freq | speaker |
| ringing happy | 1 | PAM |
| krieger shuckin | 1 | PAM |
| steering wheel's | 1 | PAM |
| list uh | 1 | PAM |
| phone call | 1 | PAM |
| pretty sad | 1 | PAM |
| paper stuck | 1 | PAM |
| amazing driver | 1 | PAM |
| frickin starving | 1 | PAM |
| pam hangs | 1 | PAM |
| stayin alive | 1 | PAM |
| hai hai | 2 | PAM |
| monster truck | 1 | PAM |
| wait till | 2 | PAM |
| guido sardoochbag | 1 | PAM |
Though it’s a bit clunky for such simple output, there are more options, such as conditional formatting, as well as some very fine-tuned controls that may make huxtable a good choice for you.
printrYihui Xie, the author of knitr, and a bevy of other useful R packages (including most of the ones ending in “-down”), also created the printr package as a “companion” to knitr. And, as far as data frames, tibbles, and the like go, the output will look the same as it does using kable() without printr attached. However, it will make a difference in terms of the way things like matrices, or lists of vignettes, and datasets are displayed.
library(printr)
kable(sample_n(pam_bigrams, 15), caption = "printr pamgrams")
| bigram | freq | speaker |
|---|---|---|
| smelly ass | 1 | PAM |
| sister edie | 3 | PAM |
| bailed triflin | 1 | PAM |
| heh head | 1 | PAM |
| follow kenny | 1 | PAM |
| matter ya | 1 | PAM |
| whatcha waiting | 1 | PAM |
| snacks enable | 1 | PAM |
| legally binding | 1 | PAM |
| horn honks | 1 | PAM |
| disease means | 1 | PAM |
| hunh unh | 1 | PAM |
| quick learner | 1 | PAM |
| yagoat raping | 1 | PAM |
| masturbate til | 1 | PAM |
vignette(package = "printr")
| Item | Title |
|---|---|
| printr | An Introduction to the printr Package (source, html) |
DTDT, from RStudio, provides “an R interface to the DataTables library.” It’s different from the other options discussed here, as it creates an HTML widget in which it displays the data. This means that it can be interactive– so, explore all the great Pam-ness, below.
library(DT)
datatable(pam_bigrams)
RJSplotRJSplot is new to me, but while perusing their gallery of some pretty cool interactive graphics, I happened upon their data table interface. Though this generates its own, separate DataTable page, it gives viewers the ability to download their data in several formats (as shown in the image of the generated DataTable page).
library(RJSplot)
# Create a data table
tables_rjs(pam_bigrams)
## The graph has been generated in the DataTable folder.
sessionInfo()
## R version 3.4.0 (2017-04-21)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS Sierra 10.12.5
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] RJSplot_2.1 DT_0.2.12 printr_0.1
## [4] huxtable_0.3.0 xtable_1.8-2 pander_0.6.0
## [7] knitr_1.16 here_0.0-6 tidytext_0.1.2.900
## [10] forcats_0.2.0 stringr_1.2.0 dplyr_0.5.0
## [13] purrr_0.2.2.2 readr_1.1.1 tidyr_0.6.3
## [16] tibble_1.3.1 ggplot2_2.2.1 tidyverse_1.1.1.9000
##
## loaded via a namespace (and not attached):
## [1] reshape2_1.4.2.9000 haven_1.0.0 lattice_0.20-35
## [4] colorspace_1.3-2 htmltools_0.3.6 SnowballC_0.5.1
## [7] yaml_2.1.14 rlang_0.1.1.9000 foreign_0.8-68
## [10] DBI_0.6-1 modelr_0.1.0 readxl_1.0.0
## [13] plyr_1.8.4 munsell_0.4.3 gtable_0.2.0
## [16] cellranger_1.1.0 rvest_0.3.2.9000 htmlwidgets_0.8
## [19] psych_1.7.5 evaluate_0.10 httpuv_1.3.3
## [22] crosstalk_1.0.0 parallel_3.4.0 highr_0.6
## [25] broom_0.4.2 tokenizers_0.1.4.9000 Rcpp_0.12.11
## [28] scales_0.4.1.9000 backports_1.1.0 jsonlite_1.4
## [31] mime_0.5 mnormt_1.5-5 hms_0.3
## [34] digest_0.6.12 stringi_1.1.5 shiny_1.0.3.9000
## [37] grid_3.4.0 rprojroot_1.2 tools_3.4.0
## [40] magrittr_1.5 lazyeval_0.2.0.9000 janeaustenr_0.1.4
## [43] Matrix_1.2-10 xml2_1.1.1 lubridate_1.6.0
## [46] assertthat_0.2.0 rmarkdown_1.5.9000 httr_1.2.1.9000
## [49] rstudioapi_0.6 R6_2.2.1 nlme_3.1-131
## [52] compiler_3.4.0
See RStudio’s R Markdown – Tables documentation for more on this.↩
By default, it will use mulitiline format, but there’s not much in the way of visuals for that with these bigrams.↩