pdfconsoleoutputexportrstudio

How to export RStudio console output directly to a pdf file


I'm running GLMs in RStudio and am trying to get the output of my Tukey's post-hoc test directly into a PFD file, and not into the console. pairs(emmeans(M50, ~AgeClass + Month, component="zi"),type="response", bias.adjust=F, adj="Tukey", infer=(TRUE)) When it appears in the console, I cannot read all the lines. I tried fixing that with this function rstudioapi::writeRStudioPreference("console_max_lines", 100000000) and changing the console properties etc., but none of that is working. I've tried exporting it into a PDF using pdf("models.pdf") but I keep getting an empty file.

What should I do?


Solution

  • First, pdf() sets up a graphics output device. So if you do

    pdf("plots.pdf")
    plot(breaks ~ tension, data = warpbreaks)
    dev.off()
    

    the plot will be saved in the PDF file. To divert, output, do a similar thing with the sink() function:

    sink(file = "output.txt")
    pairs(emmeans(...))
    sink()
    

    This will make a text file, obviously.

    But I would encourage you to consider using R Markdown, by which you can put your code in a certain format (very similar to what you need to do to post on this forum), then format it, encapsulating buth the code and the output in one place. There are a number of R Markdown tutorials available -- just search for it.