Amanda
Amanda

Reputation: 21

how to save the console in R to a file?

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")

Upvotes: 2

Views: 7768

Answers (3)

jay.sf
jay.sf

Reputation: 72803

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.

Upvotes: 4

Darren Tsai
Darren Tsai

Reputation: 35554

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

Upvotes: 1

dash2
dash2

Reputation: 2262

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.

Upvotes: 0

Related Questions