Customizing R Session

The .Rprofile file is used to customize the R session every time you start it up. The R profile script (.Rprofile) can be created in the home directory. This script gets executed whenever you start a new R session. One can use it to pre-load libraries, set global options, or define custom functions. In this article we will discuss about customizing R session.

Customizing R Session

The R profile script (.Rprofile) file can be used to

  1. Change R’s default,
  2. Define handy command-line functions,
  3. Automatically load your favorite packages

Customizing R Session

On start-up R will look for the Rprofile in the following places:

1) R Home Directory: R.home() is used to find the directory path in which R is installed.
2) User’s Home Directory: path.expand("~") is used to find the user’s home directory.
3) R Current Working Directory: getwd() is used to find the R’s current working directory.

One can employ a few minor modifications on R default settings. For example, The default prompt is > and the output printed in the console is seven numbers after decimals. The following setting will:

  1. Replace the default standard R prompt
  2. Update (reduce) the number of digits from 7 to 4. Note: it does not reduce the precision with which these numbers are internally processed and stored.
  3. The show.signif.stars=FALSE will not show stars to indicate the significance of p-values at the conventional level.
options(prompt = "Imdad> ", digits = 4, show.signif.stars = F)
Customizing R session

Edit Profile uwing usethis Package

  • Use the usethis::edit_r_profile() function (from the usethis package) to edit your easily .Rprofile.
  • Remember to include sensitive information (like API keys) directly in the script. Consider using a separate
  • The .Renviron file for such cases.
  • If you have both a project-specific .Rprofile and a user-level one, source the user profile at the beginning of your project’s .Rprofile.

In summary .Rprofile scripte file allows the use to customize the R environment by setting options, loading libraries, and defining functions that you want available in every session.

Learn abuot R Workspace, Objects and .RData File

Leave a Reply

Discover more from R Frequently Asked Questions

Subscribe now to keep reading and get access to the full archive.

Continue reading