1
votes

Im using bookdown to write a thesis. Kable() does not produce captions, neither when used in the bookdown context nor when run in the R console.

When i run the following in R i get a table without caption:

knitr::kable(
  head(mtcars[, 1:4], 4), booktabs = TRUE,
  caption = 'A table.'
)

The markdown output looks like this:

|               |  mpg| cyl| disp|  hp|
|:--------------|----:|---:|----:|---:|
|Mazda RX4      | 21.0|   6|  160| 110|
|Mazda RX4 Wag  | 21.0|   6|  160| 110|
|Datsun 710     | 22.8|   4|  108|  93|
|Hornet 4 Drive | 21.4|   6|  258| 110|

I can get the caption by adding format = 'pandoc' but then the table will not work properly in bookdown (it does not respect page margins):

knitr::kable(
           head(mtcars[, 1:4], 4), booktabs = TRUE,
           format = 'pandoc',
  caption = 'A table.'
)

The above gives this output:

Table: A table.

                   mpg   cyl   disp    hp
---------------  -----  ----  -----  ----
Mazda RX4         21.0     6    160   110
Mazda RX4 Wag     21.0     6    160   110
Datsun 710        22.8     4    108    93
Hornet 4 Drive    21.4     6    258   110

Any help would be greatly appreciated! My session info:

> devtools::session_info('rmarkdown')
Session info ------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.2 (2017-09-28)
 system   x86_64, linux-gnu           
 ui       X11                         
 language                             
 collate  sv_SE.UTF-8                 
 tz       Europe/Stockholm            
 date     2018-03-24                  

Packages ----------------------------------------------------------------------
 package   * version date       source         
 backports   1.1.0   2017-05-22 CRAN (R 3.4.0) 
 base64enc   0.1-3   2015-07-28 CRAN (R 3.4.0) 
 digest      0.6.12  2017-01-27 CRAN (R 3.4.0) 
 evaluate    0.10    2016-10-11 CRAN (R 3.4.0) 
 graphics  * 3.4.2   2017-09-28 local          
 grDevices * 3.4.2   2017-09-28 local          
 highr       0.6     2016-05-09 CRAN (R 3.4.0) 
 htmltools   0.3.6   2017-04-28 CRAN (R 3.4.0) 
 jsonlite    1.5     2017-06-01 CRAN (R 3.4.0) 
 knitr     * 1.20    2018-02-20 CRAN (R 3.4.2) 
 magrittr    1.5     2014-11-22 CRAN (R 3.4.0) 
 markdown    0.8     2017-04-20 CRAN (R 3.4.0) 
 methods   * 3.4.2   2017-09-28 local          
 mime        0.5     2016-07-07 CRAN (R 3.4.0) 
 Rcpp        0.12.13 2017-09-28 cran (@0.12.13)
 rmarkdown * 1.9     2018-03-01 CRAN (R 3.4.2) 
 rprojroot   1.2     2017-01-16 CRAN (R 3.4.0) 
 stats     * 3.4.2   2017-09-28 local          
 stringi     1.1.5   2017-04-07 CRAN (R 3.4.0) 
 stringr   * 1.2.0   2017-02-18 CRAN (R 3.4.0) 
 tools       3.4.2   2017-09-28 local          
 utils     * 3.4.2   2017-09-28 local          
 yaml        2.1.14  2016-11-12 CRAN (R 3.4.0) 
1
Why are you outputting to markdown? I assume if you are writing a thesis you want the end result to be a PDF. Therefore you can use LaTeX to produce your tables.Michael Harper
It would help to provide a minimal working example which includes the YAML and code chunk headers. In its current form, it is difficult to provide any further help.Michael Harper
I use markdown with knitr for integrating text, tables, figures and citations, since it is less cumbersome than LaTeX. My workaround to the problem, however, was to add "latex" format specification to the kable() function, although I guess that should not be necessary if it was working as intended, and it has some drawbacks (similar to using "pandoc" format as discussed in the post above).Erik
For a minimal working example, just running the first code snippet above in the R consoloe should suffice. It should output a "Table: A table" row above the table, which currently it does not.Erik

1 Answers

0
votes

In markdown you can't see the caption. (But in HTML and PDF). As @Mikey Harper in his comment to your question already mentioned: Why are you preoccupied with outputting to markdown?