1

It's me again, quite the beginner at R but somehow fumbling my way through it for my thesis. I've run a bunch of regressions and made them into tables using Stargazer. Now I need to share all these results (the glm models/their summaries/the coefficients and confidence intervals and the stargazer tables ... basically everything in my console) with a friend of mine to discuss, but I figure there's got to be a more efficient way to do this than 1) screenshot-ing the hell out of my console or 2) copy and pasting the console and thus botching the formatting. Does anyone have any advice for this?

Some of my code (the rest is just variations on the same stuff) is below in case that's helpful!

Mod4 <- glm(`HC Annual Total` ~ `state population`
           + Year + `Trump Presidency`, data = thesis.data, family = poisson())
summary(Mod4)

#pulling the coefs out, then add exp for what reason I don't remember
exp(coef(Mod4))

#finding the confidence intervals
exp(confint(Mod4))

#Using stargazer to turn Mod4 into a cleaner table
library(stargazer)

stargazer(Mod4, type="text", dep.var.labels = c("Hate Crimes"),
          covariate.labels = c("State Population", "Year", "Trump Presidency"),
          out = "models.txt")
Amanda
  • 11
  • 1
  • 3

3 Answers3

4

When you need it fast and without art, you could send console output to a simple text file using sink.

sink(file="./my_code.txt")  ## open sink connection

timestamp()
(s <- summary(fit <- lm(mpg ~ hp, mtcars)))
cat('\n##', strrep('~', 77), '\n')
texreg::screenreg(fit, override.se=s$coe[,3], override.pvalues=s$coe[,4])
cat('\n# Note: 
    We could report t-values 
    instead of SEs\n')
cat('\n##', strrep('~', 77), '\n')
cat('\nCheers!\nJ')

sink()  ## close it!


file.show("./my_code.txt")  ## look at it

Note, that you can easily create a mess with unclosed sinks and no output is shown on the console. Try closeAllConnections() in this case or perhaps milder solutions. Also consider rmarkdown as suggested in comments.

jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • 1
    After the sink command, you won't see the output from any other commands on the command line, so this is a bit risky unless the user knows exactly what to do. – dash2 Mar 11 '20 at 12:40
  • @dash2 That's not entirely true, since after the second empty `sink()` normal behavior will be reestablished. But regardless you made a point. – jay.sf Mar 11 '20 at 12:43
1

I think reprex is an intermediate solution between rmarkdown and sink. At first, make sure your R script can be executed without any errors. Then use the following code:

library(reprex)
r.file <- "path/to/Rscript/test.R" ## path to your R script file
reprex(input = r.file, outfile = NA)

There will be four files created in the directory of your R script file, i.e.

  1. test_reprex.R
  2. test_reprex.html
  3. test_reprex.md
  4. test_reprex.utf8.md

The html and md files contain codes in your original R script and their output. You can share with someone and they can see the output without running any codes.

The html file looks like:

enter image description here

Darren Tsai
  • 32,117
  • 5
  • 21
  • 51
  • 1
    The `outfile` argument of `reprex()` is deprecated as of reprex 2.0.0. ℹ Use `reprex(wd = ".")` instead of `reprex(outfile = NA)`. – UseR10085 May 16 '23 at 04:26
0

savehistory() is your friend:

savehistory(file = "my-code.txt")

You can then edit the code at will.

Or, in RStudio, you can use the history pane and copy and paste relevant bits to a text file.

If a full rmarkdown document is overkill, you could try using knitr::spin() to compile your code.

Lastly:

  • In future, always write scripts.

Reproducibility is best thought of at the start of a project, not as an add-on at the end. It's much easier to run a carefully-written script at the console, than it is to turn your meandering console input into a useful script. A good workflow is to try a few things at the console, then once you know what you are doing, add a line to your script.

dash2
  • 2,024
  • 6
  • 15
  • 1
    `savehistory` just save code, not output. The question looks like the OP want to share his output in console. – Darren Tsai Mar 11 '20 at 13:32