yihui / printr

Some (magical) printing methods for knitr
https://yihui.org/printr/
118 stars 24 forks source link

printr labels + kable caption #15

Closed m-dz closed 8 years ago

m-dz commented 8 years ago

I cannot get both printr and kable's caption option working together. This might be a bug or I am missing something simple. An example, not sure how to format it to be more readable, so a .txt and .pdf with printed .html attached: printr_test.pdf printr_test.txt

r global options, packages etc. (as text)

{r global_options, include = FALSE} knitr::opts_chunk$set(fig.width = 6, fig.height = 4, fig.path = 'Figs/', echo = TRUE, warning = FALSE, message = FALSE, cache = TRUE, tidy = FALSE, size = "small") options(width = 10000)

{r load packages, cache = FALSE} library(printr) library(knitr)

{r session, cache = FALSE} sessionInfo()

## R version 3.2.4 Revised (2016-03-16 r70336)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 7 x64 (build 7601) Service Pack 1
## 
## locale:
## [1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252    LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                            LC_TIME=English_United Kingdom.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] knitr_1.12.3 printr_0.0.5
## 
## loaded via a namespace (and not attached):
##  [1] magrittr_1.5    formatR_1.3     tools_3.2.4     htmltools_0.3.5 yaml_2.1.13     Rcpp_0.12.4     stringi_1.0-1   rmarkdown_0.9.5 stringr_1.0.0   digest_0.6.9    evaluate_0.8.3
printr (labels)
set.seed(2016)
x1 = sample(letters[1:3], 1000, TRUE)
x2 = sample(letters[1:3], 1000, TRUE)
t <- table(x1, x2, dnn = c("Sample A", "Sample B"))
t
printr + kable (caption, NO labels...)
set.seed(2016)
x1 = sample(letters[1:3], 1000, TRUE)
x2 = sample(letters[1:3], 1000, TRUE)
t <- table(x1, x2, dnn = c("Sample A", "Sample B"))
kable(t, caption = "kable table title")
something weird

When echo = false labels on first table, caption from second table before first.

set.seed(2016)
x1 = sample(letters[1:3], 1000, TRUE)
x2 = sample(letters[1:3], 1000, TRUE)
table(x1, x2, dnn = c("Sample A", "Sample B"))
kable(table(x1, x2, dnn = c("Sample A", "Sample B")), caption = "kable table title")

With kable(NULL, ...) and echo = false this kinda looks allright...

set.seed(2016)
x1 = sample(letters[1:3], 1000, TRUE)
x2 = sample(letters[1:3], 1000, TRUE)
table(x1, x2, dnn = c("Sample A", "Sample B"))
kable(NULL, caption = "kable table title")

Any idea how to get them both (labels and caption) working together?

yihui commented 8 years ago

For the caption position, see https://github.com/yihui/knitr/issues/895

For the label Sample A / Sample B, it is not supported by kable() by default, but you can pass it via the rownames.name argument, e.g.

```{r, echo = FALSE, results='asis'}
set.seed(2016)
x1 = sample(letters[1:3], 1000, TRUE)
x2 = sample(letters[1:3], 1000, TRUE)
table(x1, x2, dnn = c("Sample A", "Sample B"))

cat('\n\n<!-- -->\n\n')

kable(table(x1, x2), rownames.name = "Sample A / Sample B", caption = "kable table title")


For how to properly format Github issues when you have the triple backticks, see https://github.com/yihui/knitr/blob/master/CONTRIBUTING.md
m-dz commented 8 years ago

Thank you, I will give it a try!