csgillespie / rprofile

My .Rprofile set-up
https://csgillespie.github.io/rprofile/
50 stars 11 forks source link

My opinionated .Rprofile

CRAN
status Lifecycle:
experimental R-CMD-check Codecov test
coverage

A package (of my) Rprofile customisations. The goal is it broaden the package to be of use to other people.

Installation

You can install the development version 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

The R prompt has also been customised (using the prompt package):

A distinction needs to be made between the RStudio Console and the terminal. The console already has lots of nice features, e.g. syntax highlighting. So I have two separated functions.

Useful Start-up Messages

Currently three start-up messages are displayed:

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

# Set options for Rscript -e calls
if (requireNamespace("rprofile.setup", quietly = TRUE)) {
  # Call here if needed
  # rprofile.setup::set_repos()
  rprofile.setup::set_startup_options()
}

if (interactive() && requireNamespace("rprofile.setup", quietly = TRUE)) {
  rprofile.setup::create_make_functions()
  # Not RStudio OR RStudio console
  if (rprofile.setup::is_terminal()) {
    rprofile.setup::set_terminal()
  } else {
    rprofile.setup::set_rstudio()
  }
  .env = rprofile.setup::set_functions()
  attach(.env)
  # Display wifi and no of R sessions
  # Linux only
  rprofile.setup::set_startup_info()
}

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

Notes

Other information