• Stars
    star
    150
  • Rank 247,323 (Top 5 %)
  • Language
    R
  • Created about 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ”§ R package: startup - Friendly R Startup Configuration
CRAN check status R CMD check status Coverage Status

startup: Friendly R Startup Configuration

Introduction

When you start R, it will by default source a .Rprofile file if it exists. This allows you to automatically tweak your R settings to meet your everyday needs. For instance, you may want to set the default CRAN repository (options("repos")) so you don't have to choose one every time you install a package.

The startup package extends the default R startup process by allowing you to put multiple startup scripts in a common 'Rprofile.d' directory and have them all be sourced during the R startup process. This way you can have one file to configure the default CRAN repository and another one to configure your personal devtools settings. Similarly, you can use a 'Renviron.d' directory with multiple files defining different environment variables. For instance, one file may define environment variable LANGUAGE, whereas another file may contain your private GITHUB_PAT key. The advantages of this approach are that it gives a better overview when you list the files, makes it easier to share certain settings (= certain files) with other users, and enable you to keep specific files completely private (by setting the file privileges so only you can access those settings).

How the R startup process works

When R starts, the following user-specific setup takes place:

  1. The first .Renviron file found on the R startup search path is processed. The search path is (in order): (i) Sys.getenv("R_ENVIRON_USER"), (ii) ./.Renviron, and (iii) ~/.Renviron. The format of this file is one ENV_VAR=VALUE statement per line, cf. ?.Renviron. NOTE: Some environment variables must be set already in this step in order to be acknowledged by R, i.e. it is too late to set some of them in Step 2 and Step 3 below.

  2. The first .Rprofile file found on the R startup search path is processed. The search path is (in order): (i) Sys.getenv("R_PROFILE_USER"), (ii) ./.Rprofile, and (iii) ~/.Rprofile. The format of this file must be a valid R script (with a trailing newline), cf. ?.Rprofile.

  3. If the .Rprofile file (in Step 2) calls startup::startup() then the following will also take place:

    a. The first 'Renviron.d' directory on the R startup search path is processed. The search path is (in order): (i) paste0(Sys.getenv("R_ENVIRON_USER"), ".d"), (ii) ./.Renviron.d, (iii) ~/.Renviron.d, and (iv) {user-config-dir}/Renviron.d, where {user-config-dir} corresponds to tools::R_user_dir("startup", which = "config"), e.g. ${XDG_CONFIG_HOME}/R/startup. The format of these files should be the same as for .Renviron. NOTE: Some environment variables must be set already in Step 1 above in order to be acknowledged by R.

    b. A set of handy R options that can be used in Step 3c are set. Their names are prefixed startup.session. - see ?startup::startup_session_options for details.

    c. The first 'Rprofile.d' directory found on the R startup search path is processed. The search path is (in order): (i) paste0(Sys.getenv("R_PROFILE_USER"), ".d"), (ii) ./.Rprofile.d, (iii) ~/.Rprofile.d, and (iv) {user-config-dir}/Rprofile.d. The format of these files should be the same as for .Rprofile, that is, they must be valid R scripts.

    d. If set, any R code in environment variable R_STARTUP_INIT, or R option startup.init, is parsed and evaluated.

    e. If set, any R script specified by environment variable R_STARTUP_FILE, or R option startup.file, is parsed and evaluated.

    f. If no errors occur above, the startup package will be unloaded, leaving no trace of itself behind, except for R options startup.session.* set in Step 3b - these will be erased if startup::startup() is called with keep = NULL.

All relevant files in 'Renviron.d' and 'Rprofile.d' directories, including those found recursively in subdirectories thereof, will be processed (in lexicographic order sorted under the C locale). There are no restrictions on what the file names should be (except for the ones ignored as explained below). However, for 'Rprofile.d' files we recommend to use filename extension *.R to indicate that the files are regular R scripts. For 'Renviron.d' files we recommend to use files without extensions (or *.Renviron for clarification). To avoid confusions, do not use an *.R extension for 'Renviron.d' files because they are not R script per se (as some editors may warn you about).

Files with file extensions *.txt, *.md and *~ are ignored as well as any files named .Rhistory, .RData and .DS_Store. Directories named __MACOSX and their content are ignored. Files and directories with names starting with two periods (..) are ignored, e.g. ~/.Rprofile.d/..my-tests/.

Installation

After installing the startup packages (see instructions at the end), call

startup::install()

once. This will append

