Q: How to add straight lines through the current plot in R?

You can use the command abline in R. For example, you want add to the current plot a horizontal line at y=1, a vertial line at x=3, and a line with intercept 2 and slope 0.5. You can use the following command:

abline(h=1) abline(v=3) abline(a=2, b=0.5)
Q: How do I install a package in R/Splus in UNIX?

Below are some instructions for installing an R package into your own disk space (so that you don't need to ask the busy systems manager).

To install a package (in Unix) which is not part of the main distribution of R, follow the steps below.

  • go to http://www.r-project.org or a local mirror site http://cran.stat.sfu.ca and get the source as a gzipped tar file, an example is the package sspir_0.2.3.tar.gz for State Space Models in R
  • extract from the tar file with (say under your ~/tmp directory) mkdir ~/tmp # if directory doesn't already exist mv ~/sspir_0.2.3.tar.gz ~/tmp cd ~/tmp tar xzvf sspir_0.2.3.tar.gz
  • compile the source (typically C and/or fortran routines) and install into your local directory (say ~/Rlib) with the following Unix command line (where you replace $HOME with your home directory which is the output of 'echo $HOME'). R CMD INSTALL --library=$HOME/Rlib sspir To use the package, you need the lib.loc option for library() > library(sspir,lib.loc="$HOME/Rlib") > library(help=sspir,lib.loc="$HOME/Rlib")
  • With newer versions of R, the above can be combined into one step R CMD INSTALL --library=$HOME/Rlib sspir_0.2.3.tar.gz
  • If you decide later you don't want the package, then from the Unix command line R CMD REMOVE --library=$HOME/Rlib sspir
  • If you want to let others use your locally added packages, just set permissions appropriately to $HOME/Rlib, e.g. chmod -R og+rX $HOME/Rlib
  • Note that the different flavors of Unix on our network. A package compiled in one computer (e.g. 32-bit Linux) should work on another computer with the same architecture. If you work on different servers on our network, then you would have to compile separate versions. In this case, one possibility is something like subdirectory Rlib_linux32, Rlib_linux64, Rlib_solaris64 under your $HOME
  • Alternatively, after testing the package, you can ask the systems manager to install it (be clear whether you want the Solaris or Linux version) by providing the following instructions (where you replace $MYRPKGTARDIR with the directory where the tar file is unpacked, and replace $PKG with the name of the package) cd $MYRPKGTARDIR R CMD INSTALL $PKG

As a final note, if you want to install a package in Windows, the simplest thing to do is:

  • go to http://www.r-project.org/ and get the zip file with the Windows compiled version
  • unzip the file under $RHOME/library, where $RHOME is the folder where you installed R
  • if you don't have write access to $RHOME, just unzip the package anywhere and use library() with lib.loc argument to load the package later
  • Alternatively, use the installer in the R console menu.
Q: R Miscellaneous Add-Ons
Q: How to use R to read data stored by Minitab, S, SAS, SPSS, Stata, ...?

the foreign package provide several functions to read data stored by Minitab, S, SAS, SPSS, Stata,....

data.restore Read an S3 Binary File lookup.xport Lookup information on a SAS XPORT format library read.dta Read Stata binary files read.epiinfo Read Epi Info data files read.mtp Read a Minitab Portable Worksheet read.S Read an S3 Binary File read.spss Read an SPSS data file read.ssd obtain a data frame from a SAS permanent dataset, via read.xport read.xport Read a SAS XPORT format library SModeNames Read an S3 Binary File write.dta Write files in Stata binary format
Q: How to read data files containing time data by R?

For example, the data file test.dat is:

4188418000628; 1 ; 05-19-2002 ; 06-23-2002 ; 26.6; 3.71; 3.03; 0 4188418000628; 1 ; 05-19-2002 ; 07-15-2002 ; 28.1; 3.41; 2.79; 0 4188418000628; 1 ; 05-19-2002 ; 08-15-2002 ; 32.0; 3.43; 3.30; 0

The field is separated by ";" and the third and fourth columns are time data.

  1. Read time data as characters. y<-read.table("test.dat", sep=";", as.is=T, strip.white=T) and we can get > y$V3 [1] "05-19-2002" "05-19-2002" "05-19-2002" The argument strip.white is used only when sep has been specified, and allows the stripping of leading and trailing white space from `character' fields (`numeric' fields are always stripped). If strip.white=F, then we will get > y1<-read.table("test.dat", sep=";", as.is=T) > y1$V3 [1] " 05-19-2002 " " 05-19-2002 " " 05-19-2002 "

    The default behavior of read.table is to convert character variables (which are not converted to logical, numeric or complex) to factors. When as.is=T, read.table will not convert character variables to factors.

  2. Convert characters to a Julian date by using the function as.date in library(survival) library(survival) > a<-as.date(y$V3) > a [1] 19May2002 19May2002 19May2002 > b [1] 23Jun2002 15Jul2002 15Aug2002 > a[1]-b[1] [1] -35
Q: How to format output of R?

You can use R command format. For example

round(0.10000, 3)

produces

0.1

instead of

0.100

. To get correct format, we can use the following command

format(round(0.10000,3), nsmall=3, digits=3)

Note that the return value of the above command is a character string "0.100", not a numerical value.

Q: How do I get Firefox to run an external application on a particular file type?

A helper application is a program external to the browser that will open its own window or terminal to display results. A helper application is not the same ias a plugin. A plugin is loaded by the browser process data internally and render the results within the browser window (e.g. Acrobaty Reader plugin for PDF files).

This FAQ does not cover the installation of plugins; you have to go elsewhere for that.

If you want Firefox to autmatically use an external helper program to process files of a particular type (characterized by a filename extension or MIME type) whenever you click on a link to that file, these are the steps you can perform.

Manual Method

  • Step 1 (optional): backup your existing Firefox MIME database in case you screw it up. You can also delete the file and let Firefox rebuild it if things go wrong. cd ~/.mozilla/firefox/user-profile cp mimeTypes.rdf mimeTypes.rdf~ # Now you can edit this file ... vi mimeTypes.rdf
  • Step 2: add the MIME type to the list of helpers Firefox should handle. If the <RDF:Seq ... </RDF:Seq> tag already exists, only add the middle line. <RDF:Seq RDF:about="urn:mimetypes:root"> ... <RDF:li RDF:resource="urn:mimetype:application/testapp"/> </RDF:Seq>
  • Step 3: associate file extensions with this MIME type: <RDF:Description RDF:about="urn:mimetype:application/testapp" NC:editable="true" NC:value="application/testapp" NC:description="Description of MIME type"> <NC:fileExtensions>ext1</NC:fileExtensions> <NC:fileExtensions>ext2</NC:fileExtensions> ... <NC:handlerProp RDF:resource= "urn:mimetype:handler:application/testapp"/> </RDF:Description>
  • Step 4: inform Firefox this MIME type is automatically handled by an external application. <RDF:Description RDF:about="urn:mimetype:handler:application/testapp" NC:alwaysAsk="false" NC:useSystemDefault="false" NC:saveToDisk="false"> <NC:externalApplication RDF:resource= "urn:mimetype:externalApplication:application/testapp"/> </RDF:Description>
  • Step 5: inform Firefox which external application to use: <RDF:Description RDF:about="urn:mimetype:externalApplication:application/testapp" NC:path="executable" />

For steps 2 and onward, snippets should be placed before the final line

</RDF:RDF>

You should replace the example values above with your particular values. For example, to view postscript files using ghostview (gv), you would replace

  • application/testapp with application/postscript.
  • ext1 with ps. Optionally, you could also replace ext1 with eps for excapsulated postscript, or you can leave that out.
  • executable with gv or /usr/local/bin/gv.
  • Description of MIME type with PostScript file.

Plugin Method

A much easier way, if you have the ability to install Firefox plugins, may be to install the MimeEdit plugin, which gives the user the ability to associate MIME types with application that can operate on them using a GUI. It can be found here

https://addons.mozilla.org/en-US/firefox/addon/4498
Q: How can I access the AMS MathSciNet publication database from home/off-campus?

From within the campus, the AMS MathSciNet publication database can be accessed directly at

http://www.ams.org/mathscinet/

Due to the fact that this database is a paid service via a subscription fee, direct access is only available from campus networks. However you can access MathSciNet from off-campus (e.g. ADSL connection from home) by proxying your request through the UBC Library system. You must have a valid library card to validate your request. The instructions on how to accomplish this are here:

http://www.library.ubc.ca/home/proxyinfo/proxyhelp.html
Q: How do I handle complaints from Firefox/Netscape that another browser is running?

A browser will usually check to see if another instance of itself is running, and will refuse to start (or use your regular profile) if it believes that this is the case. It does this to ensure consistency of its database (bookmarks, browsing history, etc.) which it can't guarantee if another browser is modifying the same data it is reading from or writing to.

A browser will check that it is not stepping on its own toes by checking the existence of a lock file. A lock file is created whenever a browser starts to run to warn other potential instances of itself not to start running.

However, problems may arise if the original instance of the browser crashes or ends abnormally, and does not get the chance to remove the lock file. The lock file is located at

Firefox: ~/.mozilla/firefox/<profile-directory>/lock Netscape: ~/.netscape/lock

These file are symbolic links (not regular files) that will point to the host and process-id that created the lock file. You can glean this information by doing

ls -l <lockfile> Example output: lrwxrwxrwx 1 smith wesson 18 Jan 12 15:54 lock -> 12.34.56.78:+21321

If you cannot run a browser because of this exclusivity constraint, first make sure that there really isn't another instance running on another terminal or hiding on your desktop. You can also check to see that it really doesn't exist by logging on to the host the lock file claims the browser it is running on and doing

ssh 12.34.56.78 -l smith ps -p 21321

If this process exists, you can either find where it is displaying to and stop it, or you can kill it by doing

kill 21321

If the process does nto exist, or the lock file is still there, you can now remove it:

rm <lockfile>

Try starting your browser now. If you still can't start it up, Email the IT staff.

Pages