1

我正在使用 knitr 包在 R Studio 中编写报告。我试图简洁地描述一个变量列表并包含在分析中。为了做到这一点,我在乳胶中使用了长桌包。我已在其中一个表列中插入了一个块,以显示一些数据的直方图。问题是这会破坏表格列的宽度。我已经尝试在乳胶代码中设置表格宽度,但是在超出此范围的块中发生了一些事情。

\documentclass{article}

\usepackage{float}
\usepackage{longtable}

\begin{document}

 <<ch-req, echo=FALSE,results='hide',include=FALSE>>=
require(datasets)
 @

\begin{center}
\begin{longtable}{|c|p{.45\textwidth}|c|}
\caption{Summary table for variables in 'cars' data set} \label{tab:csumTab} \\

\hline \multicolumn{1}{|c|}{\textbf{Variable Original Name}} & \multicolumn{1}{c|}{\textbf{Variable Summary}} & \multicolumn{1}{c|}{\textbf{Renamed Variable}} \\ \hline\hline
\endfirsthead

\multicolumn{3}{c}%
{{\bfseries \tablename\ \thetable{} -- continued from previous page}} \\
\hline \multicolumn{1}{|c|}{\textbf{Variable Original Name}} &
\multicolumn{1}{c|}{\textbf{Variable Summary}} &
\multicolumn{1}{c|}{\textbf{Renamed Variable}} \\ \hline 
\endhead

\hline \multicolumn{3}{r}{{Continued on next page}} \\ 
\endfoot

\hline \hline
\endlastfoot
    `speed' & numeric   Speed (mph) of the car &  \\
                &
        \begin{figure}[H]
        <<histspeed1,echo=FALSE, fig=TRUE,out.width='2in', fig.align = 'left'>>=
        hist(cars$speed)
        @
        \end{figure}
    & \\
    \hline
    `dist' & This is the numeric stopping distance of the car (ft)& `Distance'\\
                   & \begin{tabular}{|c|c|c|}
                 \hline
                 min & median & max \\
                 \hline
                 2 & 36 & 120 \\
                 \hline
                 \end{tabular}& \\
  \end{longtable}
\end{center}


\begin{center}
\begin{longtable}{|c|p{.45\textwidth}|c|}
\caption{Summary table for variables in `cars' data set} \label{tab:csumTab} \\

\hline \multicolumn{1}{|c|}{\textbf{Variable Original Name}} & \multicolumn{1}{c|}{\textbf{Variable Summary}} & \multicolumn{1}{c|}{\textbf{Renamed Variable}} \\ \hline\hline
\endfirsthead

\multicolumn{3}{c}%
{{\bfseries \tablename\ \thetable{} -- continued from previous page}} \\
\hline \multicolumn{1}{|c|}{\textbf{Variable Original Name}} &
\multicolumn{1}{c|}{\textbf{Variable Summary}} &
\multicolumn{1}{c|}{\textbf{Renamed Variable}} \\ \hline 
\endhead

\hline \multicolumn{3}{r}{{Continued on next page}} \\ 
\endfoot

\hline \hline
\endlastfoot
    `speed' & numeric   Speed (mph) of the car &  \\
                &
        \begin{figure}[H]
        %<<histspeed2,echo=FALSE, fig=TRUE,out.width='2in', fig.align = 'left'>>=
        %hist(cars$speed)
        %@
        \end{figure}
    & \\
    \hline
    `dist' & This is the numeric stopping distance of the car (ft)& `Distance'\\
                   & \begin{tabular}{|c|c|c|}
                 \hline
                 min & median & max \\
                 \hline
                 2 & 36 & 120 \\
                 \hline
                 \end{tabular}& \\
  \end{longtable}
\end{center}

\end{document}

有两个表格只是为了表明当绘图未包含在代码中时表格大小确实有效。我曾经out.width='2in'在表格中更改直方图的大小,但默认情况下仍然为绘图保留了很大的区域(?)。如果您更改fig.align = 'left'为 ,您可以更清楚地看到这一点fig.align = 'center'。我想让表格看看它在第二个例子中的表现,但结合了情节。

非常感谢任何帮助。

4

0 回答 0