tryCatch(startup::startup(), error=function(ex) message(".Rprofile error: ", conditionMessage(ex)))

to your ~/.Rprofile. The file will be created if missing. This will also create directories ~/.Renviron.d/ and ~/.Rprofile.d/ if missing. To find the location of these folder on Windows, use normalizePath("~") - it's often located under C:\Users\Alice\Documents\.

Alternatively to the above installation setup, you can just add that line to your ~/.Rprofile file manually. The reason for using tryCatch(..., error = ...) is for the case when startup is not installed and you try to install it, e.g. after upgrading R to a new major release. Without tryCatch(), R will fail to install the startup package (or any other package) because the R profile startup script produces an error complaining about startup not being available.

Usage

Just start R :)

To debug the startup process, use startup::startup(debug = TRUE) or set environment variable R_STARTUP_DEBUG=TRUE, e.g. on Linux you can do

$ R_STARTUP_DEBUG=TRUE R

On MS Windows, you can do:

$ R R_STARTUP_DEBUG=TRUE

This will produce time-stamped messages during startup specifying which files are included together with details on the files and what they modified.

Conditional file and directory names

If the name of a file consists of a <key>=<value> specification, then that file will be included / used only if the specification is fulfilled (on the current system with the current R setup). For instance, a file ~/.Rprofile.d/os=windows.R will be ignored unless startup::sysinfo()$os == "windows", i.e. the R session is started on a Windows system.

The following startup::sysinfo() keys are available for conditional inclusion of files by their path names:

  • System values:

    • dirname - (character) the name of the current working directory (= basename(getwd()))
    • gui - (character) the graphical user interface (= .Platform$GUI)
    • nodename - (character) the host name (= Sys.info()[["nodename"]])
    • machine - (character) the machine type (= Sys.info()[["machine"]])
    • os - (character) the operating system (= .Platform$OS.type)
    • sysname - (character) the system name (= Sys.info()[["sysname"]])
    • user - (character) the user name (= Sys.info()[["user"]])
  • System flags:

    • interactive - (logical) whether running R interactively or not (= interactive())
    • quiet - (logical) Indicates whether one of the R command-line arguments -q, --quiet, and --silent was specified
    • save - (logical) TRUE if R was launched with command-line argument --save, FALSE if launched with --no-save, otherwise NA
    • ess - (logical) whether running R in Emacs Speaks Statistics (ESS) or not
    • pqr - (logical) whether running pqR ("A Pretty Quick Version of R") or not
    • radian - (logical) whether running R in radian (formerly known as 'rtichoke' and 'rice') or not
    • microsoftr - (logical) whether running R in Microsoft R Open or not
    • rapp - (logical) whether running R in [R.app] or not (a macOS GUI)
    • rgui - (logical) whether running R in [Rgui] or not (an MS Windows GUI)
    • rstudio - (logical) whether running R in RStudio Console or not
    • rstudioterm - (logical) whether running R in RStudio Terminal or not
    • wine - (logical) whether running R on Windows via Linux Wine or not
  • Installed packages:

    • package - (character) whether a package is installed or not. In addition to checking the availability, having package=<name> in the filename makes it clear that the startup file concerns settings specific to that package.
  • With a specific frequency:

    • when - (character) specify how often the file should be processed:
      • when=once - the startup file is processed only once
      • when=hourly - the startup file is processed at most once per hour
      • when=daily - the startup file is processed at most once per day
      • when=weekly - the startup file is processed at most once per week
      • when=fortnightly - the startup file is processed at most once every two weeks
      • when=monthly - the startup file is processed at most once per month

    If such a file, or its timestamp, is updated, then it will be processed the next time R is started.

  • Environment variables:

    • Any further <key>=<value> specifications with keys matching none of the above known keys are interpreted as system environment variables and startup will test such conditions against their values. Note, if <key> does not correspond to a known environment variable, then the file is skipped if <key>=<value> is used but included if <key>!=<value> is used.

To condition on more than one key, separate <key>=<value> pairs by commas, e.g. ~/.Rprofile.d/work,interactive=TRUE,os=windows.R. This also works for directory names. For instance, ~/.Rprofile.d/os=windows/work,interactive=TRUE.R will be processed if running on Windows and in interactive mode. Multiple packages may be specified. For instance, ~/.Rprofile.d/package=devtools,package=future.R will be used only if both the devtools and the future packages are installed.

It is also possible to negate a conditional filename test by using the <key>!=<value> specification. For instance, ~/.Rprofile.d/package=doMC,os!=windows.R will be processed if package doMC is installed and the operating system is not Windows.

