0
votes

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)
5
Unable to reproduce the problem. your code generates the header without a problem.TC Zhang

5 Answers

2
votes

I had that same problem and could solve it specifying the format in the arguments for kable.

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

I had a similar issue fwiw. I realized there are multiple commands called kable in different packages. A package called 'skimr' does something different with this command, for example. When I specifically referenced the package knitr::kable, the captions appeared.

library(knitr)
library(kableExtra)
library(skimr)
row1 <- c("a", "b", "c")
row2 <- c("d", "e", "f")

Here there is no caption, because it's using the skimr version of the command

data <- as.data.frame(rbind(row1, row2))
knitr::kable(data, caption = "head") %>% kable_styling()

The following includes the header because I referred to the right (knitr) library

data <- as.data.frame(rbind(row1, row2))
kable(data, caption = "head") %>% kable_styling()
0
votes

I’d define the caption as a parameter of initiating the code chunk. For example:

‘’’{r, fig.cap = “head”}

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

‘’’
0
votes

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

0
votes

The package kableExtra manage this point effectively. Try :

library(kableExtra)

row1 <- c("a", "b", "c")
row2 <- c("d", "e", "f")

as.data.frame(rbind(row1, row2)) %>% 
 kbl(caption = "head") %>%
 kable_styling()