Search code examples
rr-markdownknitrkable

Knitr Kable Table not producing captions in R markdown


I've read the various posts on this subject but am still having an issue. I can produce a nice-looking table when exporting to pdf or html, but it doesn't include the caption!

row1 <- c("a", "b", "c")
row2 <- c("d", "e", "f")
data <- as.data.frame(rbind(row1, row2))
kable(data, caption = "head")

If I try to run the code, I don't get the desired caption.

enter image description here

My session info is below.

Session info -------------------------------------------------------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.3 (2017-11-30)
 system   x86_64, mingw32             
 ui       RStudio (1.1.423)           
 language en                          
 collate  English_United States.1252  
 tz       America/Los_Angeles         
 date     2018-10-03                  

Packages -----------------------------------------------------------------------------------------------------------------------------------------------------------------
 package   * version date       source        
 backports   1.1.2   2017-12-13 CRAN (R 3.4.3)
 base64enc   0.1-3   2015-07-28 CRAN (R 3.4.1)
 digest      0.6.15  2018-01-28 CRAN (R 3.4.3)
 evaluate    0.10.1  2017-06-24 CRAN (R 3.4.4)
 glue        1.2.0   2017-10-29 CRAN (R 3.4.4)
 graphics  * 3.4.3   2017-12-06 local         
 grDevices * 3.4.3   2017-12-06 local         
 highr       0.7     2018-06-09 CRAN (R 3.4.4)
 htmltools   0.3.6   2017-04-28 CRAN (R 3.4.4)
 jsonlite  * 1.5     2017-06-01 CRAN (R 3.4.4)
 knitr       1.20    2018-02-20 CRAN (R 3.4.4)
 magrittr    1.5     2014-11-22 CRAN (R 3.4.4)
 markdown    0.8     2017-04-20 CRAN (R 3.4.4)
 methods   * 3.4.3   2017-12-06 local         
 mime        0.5     2016-07-07 CRAN (R 3.4.1)
 Rcpp        0.12.16 2018-03-13 CRAN (R 3.4.4)
 rmarkdown   1.10    2018-06-11 CRAN (R 3.4.4)
 rprojroot   1.3-2   2018-01-03 CRAN (R 3.4.4)
 stats     * 3.4.3   2017-12-06 local         
 stringi     1.1.7   2018-03-12 CRAN (R 3.4.4)
 stringr   * 1.3.0   2018-02-19 CRAN (R 3.4.4)
 tinytex     0.7     2018-08-22 CRAN (R 3.4.4)
 tools       3.4.3   2017-12-06 local         
 utils     * 3.4.3   2017-12-06 local         
 xfun        0.3     2018-07-06 CRAN (R 3.4.4)
 yaml        2.1.18  2018-03-08 CRAN (R 3.4.4)

I think it might have something to do with the packages that are installed?

library(httr)
library(dplyr)
library(scales)
library(tidyverse)
library(jsonlite)
library(skimr)
library(lubridate)
library(AER)
library(MASS)
library(countreg)

knitr::opts_chunk$set(tidy.opts=list(width.cutoff=60),tidy=TRUE)
knitr::opts_chunk$set(echo = TRUE)

Solution

  • I figured out that I had to load the knitr package at the beginning of the RMD file. Then, the caption appeared. Thanks!