Secrets (conditionally on an environment variable)

Renviron and Rprofile startup files with a non-declared <key> in their file names are skipped. A non-declared key is any key that is neither one of the above predefined keys nor a declared environment variable.

This is useful, because it allows us to keep "secrets" in private files and only load them conditionally on the value of an environment variable. For instance, we put all our secret Renviron files under ~/.Renviron.d/private/SECRET=banana/ such that they are only loaded if environment variable SECRET is set to exactly banana. Moreover, by making sure the directory ~/.Renviron.d/private/ is only accessible by you, then it will be harder for someone else two know what your secret "password" (banana) is. On a Unix system, you can set this up as:

$ mkdir -p ~/.Renviron.d/private/SECRET=banana/
$ chmod go-rw ~/.Renviron.d/private

Then go ahead an put your secret Renviron files in there, e.g.

.Renviron.d/private/SECRET=banana/
  +-- amazon  ## AWS_* credential & settings
  +-- github  ## GITHUB_TOKEN + GITHUB_PAT

Now, only if SECRET is set to exactly banana when you launch R, the above secret files will be processed and the corresponding environment variables will be available from withing R. For instance, they will be loaded if you do:

$ SECRET=banana R

but not if you do:

$ SECRET=badguess R

or if SECRET is unset.

Comment: You can use whichever variable name you like, it does not have to be SECRET. And, the "password" banana is obviously just an example.

Known limitations

Setting environment variables during startup

Renviron startup files is a convenient and cross-platform way of setting environment variables during the R startup process. However, for some of the environment variables that R consults must be set early on in the R startup process (immediately after Step 1), because R only consults them once. Examples(*) of environment variables that need to be set no later than .Renviron (Step 1) are:

  • TMPDIR, TMP, TEMP - the parent of R's temporary directory, cf. ?tempdir
  • LC_ALL - locale settings used by R, e.g. cf. ?locales
  • R_DEFAULT_PACKAGES - default set of packages loaded when R starts, cf. ?Rscript
  • R_LIBS_USER - user's library path, e.g. R_LIBS_USER=~/R/%p-library/%v is the folder specification used by default on all platforms and and R version. The folder must exist, otherwise it is ignored by R. The %p (platform) and %v (version) parts are R-specific conversion specifiers, cf. ?R_LIBS_USER
  • R_MAX_NUM_DLLS, cf. ?dyn.load

Any changes to these done in an 'Renviron.d' file (Step 3a), or via Sys.setenv() in .Rprofile (Step 2) or 'Rprofile.d' files (Step 3c), will be ignored by R itself - despite being reflected by Sys.getenv().

Furthermore, some environment variables can not even be set in .Renviron (Step 1) but must be set prior to launching R. This is because those variables are consulted by R very early on (prior to Step 1). Examples(*) of environment variables that need to be set prior to .Renviron (Step 1) are:

  • HOME - the user's home directory
  • MKL_NUM_THREADS and OMP_NUM_THREADS - the default number of threads used by OpenMP etc, cf. R Installation and Administration

These variables have to be set using methods specific to the operating system or the calling shell, e.g. in a Unix shell

$ export OMP_NUM_THREADS=1
$ R

or per call as

OMP_NUM_THREADS=1 R

(*) For further details on which environment variables R consults and what they are used for by R, see the R documentation and help, e.g. ?"environment variables" and ?Startup.

Examples

Below is a list of "real-world" example files:

.Renviron.d/
  +-- lang
  +-- r_cmd_check
  +-- secrets=banana
 
.Rprofile.d/
  +-- interactive=TRUE/
      +-- help.start.R
      +-- misc.R
	  +-- package=fortunes.R
  +-- os=windows.R
  +-- repos.R

They are available as part of this package under system.file(package = "startup"), e.g.

> f <- system.file("Rprofile.d", "repos.R", package = "startup")
> file.show(f, type = "text")

local({
  repos <- c(CRAN = "https://cloud.r-project.org")
  if (.Platform$OS.type == "windows") {
     repos["CRANextra"] <- "https://www.stats.ox.ac.uk/pub/RWin"
  }
  options(repos = c(repos, getOption("repos")))
})

Installation

R package startup is available on CRAN and can be installed in R as:

install.packages("startup")

Pre-release version

To install the pre-release version that is available in Git branch develop on GitHub, use:

remotes::install_github("HenrikBengtsson/startup", ref="develop")

