Saving Your R Work
R provides several options for saving your work. First, when you execute the
q() function to quit your session, R asks:
Save workspace image? [y/n/c]:
y to save your current R session state (so that next time you run R in that same directory, your starting status will be identical to that of the current session). Enter
n to exit without saving the current session state, or
c to continue the current session.
R provides a capability to plot directly to PNG, JPEG, and PDF files through its display setting options. To plot to a file instead of to the R graphics window, execute the
pdf() function prior to any R plotting function. For example:
> png("myImage.png") > persp(z,theta=0,phi=60,box=FALSE,col="yellow")
You can also invoke R by using scripts. This permits the integration of R into automated data analysis processes. Here's an R script that reads the S&P 500 data file and produces a PNG image of the data:
dv <- read.table("./SP500.txt", header=FALSE) png("SP500.png") plot(dv[,2], type="l") q()
The command line to execute the script (named gspc.scr) is:
R --no-save <gspc.scr > gspc.log
--no-save flag tells R not to save the script's session. The text output produced by the script is sent to file gspc.log.
R is a powerful, mature, and very well documented GNU open source project. Though the language syntax is a bit unusual, knowing just a few commands lets you quickly generate quite impressive data products.
R's base documentation consists of six manuals, the PDF versions of which comprise 1,700 total pages. The documents are included with the Windows R executable installation download, but they must be downloaded separately for Linux/Unix and MacOS platforms. "An Introduction to R" is the best choice for those who are just starting out. More specialized and detailed references are also available. The R FAQ answers basic introductory questions about R and discusses licensing and usage policy.
To download R, visit The R Project for Statistical Computing (R-Project.org) site. Click on CRAN to access the Comprehensive R Archive Network of mirrored servers. Documentation is available using the Manuals link. Section 5.6 in the R FAQ provides information about contributing to the R project (reporting bugs, coding new extensions, porting additional features from S, and so on).
- The R Project for Statistical Computing (R-Project.org): the R-Project home.
- The Comprehensive R Archive Network (CRAN): central distribution site for the R base system and contributed packages.
- R FAQ: The R Project's frequently asked questions document.
- R Article Reference Materials: author's page that provides sample raw data, the Perl and R code used in developing the article, and related materials.
- Yahoo! Finance: source of the S&P 500 data used in this article.
- Office of Federal Housing Enterprise Oversight (OFHEO): source of the U.S. housing data used in this article.
Return to ONLamp.com.