Displaying/Saving Results
Use the cat(...) command to output variables to a file.
There are several ways to display the values of variables in the command window.
print(variable)
command. Formatting of the output from cat, print or when typing the name of a variable on the command line is set with
the options(...).
One can also format the
output before using cat by using the formatC command.
External file manipulation routines are file.create(...)
, file.remove(...)
etc. See file for details. Within the windows GUI one can view files
by selecting the File/Display file item from the menu bar.
Original documentation: cat, options,
file,
formatC
Samples
# printing the internal value of pi
print(pi) [1] 3.141593
# resetting the number of output digits to 10,
# and then printing pi
options(digits = 10); print(pi) [1] 3.141592654
# using the cat command. I've included a "\n" so that
# there is a carriage return after the output of pi.
cat(pi,"\n"); 3.141592654
# generate some x,y data and then save it to the file results.txt
x <- seq(0.0,1.0,.2); x [1] 0.0 0.2 0.4 0.6 0.8 1.0 y <- sin(x); y [1] 0.0000000 0.1986693 0.3894183 0.5646425 0.7173561 0.8414710 cat(x,"\n",file="results.txt"); cat(y,"\n",file="results.txt",append=TRUE);
# An example of using the formatC command to print values using different formats. Note that
# to print out the three numbers x,y, and e, in a row, one can use the c(...) (concatenate) operator.
#
iter <- 5; x <- 3.0; y <- 2.0; e <- 5.0; cat(formatC(iter,digits=2),' ',formatC(c(x,y,e),format='f',width=19,digits=16),'\n')
5 3.0000000000000000 2.0000000000000000 5.0000000000000000
UCLA Mathematics Department | ©2000 |