Search code examples
rknitrtabular

placing knitr chunks including plots inside a latex layout (table)


I'm having difficulties inserting R output within a layout based on tabular.

\documentclass{article}
\usepackage{float}

\begin{document}

    \begin{table}
        \begin{tabular}{ll}
        A & 
        <<results1>>=
        plot(1,1)
        @ \\
        B & 
        <<results2>>=
        table(rnorm(10))
        @
        \end{tabular}
    \end{table}

\end{document}

either knitr or latex chokes because the newline/chunk syntax etc. is all wrong.

I got something to work with minipage, but I need more freedom in the layout. A workaround would be to use brew, alone or before knitr, but I wonder if there are easier alternatives.


Solution

  • I think you need to place the \\ on a new line. I think @ has to be on its own. As well, you need another \\ on the second table row. This appears to work for me:

    Update: Thanks to @Statwonk I realized that the echo=FALSE will prevent the R code from displaying.

    \documentclass{article}
    \usepackage{float}
    
    \begin{document}
    
        \begin{table}
            \begin{tabular}{ll}
            A & 
            <<results1, echo=FALSE>>=
            plot(1,1)
            @ 
            \\
            B & 
            <<results2, echo=FALSE>>=
            table(rnorm(10))
            @
            \\
            \end{tabular}
        \end{table}
    
    \end{document}