user3149973
user3149973

Reputation: 53

Include latex tables generated by Hmisc latex in knitr slides

I am sorry, I am new to using knitr to make slides. I generally use the latex() function in Hmisc package to generate my tables based on R objects. I would like to produce a slide that shows the r code and then below it displays the properly formatted table. Something like:

   ``` {r}
latex(tabdat,file="tables/tabdat.tex",ctable=TRUE,caption="A basic table",caption.loc="bottom",label="tab:dat1",row.names=NULL,rowlabel="")
```

So that the finished slide displays the exact r code and the formatted table looking exactly as if I had run latex using \input{tabdat}

I would appreciate any advice on how to accomplish this.

Thanks!

Upvotes: 2

Views: 2226

Answers (1)

Mark Heckmann
Mark Heckmann

Reputation: 11441

I am a bit puzzled because you talk about PDF/LaTeX output but you are using R markdown tags. Here are small examples for both cases, R Sweave, i.e. LaTeX output, and R markdown, i.e. HTML output. For creating the LaTeX code there are several packages available (xtable, Hmisc etc.) for HTML AFAIK only xtable.

The main point of how to include the raw output just like it appears in the console is the same for both output types and was already explained by Tyler Rinker above, i.e. by adding results="asis" to the chunk options.

PDF/LaTeX / Rnw-file

\documentclass{article}
\begin{document}

<<echo=FALSE, message=FALSE>>=
library(Hmisc)
library(xtable)
@

<<results='asis'>>=
latex(head(iris), file = '')
@

<<results='asis'>>=
xtable(head(iris))
@

\end{document}

HTML, Rmd-file

```{r echo=FALSE}
library(xtable)
```

```{r results='asis'}
tbl <- xtable(head(iris))
print(tbl, type="html")
```

Look here for more examples and options: http://www.stat.iastate.edu/centers/CCGS/slides/slides-Rtables.pdf

Upvotes: 4

Related Questions