Domanda

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.

È stato utile?

Soluzione

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}

Altri suggerimenti

Per @nograpes answer, you need to have the \\ endlines on a separate line from the leading @.

The reason why the plot is moved off to the right is that, by default, the R code chunk is shown. To override this set echo=FALSE. Also note that you can use the \Sexpr{ r code goes here } syntax to put small bits of R code inline. This can help keep your table code neat.

See below:

 \documentclass{article}
 \usepackage{float}

\begin{document}

\begin{table}
    \begin{tabular}{ll}
    A &
    <<result1, echo=FALSE>>=
    plot(1,1)
    @
    \\
    B & \Sexpr{table(rnorm(10))}
    \\
    \end{tabular}
\end{table}

\end{document}

Hope this helps!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top