How to Access and Edit Rprofile

Locate the .Rprofile file generating default options

Like @Gsee suggested, ?Startup has all you need. Note that there isn't just the user profile file, but also a site profile file you could have messed with. And that both files can be found in multiple locations.

You could run the following to list existing files on your system among those listed on the page:

candidates <- c( Sys.getenv("R_PROFILE"),
file.path(Sys.getenv("R_HOME"), "etc", "Rprofile.site"),
Sys.getenv("R_PROFILE_USER"),
file.path(getwd(), ".Rprofile"),
file.path(Sys.getenv("HOME"), ".Rprofile"))

Filter(file.exists, candidates)

Note that it should be run on a fresh session, right after your started R, so that getwd() will return the current directory at startup. There is also the tricky possibility that your profile files do modify the current directory at startup, in which case you would have to start a "no-profile" session (run R --no-site-file --no-init-file) before running the code above.

How to modify and save Rprofile.site under Windows?

You don't have write access to the program files folder. To get it on a temporary basis start Notepad++ as administrator from the right click menu of the shortcut you use to start it. Then you will be able to write this file.

Expert R users, what's in your .Rprofile?

Here is mine. It won't help you with the coloring but I get that from ESS and Emacs...

options("width"=160)                # wide display with multiple monitors
options("digits.secs"=3) # show sub-second time stamps

r <- getOption("repos") # hard code the US repo for CRAN
r["CRAN"] <- "http://cran.us.r-project.org"
options(repos = r)
rm(r)

## put something this is your .Rprofile to customize the defaults
setHook(packageEvent("grDevices", "onLoad"),
function(...) grDevices::X11.options(width=8, height=8,
xpos=0, pointsize=10,
#type="nbcairo")) # Cairo device
#type="cairo")) # other Cairo dev
type="xlib")) # old default

## from the AER book by Zeileis and Kleiber
options(prompt="R> ", digits=4, show.signif.stars=FALSE)


options("pdfviewer"="okular") # on Linux, use okular as the pdf viewer

Sourcing in Rprofile.site: How to find the location of the sourced file

If you can't use absolute paths and that your working directory is not stable, one way is to use .libPaths or .Library.

By default your Rprofile should be in directory paste0(.Library,"/../etc/") or paste0(.libPaths()[2],"/etc/") so you can put your file there and source it with :

source(paste0(.Library,"/../etc/my_settings.R")) 
source(paste0(.libPaths()[2],"/etc/my_settings.R"))

As far as I understand the first option is stable (I don't think one can change the value of .Library).

If you use the second option just make sure that if in the future you alter your .libPaths() you do it after sourcing your file.

See ?.libPaths for more info on default folders.

How can I modify the startup message in R? Where is the code with this message?

You could set a personal startup message for R (which also will show in RStudio) by adding a line with message() in the .Rprofile.

In my case, the .Rprofile is in "My Documents" under Windows and is edited with the free tool Notepad++.

At the end of the file I have a line

### KoenV: Illustrate setting R startup message
message("I have changed my message")

The following appears when I start R

Sample Image

You can read more about accessing and editing this file in this post.

How to automatically load settings in R on OSX? How to find R_HOME, configure Rprofile.site, etc?

Over the years I have come to rely on the help(Startup) documentation as the best place to read up on this. There are numerous per-user and per-site configuration file as is customary for rich applications. It may seem like overkill at first but it is a really good system. And once you grok Renviron versus Renviron.site and dito for Rprofile, you appreciate the consistent behaviour across platforms.



Related Topics



Leave a reply



Submit