}

Using .RProfile to Customize your R Environment

When R starts up, it looks for several files to initialize its configuration. R has its own configuration file called Rprofile.site located in the /etc folder. It is probably wise not to mess with this file unless you are very confident in your understanding. Most R users are aware that during startup, R reads an .RData file (if there is one). This restores the variable and dataframes to memory that had been saved during a previous session. On startup R will also read the .RProfile belonging to the individual user if it exists. It is this file that can be modified to customize a particular user's R environment.

screenshot of R environment

A Word about a Dot


On Unix and Linux systems, a user's home directory if often filled with lots of files beginning with a period; these files are used by many applications to configure themselves on startup. You don't see these files, however; that's why the dot is there. In Linux, filenames beginning with are not displayed unless you specifically ask to see them. The same naming convention is used by R running on Windows, but .RProfile is displayed like any other file. The closest thing in Windows to the Linux "home" directory is an individual's "Documents" directory, which is often seen as "My Documents" in the Windows user interface. If a file named .RProfile exists in this directory, R will read it on startup and curiously, Windows users can save a filename beginning with a period, but the Explorer will not let you rename a file to the same name. Command prompt renaming works fine.

.RProfile


Pretty much any R code is fair game for .RProfile, but we will focus on two important potential functions. If .RProfile contains a function called ".First" that function will be executed, well, first. Similarly, if there is a function named ".Last", that function will be executed at the end of an R session.

Here we see a prototype .RProfile file:

.First <- function(){

# options(repos=structure(c(CRAN="https://cran.****.gov/")))

cat("\nHello, Dan ", date(), "\n")

}

# the .Last() function can be used to save the workspace image to a consistent directory,

# regardless of the current working directory

.Last <- function(){

cat("\nGoodbye, Dan ", date(), "\n")

save.image("C:\\Temp\\.RData")

}

In this example, the .First function simply greets me, just to illustrate that the code is working. The line of code (commented out) invoking the options( ) function represents a potential practical use. In some agencies, users are not allowed to download R packages from any server except a secure and monitored server controlled by the agency. The .First function sets the appropriate Cran mirror. This is, of course, not a way to implement security; it's just a convenience to help a user attempting to install a package. Of course, the .First( ) function can be used to create any startup state you wish.

The .Last function is a great way to keep track of environment files. When shutting down, R presents the user with the option of saving an image of values and data in memory, but R will save this to the current working directory. If desired, the .Last function can be used to ensure that the R environment is consistently saved to the desired directory. Concatenating the current date to the end of the workspace filename creates a sequence of workspace backups of your R sessions.

Conclusion


It is easy to modify a user's .RProfile file to customize his or her R environment. Enforcing consistency in saving environment data is one useful application of .RProfile.

Related Training:

Business Intelligence

Chat With Us