Search This Blog

Wednesday, January 04, 2012

Quick R intro stattistics

Input/Output

By default, launching R starts an interactive session with input from the keyboard and output to the screen. However, you can have input come from a script file (a file containing R commands) and direct output to a variety of destinations.

Input

The source( ) function runs a script in the current session. If the filename does not include a path, the file is taken from the current working directory.
# input a script
source("myfile")

Output

The sink( ) function defines the direction of the output.
# direct output to a file
sink("myfile", append=FALSE, split=FALSE)

# return output to the terminal
sink()

The append option controls whether output overwrites or adds to a file. The split option determines if output is also sent to the screen as well as the output file.
Here are some examples of the sink() function.
# output directed to output.txt in c:\projects directory.
# output overwrites existing file. no output to terminal.
sink("c:/projects/output.txt")

# output directed to myfile.txt in cwd. output is appended
# to existing file. output also send to terminal.
sink("myfile.txt", append=TRUE, split=TRUE)

When redirecting output, use the cat( ) function to annotate the output.

Graphs

sink( ) will not redirect graphic output. To redirect graphic output use one of the following functions. Use dev.off( ) to return output to the terminal.
Function Output to
pdf("mygraph.pdf") pdf file
win.metafile("mygraph.wmf") windows metafile
png("mygraph.png") png file
jpeg("mygraph.jpg") jpeg file
bmp("mygraph.bmp") bmp file
postscript("mygraph.ps") postscript file
Use a full path in the file name to save the graph outside of the current working directory.
# example - output graph to jpeg file
jpeg("c:/mygraphs/myplot.jpg")
plot(x)
dev.off()


No comments:

Post a Comment

Thank you