Capture R output and replace with LaTeX code

只愿长相守 提交于 2020-01-03 11:02:22

问题


I'm trying to capture the output from some R code and replace it with latex code.

If you run this code:

library(stargazer)
x <- capture.output(stargazer(mtcars[1:5, 1:3], summary = FALSE, title="The main caption of the table."))

x

This is the output:

 [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 & $21$ & $6$ & $160$ \\\\ "                                                                     
[13] "Mazda RX4 Wag & $21$ & $6$ & $160$ \\\\ "                                                                 
[14] "Datsun 710 & $22.800$ & $4$ & $108$ \\\\ "                                                                
[15] "Hornet 4 Drive & $21.400$ & $6$ & $258$ \\\\ "                                                            
[16] "Hornet Sportabout & $18.700$ & $8$ & $360$ \\\\ "                                                         
[17] "\\hline \\\\[-1.8ex] "                                                                                    
[18] "\\end{tabular} "                                                                                          
[19] "\\end{table} " 

I need to replace line 5 with this:

"  \\caption[short caption]{The main caption of the table.} "

How can I do this?


回答1:


Try:

x <- sub("\\caption{The main caption of the table.}", 
         "\\caption[short caption]{The main caption of the table.}", fixed = TRUE, x)



回答2:


This is a little different than what you had in mind, but perhaps you could just use xtable which has a caption.width argument, e.g.:

print.xtable(xtable(mtcars[1:5, 1:3],
                    caption="The main caption of the table"),
             caption.width="10cm",
             caption.placement="top")

The output will not port exactly to what you're going for, but maybe you could bend this to your purposes if you prefer the more concise code; from ?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.

Here's the output:

% 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}

You'll also have to mess around with the other options (e.g., digits) to get the exact formatting of the rest of the table to match that from stargazer, depending on what exactly the formatting is that you had in mind.



来源:https://stackoverflow.com/questions/31087731/capture-r-output-and-replace-with-latex-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!