Default R

Source a bunch of files under given folders and/or subfolders

I wrote a function sourceDir() that can source all the functions under given folders and/or subfolders. This function is very much like “Set Path” in Matlab.Please visit this page to obtain the update to dated version.

To use it more efficiently, I suggest you put the function at some place, e.g. ~/workspace/R_utils/sourceDir.R, and write the following lines in .Rprofile file.

.my.env <- new.env()

Next time the function will be automatically loaded under a personal environment .my.env when R is launched and you can use it directly. The advantage of this is that rm(list=ls()) command will not remove the function sourceDir. But if you really want to remove sourceDir function, you can use rm(list=ls(all=TRUE)) command instead.

Default R

How to prevent dropping dimensions in a matrix/array?

When you create a matrix in the usual way like this,

> a <- matrix(rnorm(10),2,5)
> a
       [,1]      [,2]       [,3]       [,4]       [,5]
 [1,]  1.3488918 0.6225795 -0.7444514  1.3130491  1.7877849
 [2,] -0.2385392 0.5656759  0.9037435 -0.2217444 -0.2656875

the dimension dropped after picking up a single row or column in this way,

> b <- a[,1]
> b
 [1]  1.3488918 -0.2385392
 > dim(b)

The solution is to try it with a parameter drop = FALSE,

> b <- a[,1,drop = FALSE]
 > b
 [1,]  1.3488918
 [2,] -0.2385392
 > dim(b)
 [1] 2 1
Default R

How to crop an eps file?

When you create a figure (in e.g. eps format) with R, the margins around the main context are always too wide. To save the space in the final documents, e.g. LaTeX generated pdf file. I have figured out two ways of reducing the margins.If you just want to shrink the white margin of an eps file, try Method A–C
If you want to crop an esp file, see Method D 

  • Method A (R users) Before you make your graph in R, use par(mar=c(bottom, left, top, right))to specify the margin you want to keep. The default value is c(5, 4, 4, 2) + 0.1. Try this example to see the differences.
    par(mar=c(5,4,4,2)+0.1) # The defualt margins plot(rnorm(100)) dev.copy2eps()          # Save as eps 
    par(mar=c(4,4,0,0)+0.1) # Figure with very tight margins plot(rnorm(100)) dev.copy2eps()
  • Method B (use epstool) Very handy tool that can handle the optimal bounding box
    epstool --copy --bbox file.eps file_new.eps
  • Method C (use ps2epsi)It automatically calculates the bounding box required for all encapsulated PostScript files, so most of the time it does a pretty good job
    ps2epsi <input.eps> <output.eps>
  • Method D (DIY for any eps )Use a text editor open your eps file and you will find a line like this
    %%BoundingBox: 0 0 503 503

    in the front lines of the file. Adjust these vales to proper integers. Save it and test if the margins are better. When you want to crop an eps file and include it into LaTeX with \includegraphics command, you should use  \includegraphics* instead. Because If * is present, then the graphic is ‘clipped’ to the size specified. If * is omitted, then any part of the graphic that is outside the specified ‘bounding box’ will over-print the surrounding text. By the way, the options trim, bb, viewport options in \includegraphics can do the same job in a different manner without editing the eps file, see the help document for details.