Optional: Modify Rprofile to change R startup

Note: I don’t recommend that you edit .Rprofile until later in the semester. This page is just for how to do it, not my recommendation for you to do it as you are first learning about R.

What’s on this page

  1. Modify R startup by creating an .Rprofile settings file and Loading R Commander
  2. Background
  3. Change locale language
  4. References
  5. Quiz

What to do

  1. Learn about the R environment,
  2. Modify .Rprofile to change how R behaves at startup
    1. Set working directory
    2. Set CRAN mirror
    3. Load R Commander at startup
    4. Set language

How to do it

You’ve been working with R and R Commander for a while now, so it’s time to think about how to have R work with you (and not against you!). R allows you to modify startup. For one, we expect you to maintain and work from a working directory, e.g., ~Desktop/BI311. For another example, since we use R Commander, why not have R load R Commander for you at start up?  These requests can be handled by modifying configuration files, including a user-defined .Rprofile. However, before you begin, I recommend you first read the Background below (scroll down, or click here).

Note: This page describes modifications to .RProfile. To make changes system-wide, modify RProfile.site, the first file R looks to during startup. RProfile.site should be located in R_HOME/etc/. You can find R_HOME by running the command R.home(component = "home"). For example, on my Win11 desktop, Rprofile.site is locate in "C:/R/R-4.3.1".

1. Modify R startup by creating an .Rprofile settings file and Loading R Commander

The following instructions assumes you have no site-wide or user-defined .Rprofile files in operation. If after making the changes the behavior is not what you expect, please read the Background below (scroll down, or click here).

1. Start R

2. Start R Commander

3. Once R Commander starts, go to Tools → Save Rcmdr options…

The following window will popup (Fig. 1).

Figure 1. R Commander options window

You are now ready to edit the .Rprofile text file.

4. Click in the blank area in the upper portion of the edit window

Type in your command to change to your working directory, e.g., on my Mac,

setwd("/Users/USERNAME/Desktop/BI311")

(obviously, replace “USERNAME” with your user name. Mine is mikedohm, as you may be able see in the output, Fig 2.)

Go ahead and make permanent your CRAN mirror choice. Type in the following

# set a CRAN mirror
local({r <- getOption("repos")
r["CRAN"] <- "https://cloud.r-project.org/"
options(repos=r)})

5. Next remove the # before the four lines as instructed in the message so that R commander will automatically load at start up. Your edits should look like this next screenshot (Fig. 2)

Figure 2. Screenshot of changes to Options

6. Click on the OK button, select the location where to save your Rprofile (probably best to save in your user name folder (e.g., /Users/USERNAME), then exit from R and R Commander.

7. Restart R and note behavior. Startup will likely be slower because of the number of packages that must be loaded. If you don’t like the changes, simply remove the .Rprofile file from the folder.

2. Background

When you start R, the program looks for one of several files that tell R how to act, its environment. Complete details on startup are available in the R manual, accessed on your computer by typing ?Startup at the R prompt.

R has a number of ways of setting the environment, we’ll focus on just one. We’ll create and use a “user profile” file called .Rprofile (that’s a file that starts with a dot, and the extension is Rprofile). Although it is a hidden file, It’s just a text file, so you can open and change it with any text editor.

Note: Hidden files with the prefix “.” trace back to the UNIX days. MacOS and Windows PC by default hide these files from the user. To view hidden files on macOS, in Finder window, keyboard shortcut Command plus Shift plus . keys. (To hide files, repeat the shortcut key strokes Cmd plus Shift plus .).  To view hidden files on Windows PC, either keyboard shortcut CTRL plus H or right-click anywhere in a folder and check the Show hidden files option at the bottom. Note a keyboard shortcut combination like CTRL plus H means the user presses the Ctrl key first, holds it down, and then presses the H key to initiate the action.

Since we’re just getting started with R, I’m not expecting that you have created or modified any .Rprofile files yet. Be advised that this page describes steps assuming that there are no .Rprofile files on your computer. If you run If you’ve done something different from what I expect, and you do have .Rprofile files on your computer, the best advise is to search and remove these files before proceeding. Note that although .Rprofile is just a text file, your computer likely hides this, and other files like it, from you. Users may need to make these files viewable. Search “hidden files” in FAQ about your computers on your CANVAS site.

When R starts, it looks for a copy of a user-defined .Rprofile in the current directory or in the user’s home directory. The home directory refers to the top-level directory of the R installation being run (for more information, see the R manual, type at the R prompt, ?Rhome). Your home directory was set at installation of R.

Locate your home directory

At the R prompt, type and submit the command

R.home()

On my office Mac computer, the output is

[1] "/Library/Frameworks/R.framework/Resources"

On Windows 10, the output should look like

[1] "C:/PROGRA~1/R/R-35~1.1"

Check if you have site-wide Rprofile setup

At the R prompt, type and submit the command

R.home(component = "home")

and then go to that folder on your computer and look for Rprofile.site. If that file is present, remove it. On my computer this command again returned

[1] "/Library/Frameworks/R.framework/Resources"

I didn’t have a site Rprofile file on my macos, so there was nothing to do; see second note above for my Win11 desktop.

Check for user-defined .Rprofile

To find user-defined Rprofile configuration files, look in the folder revealed by the following command

path.expand("~")

On my computer, this command showed

[1] "/Users/mikedohm"

Navigate to that folder and remove any .Rprofile or .Rhistory files.

Chances are you won’t need this information. The purpose of this step was for you to gather information about your computer in case we have to do some troubleshooting with your R installation. If the following instructions for setting up a user-defined Rprofile file doesn’t result in the behavior you expected, then likely you have settings files in locations on your computer — apply the commands and follow the suggested instructions under Background to clean up your R environment settings.

3. Change locale language

At installation of R, R assumes the language you wish R to communicate with you is the default language set for your computer (your LOCALE).

You can confirm the language setting by exploring the environment. At the R prompt type

Sys.getenv(c("LANG"))

On my computer, R returns

[1] "en_US.UTF-8"

Alternatively, a simple way to check the default language is to see the langue R uses in error message. At the line prompt In R, type and submit (press return key)

2 + x

An error message should be printed because you haven’t define “x.” If Spanish is your default system (computer) language setting, then the error message will be

Error: objeto 'x' no encontrado

We can set the default language to English by submitting a simple command. At the prompt type (and submit)

Sys.setenv(LANG = "en")

Repeat the simple function call and confirm the error message is in English

2 + x

Error: object 'x' not found

Start R Commander again and confirm that English has been set.

Note: If R is running, to restart R Commander recall you type Commander() at the prompt — you don’t need to reload the Rcmdr libarary.

Now, this isn’t a permanent solution — you’d have to make changes to the .RProfile or the .Renviron file. It’s a simple enough task, but it’s off topic — we want you to be working on statistics! So, all you have to remember to do is, before starting Rcmdr, type and submit

Sys.setenv(LANG = "en")

For other language settings, see list of ISO 639-1 codes at Wikipedia.

4. References

http://www.onthelambda.com/2014/09/17/fun-with-rprofile-and-customizing-r-startup/

An Introduction to R, https://cran.r-project.org/doc/manuals/r-release/R-intro.html

5. Page quiz


RProfile quiz

Five questions from this page