Stat 445: Introduction to Exploratory Data Analysis

This is archived information for Stat 445 Sect 201 (Spring, 2005).

Frequently Asked Questions

Working at MSRC

How do I save/print R output?

It's probably easiest to open an editor first and cut and paste output from the R window into a file (and then maybe copy the file to your home computer using the method below).

R also has a sink(...) function that will cause R's output, but not the commands you enter or any warnings or errors, to be diverted to a file. For example, if you do:

> sink("output.txt")           # divert output to file
> BOD
> l <- lm(demand ~ Time, data=BOD)
> l
> summary(l)
> sink()                       # send output back to screen
>

none of the normal output between sink("output.txt") and sink() will appear on the screen. Instead, R will create an output.txt file containing all the generated output (but only the output, not the commands you typed, unfortunately).

Actually, you might want to use split=T, like so:

> sink("output.txt", split=T)  # output to screen and file
> [ . . . ]
> sink()                       # back to screen only
>

which will cause R to send its output to the file and also show it on the screen.

How do I save/print R graphics?

You can use the postscript(...) or pdf(...) functions to divert all graphics output into a PostScript or PDF file followed by the dev.off() function to close (and finishing writing) the file. By default, there will be one plot per page.

> postscript("myplots.ps")     # divert to file
> plot(x, y)                   # start plot #1
> lines(lowess(x,y))           # add to plot
> lines(x, predict(mymodel))   # add more to plot
> plot(resid(mymodel))         # start plot #2
> abline(0,0)                  # add to plot
> plot(mymodel)                # generates four more plots
> dev.off()                    # finish writing "myplots.ps"
null device 
          1 
> system("gv myplots.ps")      # run "gv" to see all 6 plots
> system("lpr myplots.ps")     # print plots on MSRC printer
>

Once written, the PostScript file may be printed directly using the "lpr" UNIX command, either at the UNIX prompt or using the R "system(...)" function, as shown in the example as shown above. If you generate a PDF file, you can view (and print) it using "acroread" or "xpdf", or copy it to your home computer using the method below.

The jpeg(...) and png(...) functions work similarly but create JPEG and PNG (portable network graphics) files respectively. Microsoft Word can import both these image types, though a PNG file will generally produce better results than a JPEG file.

How do I copy a file from MSRC to my home computer?

The easiest way is probably just to e-mail the file to yourself. On the MSRC UNIX machines, you can use the "mpack" command to e-mail a file as an attachment:

joeuser@unixlab% mpack plots.pdf youremail@home.address
Subject: plots for assignment #1
joeuser@unixlab%

(Note that you run "mpack" from the UNIX prompt, not the R prompt.)

Working at Home

How do I install R at home?

If you have a Windows (95 or later) machine at home, you can install R version 2.0.1 by downloading the executable file rw2001.exe (23 megabytes) and running it to install the base system. For other systems, or for more information, visit one of the CRAN links on the Resources page.

How do I import R graphics into a Microsoft Word document?

In R for Windows, you can right-click an "R Graphics" window to get a context menu that includes the options "Copy as metafile" and "Copy as bitmap". Either of these options will copy the currently displayed plot to the clipboard, and you can then do an "Edit->Paste" in the Microsoft Word document to paste the graphic in at the current cursor position. The object can be resized as required.

Generally speaking, the "Copy as metafile" option will produce better results than the "Copy as bitmap" option, particularly if you resize the object after pasting it into your Word document. However, this isn't always true, so you may have to experiment with both methods.

If you're working at MSRC and would like to save an R plot to import into Microsoft Word at home, see above for how to save a plot as a PNG file and e-mail it to your home computer. Then, in Microsoft Word, use "Insert->Picture->From File..." to import the PNG file into your document. (Saving the plot as a JPEG will also work but will generally result in lower quality output.)

This is archived information for Stat 445 Sect 201 (Spring, 2005).