14

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.

FooBar
  • 15,724
  • 19
  • 82
  • 171
  • What is the value of `myFile`? If it's not something like "path/to/file.ext" then you are seeing the results of an incomplete request. – IRTFM May 12 '15 at 17:47
  • @BondedDust the value is `[1] "../log/regressions_ldiffStatus.tex"`, and that path exists. – FooBar May 12 '15 at 17:49

4 Answers4

10

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.

Community
  • 1
  • 1
eipi10
  • 91,525
  • 24
  • 209
  • 285
  • Thanks! The use of `cat()` to handle the output of `stargazer()` is useful here and in fact sometimes even necessary to get proper formatting of tables, regardless of whether you want to suppress all the printing to screen. See [here](https://stackoverflow.com/questions/44318860/r-stargazer-manually-specify-r2-and-write-to-tex). – Michael Ohlrogge Dec 25 '18 at 19:12
7

well given the answer of eipi10, the only part you need is

bla <- capture.output(stargazer(..., out=output.file))

specifying the output file in stargazer and capture the output in something random, which you simply remove or overwrite for the next table. No need to define a new function.

wesselnv
  • 79
  • 1
  • 1
0

the easiest way to approach the problem is:

  1. store the output of stargazer in an object without an output file nor capture.output()

output <- stargazer(..., type="text")

this stores the output as an nx1 matrix which doesn't look nice so you must convert it in the second step

  1. convert the output object into a nice format

2.a) with dplyr:

output %>% paste(., collapse = "\n") %>% cat("\n")

2.b) without dplyr:

cat(paste(output, collapse = "\n"), "\n")

2.c) as a function if you really prefer this:

print_stargazer <- function(object) {
   cat(paste(object, collapse = "\n"), "\n")
}

and then use it like this:

output <- stargazer(..., type="text")
print_stargazer(object)
Agile Bean
  • 6,437
  • 1
  • 45
  • 53
0

First of all, set working directory. Then if you run the following code, it will save a text file with stargazer results to your working directory folder.

model_results <- stargazer(model1, type = "text") capture.output(model_results, file = "model1.txt")

  • You don't need to set a working directory for this. As the other answers note, you can simply provide a path to whatever output file that you want, and then supply it to the `file` argument. – slamballais May 23 '21 at 07:06