Analyzing Statistics with GNU R
Pages: 1, 2, 3, 4, 5
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]:
Enter 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 png(), jpeg(), or 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
The --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.
Conclusions
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).
Related Links
- 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.
Kevin Farnham is the owner of Lyra Technical Systems, Inc, a small consulting and publishing company.
Return to ONLamp.com.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 2 of 2.
-
dev.off()
2005-11-26 09:19:40 Kevin Farnham |
[Reply | View]
-
Updating R with Sarge
2005-11-22 03:31:30 charshaw [Reply | View]
If a user wants to maintain their R package up to date with backports for their their Sarge installation, simply add the following line to their /etc/apt/sources.list file:
## CRAN repo for backports of current R to stable
deb http://cran.R-project.org/bin/linux/debian sarge/
Then apt-get update and apt-get upgrade as usual.
Hope this helps,
Clint







A member of the R mailing list suggests using the dev.off() statement when writing R graphics to a file (see the "Saving Your R Work" section). The dev.off() statement closes R graphics devices.
While the q() statement does close the output graphics file (because R by default flushes buffers and closes open files as it shuts down), the proper way to close the file is to apply the dev.off() statement.
For example, the script that writes a PNG file of the S&P500 graph should be modified to be:
dv <- read.table("./SP500.txt", header=FALSE)
png("SP500.png")
plot(dv[,2], type="l")
dev.off()
q()
</code>