This will install the package from source.

More Repositories

1

future

πŸš€ R package: future: Unified Parallel and Distributed Processing in R for Everyone
R
916
star
2

progressr

δΈ‰ R package: An Inclusive, Unifying API for Progress Updates
R
254
star
3

future.apply

πŸš€ R package: future.apply - Apply Function to Elements in Parallel using Futures
R
200
star
4

matrixStats

R package: Methods that Apply to Rows and Columns of Matrices (and to Vectors)
R
191
star
5

Wishlist-for-R

Features and tweaks to R that I and others would love to see - feel free to add yours!
R
127
star
6

speedtest-cli-extras

πŸ“Ά Tools to enhance the speedtest-cli network tools
Shell
120
star
7

parallelly

R package: parallelly - Enhancing the 'parallel' Package
R
111
star
8

R.matlab

R package: R.matlab
R
84
star
9

future.batchtools

πŸš€ R package future.batchtools: A Future API for Parallel and Distributed Processing using batchtools
R
83
star
10

doFuture

πŸš€ R package: doFuture - Use Foreach to Parallelize via Future Framework
R
79
star
11

R.utils

πŸ”§ R package: R.utils (this is *not* the utils package that comes with R itself)
R
59
star
12

dirdf

R package: dirdf - Extracts Metadata from Directory and File Names
R
58
star
13

future.callr

πŸš€ R package future.callr: A Future API for Parallel Processing using 'callr'
R
56
star
14

R.cache

♻️ R package: R.cache - Fast and Light-weight Caching (Memoization) of Objects and Results to Speed Up Computations
R
35
star
15

profmem

πŸ”§ R package: profmem - Simple Memory Profiling for R
R
33
star
16

ucsf-vpn

Linux command-line client to manage a UCSF VPN connection
Shell
29
star
17

globals

🌐 R package: Identify Global Objects in R Expressions
R
28
star
18

listenv

R package: listenv - Environments Behaving As Lists
R
28
star
19

dotfiles-for-R

My dotfiles for R, e.g. .Rprofile and .Renviron
R
28
star
20

R.rsp

πŸ“„ R package: Dynamic generation of scientific reports
R
27
star
21

R.oo

R package: R.oo - R Object-Oriented Programming with or without References
R
20
star
22

brother-ptouch-label-printer-on-linux

How to print to a Brother P-touch (PT) label printer on Linux
Lua
18
star
23

R.devices

🎨 R package: Unified Handling of Graphics Devices
R
17
star
24

shellcheck-repl

Validation of Shell Commands Before Evaluation
Shell
14
star
25

future.mapreduce

[EXPERIMENTAL] R package: future.mapreduce - Utility Functions for Future Map-Reduce API Packages
R
13
star
26

port4me

πŸ†“ port4me - Get the Same, Personal, Free TCP Port over and over
Shell
13
star
27

marshal

R package: marshal - Framework to Marshal Objects to be Used in Another R Processes
R
13
star
28

TopDom

R package: TopDom - An efficient and Deterministic Method for identifying Topological Domains in Genomes
R
13
star
29

git-bioc

:octocat: LEGACY: Git commands to keep a Git repository and Bioconductor SVN in sync
Shell
10
star
30

article-bengtsson-future

H. Bengtsson, A Unifying Framework for Parallel and Distributed Processing in R using Futures, The R Journal, 10.32614/RJ-2021-048, 2021
TeX
10
star
31

future.tests

πŸ”© R package: future.tests - Test Suite for Future API Backends
R
10
star
32

future.clustermq

πŸš€ R package future.clustermq: A Future API for Parallel Processing using 'clustermq'
R
9
star
33

aroma.affymetrix

πŸ”¬ R package: Analysis of Large Affymetrix Microarray Data Sets
R
9
star
34

fake-hdf5r

R package: hdf5r - Fake, Dummy, Non-Working 'hdf5r' Package for 'Seurat' Users
R
8
star
35

future-tutorial-user2022

Tutorial: An Introduction to Futureverse for Parallel Processing in R (useR! 2022)
R
8
star
36

RNativeAPI

R package: RNativeAPI - Documentation and Examples of the R Native API (Proof of Concept)
R
8
star
37

future.BatchJobs

πŸš€ R package: future.BatchJobs: A Future API for Parallel and Distributed Processing using BatchJobs [Intentionally archived on CRAN on 2021-01-08]
R
8
star
38

PSCBS

