R Programming/Utilities

This page includes material about some utilities. Most of the functions presented here have nothing to do with statistical analysis but may be useful when working on a project. Many functions are just similar to standard unix functions.

System (Unix/DOS)
system gives access to the system (DOS or unix). The option wait=FALSE means that you don't ask R to wait that the task is finished.

Some examples :
 * You can convert an image from to PS to PNG using the unix convert function of your computer. If you want to know more about this function, open a Terminal application and type man convert (This should work on Mac OS and Linux).
 * You can open Stata and run a program.
 * You can run pdflatex from R and directly open the pdf in a pdf browser.

See also sys in the Hmisc package, shell and shell.exec.

File Handling
dir lists all the files in a directory. It is similar to the Unix function <tt>ls</tt>. creates a new directory. It is similar to <tt>mkdir</tt> in Unix.

<tt>file.info</tt> gives information about a file. Removing files with a specific pattern :


 * opens a file in the text editor.
 * <tt>file.show</tt> opens a file in a new window.
 * <tt>tempfile</tt> creates a temporary file.


 * <tt>getZip</tt> in the Hmisc package.

Internet
<tt>browseURL</tt> opens an URL using an internet browser. <tt>download.file</tt> download a file from the internet.

To see the default browser, use <tt>getOption</tt>

We can change the default browser using the <tt>options</tt> command. It is safer to store the options before.

You can download a file from the internet using. Note that very often you don't need to download a file from the internet and you can directly load it into R from the internet using standard functions. For instance, if you want to read a text file from the internet, you can use,   or.

See also RCurl

Computing time
If you perform computer intensive task you may want to optimize the computing time. Two functions are available system.time and proc.time. Both returns a vector of values. The first is the standard CPU time.

Computing process
<tt>user.prompt</tt> (Zelig) makes a pause in the computation process (useful if you want to do a demo). <tt> waitReturn</tt> (cwhmisc) does the same job. <tt>Sys.sleep</tt> stop the computation during a few seconds.

It is possible to stop the computing process if a logical condition is not true using <tt>stopifnot</tt>.

Miscellanous

 * <tt>trCopy</tt> (TinnR package) copy an object to the clipboard. It is useful if you want to copy a large object to the clipboard. For instance, if you want to copy the code of a function and paste it in a text editor.


 * <tt>sessionInfo</tt> gives information on the current session info (R version + loaded packages). This function may be useful for reproducible computing. <tt>getRversion</tt> gives the current R version. <tt>R.version</tt> gives more details about the computer and <tt>R.Version</tt> returns the same informations as a list.