Skip to content

Instantly share code, notes, and snippets.

@IdoBar
Last active January 26, 2020 12:05
Show Gist options
  • Save IdoBar/222ab8c0d10481f793dc54f4dc857d0e to your computer and use it in GitHub Desktop.
Save IdoBar/222ab8c0d10481f793dc54f4dc857d0e to your computer and use it in GitHub Desktop.
Example of my .Rprofile
# If loading in an environment that doesn't respect my RPATH environment
# variable, set it here
sourceDir <- file.path(path.expand("~"), "R", "source")
if (Sys.getenv("RPATH") == "") {
if (!file.exists(sourceDir)) dir.create(sourceDir)
Sys.setenv(RPATH=sourceDir)
}
# Load commonly used functions and packages
# enable auto-completion of package names
utils::rc.settings(ipck=TRUE)
# Enable more than 100 packages loaded
if (getRversion() >= "3.4.0") Sys.setenv("R_MAX_NUM_DLLS"=300)
# Load custom functions from util.R file (see in https://gist.github.com/IdoBar/7f63547158ecdbacf31b54a58af0d1cc)
# if (interactive()){
# if (require(devtools) & curl::has_internet()) devtools::source_gist("7f63547158ecdbacf31b54a58af0d1cc", filename = "util.R")
# if (file.exists(search.path(utilsFileName))) source(search.path(utilsFileName))
# }
# Setup favourite CRAN mirror and a site install library.
# Choose CRAN mirror (1 will use https://cloud.r-project.org/ to Automatically redirect to servers worldwide through )
utils::chooseCRANmirror(ind = 1)
# fix "Warning: unable to access index for repository https://www.stats.ox.ac.uk/pub/RWin/src/contrib"
# https://stackoverflow.com/questions/45108484/warning-unable-to-access-index-for-repository-https-www-stats-ox-ac-uk-pub-rw#45112048
options(repos = getOption("repos")["CRAN"])
# options(repos=structure(c(CRAN="https://cloud.r-project.org/")))
# myLib <- "C:/Users/Public/R/MRO-3.3.1/library"
# .libPaths(c(myLib, .libPaths()))
# Function to run when R is started
.First <- function() cat(sprintf("\n\nDefault location to install new packages: '%s' \nCRAN mirror is set to: '%s'. \n\n Welcome to R!\n\n",
.libPaths()[1], getOption("repos")["CRAN"]))
@IdoBar
Copy link
Author

IdoBar commented Feb 13, 2017

An example of an .Rprofile, used to define basic environment and load custom functions from util.R.
Place this file in your home folder (~ in linux or My Documents in windows)

@IdoBar
Copy link
Author

IdoBar commented Feb 15, 2017

Updated line 5: dir.exist() to file.exists() to be compatible with older R versions (<3.2)

@IdoBar
Copy link
Author

IdoBar commented Sep 8, 2017

Added an environment variable declaration R_MAX_NUM_DLLS=200 to support larger number of packages to be loaded (for R versions >=3.4)

@IdoBar
Copy link
Author

IdoBar commented Jan 30, 2019

Updated default CRAN repository

@IdoBar
Copy link
Author

IdoBar commented Jun 3, 2019

Another update to default CRAN repository (use top of list rather than hard-coded address).
In addition, remove sourcing of util.R, it is better practice to source it from Gist only in scripts as needed for portability.

@IdoBar
Copy link
Author

IdoBar commented Jan 26, 2020

Remove CRANExtra repository which causes warning (and leads to errors in the installation of some packages).
See this SO thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment