Modify RStudio prompt to show current git branch

Author : Sébastien Rochette
Categories : tips, development, rstudio
Tags :
Date :

At the last Raddicts Paris Meetup, Romain Francois (to be followed on twitter here) made one strong impression, among others, with his RStudio console which indicates the active git branch and the RAM memory used by R.

If you too want to have this (to shine in society or because you spend your time doing git checkout):

git info shown in R console prompt in RStudio

 

Here is the way to go:

This code to show the active git branch and R memory used needs to be hosted in your ".Rprofile"

  • You first need to install package {prompt} from Gabor Csardi. This package is not on CRAN, you will need to install it from GitHub using:
    remotes::install_github("gaborcsardi/prompt")
  • Dependencies may not be directly installed and you will need to additionally install package {memuse}:
    install.packages("memuse")
  • Then, open your ".Rprofile", using {usethis}
    usethis::edit_r_profile()
  • The content of your ".Rprofile" is executed as soon as you start R. Add the following code in this file:
prompt::set_prompt(function(...){
paste0(
"[", prompt::git_branch(), prompt::git_dirty(), prompt::git_arrows(), "] ",
prompt::prompt_memuse()
)
})

Tada !

Thanks Romain !!


Comments


Also read