2

I am trying to create a LaTeX table using the table and Hmisc packages however, I am having trouble getting the caption to appear.

Here is a reproducible example:

    ```{r, results = "asis"}

# data:
dow <- sample(1:7, 100, replace=TRUE)
purp <- sample(1:4, 100, replace=TRUE)
dow <- factor(dow, 1:7, c("Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun"))
purp <- factor(purp, 1:4, c("Business", "Commute", "Vacation", "Other"))

dataframe <-  data.frame( dow, purp)

# The packages
library(tables)
library(Hmisc)

# The table
#tabular(  (Weekday=dow) ~  (Purpose=purp)*(Percent("row")+ 1)    ,data=dataframe        )

# The latex table
tab <- latex(  tabular(  (Weekday=dow) ~  (Purpose=purp)*(Percent("col")+ 1)    ,data=dataframe        ), caption = "This is a Caption")
print(tab)
```

The knitted table does not contain the caption. I am new to LaTeX and my only experience with LaTeX is through R and Rstudio. Any advice would be greatly appreciated.

Table produced when knitted

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
TDP
  • 335
  • 1
  • 3
  • 9
  • as far as i can see, It seems latex is from the tables package rather than Hmisc (see `?latex.tabular`) and doesnt seems to have the same options. You could capture.output of latex and add a caption manually – user20650 Jun 13 '17 at 16:34
  • see https://stackoverflow.com/questions/21541718/how-to-add-caption-with-the-tables-r-package for an alt way – user20650 Jun 13 '17 at 16:35

1 Answers1

0

when you run

tab <- latex(tabular(  (Weekday=dow) ~  (Purpose=purp)*(Percent("col")+ 1), data=dataframe), caption = "This is a Caption")

you are using the default S3 method which in this case is latex.tabular

## S3 method for class 'tabular'
latex(object, file = "", options = NULL, append = FALSE, ...)

latex.tabular does not take a caption argument

Cedric
  • 2,412
  • 17
  • 31