πŸ”¬ R package: Analysis of Parent-Specific DNA Copy Numbers
R
7
star
39

BiocParallel.FutureParam

πŸš€ R package: BiocParallel.FutureParam - Use Futures with BiocParallel
Makefile
7
star
40

affxparser

πŸ”¬ R package: This is the Bioconductor devel version of the affxparser package.
C++
7
star
41

illuminaio

πŸ”¬ R package: This is the Bioconductor devel version of the illuminaio package.
R
6
star
42

x86-64-level

x86-64-level - Get the x86-64 Microarchitecture Level on the Current Machine
Shell
6
star
43

future.aws.lambda

R package: future.aws.lambda - A Future API for Parallel Processing on AWS Lambda
5
star
44

conda-stage

conda-stage: Stage a Conda Environment on Local Disk
Shell
5
star
45

ThinkpadX1-Windows10-Middle_mouse_button_issue

AutoHotkey
5
star
46

teeny

🐣 R package: teeny - A Minimal, Valid, Complete R Package
R
4
star
47

rcli

R package: rcli - R Command-Line Interface Extras
R
4
star
48

easycatfs

easycatfs - Easy Mounting of Slow Folders onto Local Disk
Shell
4
star
49

pkgdown.extras

R package: pkgdown.extras: Enhancing the 'pkgdown' Package
R
3
star
50

TopDomData

R package: TopDomData - Data for the TopDom Package
R
3
star
51

environments

[experimental] R package: environments - Working with Environments and Closures in R
R
3
star
52

CostelloPSCNSeq

R package: Parent-specific Copy-number Estimation Pipeline using HT-Seq Data
R
3
star
53

fix.connections

R package: fix.connections - Workarounds for Deficiencies in R's Built-in Connections [PROTOTYPE]
R
3
star
54

jottr.org-blogdown

JottR - Some Jotter on R
HTML
2
star
55

git-r

A Git Extension Making it Easier to Build R from Source
Shell
2
star
56

revdepcheck.extras

R package: revdepcheck.extras - Reverse-Dependency Checks from the Command Line (CLI)
R
2
star
57

r-base-centos7

Docker container image: Centos 7 with R (UNDER CONSTRUCTION)
2
star
58

R.filesets

R package: R.filesets - Easy Handling of and Access to Files Organized in Structured Directories
R
2
star
59

trackers

PROTOTYPE: trackers - Track Changes in R
R
2
star
60

CBI-software

A Scientific Software Stack for HPC (CentOS oriented)
Makefile
2
star
61

R_CRAN_Booster

Chrome Extension: R CRAN Booster - adds useful annotations to CRAN package pages
JavaScript
2
star
62

drat

R package repository
1
star
63

RGitHubAPI

R
1
star
64

dotfiles-for-emacs

Dot files for Emacs
Emacs Lisp
1
star
65

bash-startup

Bash Startup utility functions
Shell
1
star
66

markin

markin - The Markdown Injector
Shell
1
star
67

AutoHotkey-scripts

AutoHotkey
1
star
68

docker-spark-r

1
star
69

aroma.cn

πŸ”¬ R package: aroma.cn
R
1
star
70

aroma.agilent

πŸ”¬ R package: aroma.agilent [DORMANT]
R
1
star
71

R.batch

R package: R.batch [DEPRECATED]
R
1
star
72

R.lang

R.package: R.lang [DEPRECATED]
R
1
star
73

r-mirrors

Mirror CRAN and Bioconductor repositories on the local file system for R package installaions without internet access
Makefile
1
star
74

covr-utils

[LEGACY] Enhancements for covr making it even easier to do assess source-code coverage of R package tests
R
1
star
75

amazonlinux-r-minimal

Docker Hub Image: docker pull henrikbengtsson/amazonlinux-r-minimal
1
star
76

future.api.tests

[PLANNED] R package future.api.tests: Conformance Tests for the Future API
1
star
77

calmate

πŸ”¬ R package: calmate - Improved Allele-Specific Copy Number of SNP Microarrays for Downstream Segmentation
R
1
star
78

LinuxEnvironmentModules

R package: LinuxEnvironmentModules - An R API to Linux Environment Modules
R
1
star
79

Affx-Fusion-SDK

πŸ”¬ Affymetrix Fusion Software Developers Kit (SDK)
C++
1
star
80

aroma.core

πŸ”¬ R package: aroma.core - Core Methods and Classes Used by 'aroma.*' Packages Part of the Aroma Framework
R
1
star