Stargazer: Save to file, don't show in console

FooBar picture FooBar · May 12, 2015 · Viewed 9.1k times · Source

When I want to save my regression results with

stargazer(regressions[[reg]], out=myFile, out.header=FALSE

stargazer keeps also displaying/printing the result into the console. As I'm iterating over dozens of results, this ruins my overview and the log. Is there any way to explicitly tell stargazer not only to save the output to the file, but also not to print it additionally?

I'm on stargazer_5.1.

Answer

eipi10 picture eipi10 · May 12, 2015

You can write a function that captures the output of stargazer and saves it to a file without any output to the console. For example, adapting code from this SO answer:

mod_stargazer <- function(output.file, ...) {
  output <- capture.output(stargazer(...))
  cat(paste(output, collapse = "\n"), "\n", file=output.file, append=TRUE)
}

Then, to run the function:

mod_stargazer(myfile, regressions[[reg]], header=FALSE)

append=TRUE results in all your tables being saved to the same file. Remove it if you want separate files for each table.