Site icon R-bloggers

Customising your Rprofile

[This article was first published on r – Jumping Rivers, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

What is an Rprofile

Every time R starts, it runs through a couple of R scripts. One of these scripts is the .Rprofile. This allows users to customise their particular set-up. However, some care has to be taken, as if this script is broken, this can cause R to break. If this happens, just delete the script!

Full details of how the .Rprofile works can be found in my book with Robin on Efficient R programming. However, roughly R will look for a file called .Rprofile first in your current working directory, then in your home area. Crucially, it will only load the first file found. This means you can have per project Rprofile.

My Rprofile

A few months ago, I noticed my Rprofile was becoming increasing untidy, so I bundled it up into a single, opinionated, package. This also made it easier for me to switch between computers. Last week, there was an interesting twitter thread on customising your .Rprofile started by Kara Woo. The thread became popular with lots of great suggestions on neat customisations. This also provided the impetus to write this post.

Installation

You can install the package from GitHub with:

# install.packages("remotes")
remotes::install_github("csgillespie/rprofile")

The package also uses two non-cran packages

# Used for nice prompts
remotes::install_github("gaborcsardi/prompt")
# Used for nice colours in the terminal; not for Windows
remotes::install_github("jalvesaq/colorout")

R Prompt

You can make simple customisations to your R prompt using options(), but for extra bling I use the the prompt package.

As the RStudio console already has alot of nice features, e.g. syntax highlighting, a distinction needs to be made between the RStudio Console and running R in the terminal. So in .Rprofile I’ve got some logic to detect where I’m running R (in my .Rprofile) and adjust accordingly.

Useful Start-up Messages

If you use R a lot, you want to minimise noise. I used have the fortunes package display a fortune in my profile, but this got repetitive. Then I tried grabbing stuff from twitter, but this slowed everything down when the wifi was poor.

Currently three start-up messages are displayed:

If anyone wants to expand the Linux only functions to Windows and Macs, please submit a pull request!

Helper Functions

It’s always dangerous to load functions in your start-up script, so I’ve only included functions I’m fairly sure won’t be used in a script.

RStudio Functions

Setting Better options()

The set_startup_options() function sets better (in my opinion) set of start-up options. These include

I’ve also created a convenience function for adding additional R repositories – set_repos(). Probably not needed by most people.

Example .Rprofile

Open your .Rprofile, e.g. file.edit("~/.Rprofile") and customise however you want. Here’s an example

if (interactive() && requireNamespace("rprofile", quietly = TRUE)) {

  # Only useful if you use Makefiles
  rprofile::create_make_functions()

  # Startup options
  rprofile::set_startup_options()

  # Not RStudio console
  if (rprofile::is_terminal()) {
    rprofile::set_terminal()
  } else {
    rprofile::set_rstudio()
  }

  .env = rprofile::set_functions()
  attach(.env)
  # Display wifi and no of R sessions
  # Linux only
  rprofile::set_startup_info()
}

# Prints RStudio project on start-up
setHook("rstudio.sessionInit", function(newSession) {
  active_rproj = rprofile::get_active_rproj()
  if (!is.null(active_rproj)) {
    message(glue::glue("{crayon::yellow('R-project:')} {active_rproj}"))
  }
}, action = "append")

Notes and thanks


Jumping Rivers are RStudio Certified Full Service Partners. If you need help installing, or running RStudio Pro products, give us a shout. We might even be able to do it for free!

The post Customising your Rprofile appeared first on Jumping Rivers.

To leave a comment for the author, please follow the link and comment on their blog: r – Jumping Rivers.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.