JeanVuda JeanVuda - 3 months ago 12
R Question

How to invoke Stata and run syntax via R?

I have an odd situation, and please pardon me for not providing a reproducible example for this question. I have more than 1000 lines of syntax written for Stata to carry out multiple analyses (I wrote it before I started using R). This syntax is used to perform analysis in a quarterly data every 3 months to create a report. Results of the analyses are saved in csv files, and read via R, and put into a word document using ReporterS package. What I like to know is if there is any way to invoke Stata via R, and specify/pipe the syntax to run it? (I understand the viceversa situation can be done using rsource (user written function) in Stata). I can still manually fire up Stata and run the syntax there. But I would like to know whether it is possible to do it via R? So, a shiny app/web interface can be created to do this part, and client doesn't need to do it manually?

Answer

As @thelatemail suggests, the easiest thing to do here is simply run Stata in batch mode from a system call.

Here's an example do file (called "example.do"):

log using out.log, replace
sysuse auto
regress mpg weight foreign

And here's the R code to run it and retrieve the output (assuming Stata is on your path and you replace Stata-64 with the appropriate binary file on your machine):

> system("Stata-64 /e do example.do"); readLines("out.log")
 [1] "-----------------------------------------------------------------------------------------------------------------------"
 [2] "      name:  <unnamed>"                                                                                                 
 [3] "       log:  FilePathHere"                                                                     
 [4] "  log type:  text"                                                                                                      
 [5] " opened on:   9 Jan 2015, 13:34:18"                                                                                     
 [6] ""                                                                                                                       
 [7] ". sysuse auto"                                                                                                          
 [8] "(1978 Automobile Data)"                                                                                                 
 [9] ""                                                                                                                       
[10] ". regress mpg weight foreign"                                                                                           
[11] ""                                                                                                                       
[12] "      Source |       SS       df       MS              Number of obs =      74"                                         
[13] "-------------+------------------------------           F(  2,    71) =   69.75"                                         
[14] "       Model |   1619.2877     2  809.643849           Prob > F      =  0.0000"                                         
[15] "    Residual |  824.171761    71   11.608053           R-squared     =  0.6627"                                         
[16] "-------------+------------------------------           Adj R-squared =  0.6532"                                         
[17] "       Total |  2443.45946    73  33.4720474           Root MSE      =  3.4071"                                         
[18] ""                                                                                                                       
[19] "------------------------------------------------------------------------------"                                         
[20] "         mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]"                                         
[21] "-------------+----------------------------------------------------------------"                                         
[22] "      weight |  -.0065879   .0006371   -10.34   0.000    -.0078583   -.0053175"                                         
[23] "     foreign |  -1.650029   1.075994    -1.53   0.130      -3.7955    .4954422"                                         
[24] "       _cons |    41.6797   2.165547    19.25   0.000     37.36172    45.99768"                                         
[25] "------------------------------------------------------------------------------"                                         
[26] ""                                                                                                                       
[27] ". "                                                                                                                     
[28] "end of do-file"                                                                                                         
[29] ""                                                                                                                       
[30] ". exit, clear"                                                                               

It may be easier to parse the output if you log using Stata Markup Control Language (SMCL), by replacing the first line of the do file with log using out.log, replace smcl. Then the output will be:

 [1] "{smcl}"                                                                                                                                                                                 
 [2] "{com}{sf}{ul off}{txt}{.-}"                                                                                                                                                             
 [3] "      name:  {res}<unnamed>"                                                                                                                                                            
 [4] "       {txt}log:  {res}FilePathHere"                                                                                                                        
 [5] "  {txt}log type:  {res}smcl"                                                                                                                                                            
 [6] " {txt}opened on:  {res} 9 Jan 2015, 13:41:53"                                                                                                                                           
 [7] "{txt}"                                                                                                                                                                                  
 [8] "{com}. sysuse auto"                                                                                                                                                                     
 [9] "{txt}(1978 Automobile Data)"                                                                                                                                                            
[10] ""                                                                                                                                                                                       
[11] "{com}. regress mpg weight foreign"                                                                                                                                                      
[12] ""                                                                                                                                                                                       
[13] "      {txt}Source {c |}       SS       df       MS              Number of obs ={res}      74"                                                                                           
[14] "{txt}{hline 13}{char +}{hline 30}           F(  2,    71) ={res}   69.75"                                                                                                               
[15] "    {txt}   Model {char |} {res}  1619.2877     2  809.643849           {txt}Prob > F      = {res} 0.0000"                                                                              
[16] "    {txt}Residual {char |} {res} 824.171761    71   11.608053           {txt}R-squared     = {res} 0.6627"                                                                              
[17] "{txt}{hline 13}{char +}{hline 30}           Adj R-squared = {res} 0.6532"                                                                                                               
[18] "    {txt}   Total {char |} {res} 2443.45946    73  33.4720474           {txt}Root MSE      = {res} 3.4071"                                                                              
[19] ""                                                                                                                                                                                       
[20] "{txt}{hline 13}{c TT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12}"                                                                                                        
[21] "{col 1}         mpg{col 14}{c |}      Coef.{col 26}   Std. Err.{col 38}      t{col 46}   P>|t|{col 54}     [95% Con{col 67}f. Interval]"                                                
[22] "{hline 13}{c +}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12}"                                                                                                              
[23] "{space 6}weight {c |}{col 14}{res}{space 2}-.0065879{col 26}{space 2} .0006371{col 37}{space 1}  -10.34{col 46}{space 3}0.000{col 54}{space 4}-.0078583{col 67}{space 3}-.0053175"      
[24] "{txt}{space 5}foreign {c |}{col 14}{res}{space 2}-1.650029{col 26}{space 2} 1.075994{col 37}{space 1}   -1.53{col 46}{space 3}0.130{col 54}{space 4}  -3.7955{col 67}{space 3} .4954422"
[25] "{txt}{space 7}_cons {c |}{col 14}{res}{space 2}  41.6797{col 26}{space 2} 2.165547{col 37}{space 1}   19.25{col 46}{space 3}0.000{col 54}{space 4} 37.36172{col 67}{space 3} 45.99768"  
[26] "{txt}{hline 13}{c BT}{hline 11}{hline 11}{hline 9}{hline 8}{hline 13}{hline 12}"                                                                                                        
[27] "{res}{txt}"                                                                                                                                                                             
[28] "{com}. "                                                                                                                                                                                
[29] "{txt}end of do-file"