捕获 R 输出并替换为 LaTeX 代码
Capture R output and replace with LaTeX code
我正在尝试捕获一些 R 代码的输出并将其替换为乳胶代码。
如果您运行此代码:
library(stargazer)
x <- capture.output(stargazer(mtcars[1:5, 1:3], summary = FALSE, title="The main caption of the table."))
x
这是输出:
[1] ""
[2] "% Table created by stargazer v.5.1 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu"
[3] "% Date and time: Sat, Jun 27, 2015 - 11:36:07"
[4] "\begin{table}[!htbp] \centering "
[5] " \caption{The main caption of the table.} "
[6] " \label{} "
[7] "\begin{tabular}{@{\extracolsep{5pt}} cccc} "
[8] "\\[-1.8ex]\hline "
[9] "\hline \\[-1.8ex] "
[10] " & mpg & cyl & disp \\ "
[11] "\hline \\[-1.8ex] "
[12] "Mazda RX4 & $ & $ & 0$ \\ "
[13] "Mazda RX4 Wag & $ & $ & 0$ \\ "
[14] "Datsun 710 & .800$ & $ & 8$ \\ "
[15] "Hornet 4 Drive & .400$ & $ & 8$ \\ "
[16] "Hornet Sportabout & .700$ & $ & 0$ \\ "
[17] "\hline \\[-1.8ex] "
[18] "\end{tabular} "
[19] "\end{table} "
我需要将第 5 行替换为:
" \caption[short caption]{The main caption of the table.} "
我该怎么做?
尝试:
x <- sub("\caption{The main caption of the table.}",
"\caption[short caption]{The main caption of the table.}", fixed = TRUE, x)
这与您的想法略有不同,但也许您可以只使用具有 caption.width
参数的 xtable
,例如:
print.xtable(xtable(mtcars[1:5, 1:3],
caption="The main caption of the table"),
caption.width="10cm",
caption.placement="top")
输出不会完全移植到你想要的东西,但如果你喜欢更简洁的代码,也许你可以根据你的目的改变它;来自 ?print.xtable
:
The caption will be placed in a "parbox" of the specified width if caption.width is not NULL and type="latex". Default value is NULL.
这是输出:
% latex table generated in R 3.1.3 by xtable 1.7-4 package
% Tue Jun 30 14:52:06 2015
\begin{table}[ht]
\centering
\parbox{5cm}{\caption{The main caption of the table}}
\begin{tabular}{rrrr}
\hline
& mpg & cyl & disp \
\hline
Mazda RX4 & 21.00 & 6.00 & 160.00 \
Mazda RX4 Wag & 21.00 & 6.00 & 160.00 \
Datsun 710 & 22.80 & 4.00 & 108.00 \
Hornet 4 Drive & 21.40 & 6.00 & 258.00 \
Hornet Sportabout & 18.70 & 8.00 & 360.00 \
\hline
\end{tabular}
\end{table}
您还必须弄乱其他选项(例如,digits
)以获得 table 其余部分的确切格式,以匹配 stargazer
中的格式,具体取决于您想要的格式。
我正在尝试捕获一些 R 代码的输出并将其替换为乳胶代码。
如果您运行此代码:
library(stargazer)
x <- capture.output(stargazer(mtcars[1:5, 1:3], summary = FALSE, title="The main caption of the table."))
x
这是输出:
[1] ""
[2] "% Table created by stargazer v.5.1 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu"
[3] "% Date and time: Sat, Jun 27, 2015 - 11:36:07"
[4] "\begin{table}[!htbp] \centering "
[5] " \caption{The main caption of the table.} "
[6] " \label{} "
[7] "\begin{tabular}{@{\extracolsep{5pt}} cccc} "
[8] "\\[-1.8ex]\hline "
[9] "\hline \\[-1.8ex] "
[10] " & mpg & cyl & disp \\ "
[11] "\hline \\[-1.8ex] "
[12] "Mazda RX4 & $ & $ & 0$ \\ "
[13] "Mazda RX4 Wag & $ & $ & 0$ \\ "
[14] "Datsun 710 & .800$ & $ & 8$ \\ "
[15] "Hornet 4 Drive & .400$ & $ & 8$ \\ "
[16] "Hornet Sportabout & .700$ & $ & 0$ \\ "
[17] "\hline \\[-1.8ex] "
[18] "\end{tabular} "
[19] "\end{table} "
我需要将第 5 行替换为:
" \caption[short caption]{The main caption of the table.} "
我该怎么做?
尝试:
x <- sub("\caption{The main caption of the table.}",
"\caption[short caption]{The main caption of the table.}", fixed = TRUE, x)
这与您的想法略有不同,但也许您可以只使用具有 caption.width
参数的 xtable
,例如:
print.xtable(xtable(mtcars[1:5, 1:3],
caption="The main caption of the table"),
caption.width="10cm",
caption.placement="top")
输出不会完全移植到你想要的东西,但如果你喜欢更简洁的代码,也许你可以根据你的目的改变它;来自 ?print.xtable
:
The caption will be placed in a "parbox" of the specified width if caption.width is not NULL and type="latex". Default value is NULL.
这是输出:
% latex table generated in R 3.1.3 by xtable 1.7-4 package
% Tue Jun 30 14:52:06 2015
\begin{table}[ht]
\centering
\parbox{5cm}{\caption{The main caption of the table}}
\begin{tabular}{rrrr}
\hline
& mpg & cyl & disp \
\hline
Mazda RX4 & 21.00 & 6.00 & 160.00 \
Mazda RX4 Wag & 21.00 & 6.00 & 160.00 \
Datsun 710 & 22.80 & 4.00 & 108.00 \
Hornet 4 Drive & 21.40 & 6.00 & 258.00 \
Hornet Sportabout & 18.70 & 8.00 & 360.00 \
\hline
\end{tabular}
\end{table}
您还必须弄乱其他选项(例如,digits
)以获得 table 其余部分的确切格式,以匹配 stargazer
中的格式,具体取决于您想要的格式。