Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture R output and replace with LaTeX code

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?

like image 706
luciano Avatar asked Jun 27 '15 10:06

luciano


1 Answers

Try:

x <- sub("\\caption{The main caption of the table.}", 
         "\\caption[short caption]{The main caption of the table.}", fixed = TRUE, x)
like image 149
Steven Beaupré Avatar answered Sep 30 '22 06:09

Steven Beaupré