Useful hints to personalize Gnu R.

Most of you should know Gnu R, but only a few might heard about it’s .rc !

Just create the file .R.rc in your home directory, and R will parse it if you run it. Some useful annotations:

  • There are to variables: `.First` and `.Last` . Assign a function to it and they are run at any start respectively quit of R.
  • On most screens R utilizes the width not effectively, either there is to much space left blank on the right side or the terminal breaks lines such that the output isn't readable anymore:
    # too much white space right:
    > 1:50
     [1]  1  2  3  4  5  6  7  8  9 10
    [11] 11 12 13 14 15 16 17 18 19 20
    [21] 21 22 23 24 25 26 27 28 29 30
    [31] 31 32 33 34 35 36 37 38 39 40
    [41] 41 42 43 44 45 46 47 48 49 50
    # to long lines so the terminal breaks them:
    > 1:50
     [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
     21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 
    42 43 44
    [45] 45 46 47 48 49 50
    So define the number of characters that can be presented in one line of your terminal at the start of R.:
    options(width=236)
  • It's of course possible to define functions that you often need. I've two screens with different width, so I created two functions that readjust the line width if I move the terminal from one screen to the other:
    bigScreen <- function() options(width=236)
    smallScreen <- function() options(width=180)

So here is a part of my .R.rc to give you an example:

###################################
#
# R.rc by Martin Scharm
#   copy to $HOME/.R.rc
#
# for more informations visit:
#          http://binfalse.de
#
###################################


options (width=236, digits=7, show.error.messages="TRUE", warn=1)

# freq used libs
library(ggplot2)
library(plyr)

# aliases
s <- base::summary;
h <- utils::head;
n <- base::names;

# executed at startup and quit
.First <- function () cat ("\n   Hey nice Guy! Have Fun!\n\n")
.Last <- function ()  cat ("\n   Goodbye!\n\n")

# adjust line length depending on screen width
bigScreen <- function () options (width=236)
smallScreen <- function () options (width=180)

# shorten quit ("yes")
bye <- function () quit ("yes")


##################################
# some frequently used functions #
##################################

# calc faculty with f() and faculty()
f = faculty = function (x) gamma (x + 1)

# find nth biggest element of a vector
biggest = function (data, x=1) which (data == sort (data)[length(data)-x+1])

# find nth smallest element of a vector
smallest = function (data, x=1) which (data == sort (data)[x])
Download: RC: .R.rc (Please take a look at the man-page. Browse bugs and feature requests.)

Martin Scharm

stuff. just for the records.

Do you like this page?
You can actively support me!

Leave a comment

There are multiple options to leave a comment: