Using .RProfile to Customize your R Environment


[:en]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.

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.


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")



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.


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[:]
Dan Buskirk

Written by Dan Buskirk

"The pleasures of the table belong to all ages." Actually, Brillat-Savaron was talking about the dinner table, but the quote applies equally well to Dan’s other big interest, tables of data. Dan has worked with Microsoft Excel since the Dark Ages and has utilized SQL Server since Windows NT first became available to developers as a beta (it was 32 bits! wow!). Since then, Dan has helped corporations and government agencies gather, store, and analyze data and has also taught and mentored their teams using the Microsoft Business Intelligence Stack to impose order on chaos. Dan has taught Learning Tree in Learning Tree’s SQL Server & Microsoft Office curriculums for over 14 years. In addition to his professional data and analysis work, Dan is a proponent of functional programming techniques in general, especially Microsoft’s new .NET functional language F#. Dan enjoys speaking at .NET and F# user’s groups on these topics.

Chat With Us