# Title: .Rprofile
# Author: Frédéric CHEVALIER <f15.chevalier@gmail.com>
# Version: 0.4
# Created in: 2014-07-06
# Modified in: 2016-10-02



#==========#
# Comments #
#==========#

# ~/.Rprodile: executed by R for interactive shells.
# see http://stat.ethz.ch/R-manual/R-devel/library/base/html/Startup.html



#==========#
# Versions #
#==========#

# v0.4 - 2016-10-02: hour display on prompt simplified / auto-update of terminal column value for display added
# v0.3 - 2014-11-03: less function added
# v0.2 - 2014-10-21: bug correction about width of terminal
# v0.1 - 2014-08-06: bug correction about interactivity (curly brackets missing)
# v0.0 - 2014-07-06: creation



#=======================#
# User specific options #
#=======================#

# Test if it is an interactive session
if (interactive()) {

# Create a new invisible environment for all the functions to go in so it doesn't clutter your workspace
.startup <- new.env()

# Don't ask me for my CRAN mirror every time
options("repos" = c(CRAN = "http://cran.revolutionanalytics.com"))



#===========#
# Functions #
#===========#

# Check if pacakge is installed
# source: http://r.789695.n4.nabble.com/test-if-a-package-is-installed-td1750671.html
require(utils, quietly = TRUE)
.startup$is.installed <- function(mypkg) {is.element(mypkg, installed.packages()[,1])}


# All functions executed when starting R
.First <- function() {
        
	# First message
	cat("   Welcome to R!\n\n")
	
	# Add colors to prompt
	options(colorout.anyterm = TRUE)
	if(! .startup$is.installed("colorout")) {
		cat("If you want colored prompt, you should install the colorout package.\nLink: http://www.lepem.ufc.br/jaa/colorout.html\n\n")
	} else {
		library('colorout')
		setOutputColors256(
	    	normal = 40,
		    number = 214,
    		string = 85,
	    	const = 35,
		    stderror = 45,
    		error = c(1, 0, 1),
	    	warn = c(1, 0, 100)
    	)
		cat("\n")
    }
	
}


# All functions executed when exiting R
.Last <- function() {
    if (! any(commandArgs() == '--no-readline')) {
        require(utils, quietly=TRUE)
        #timestamp(quiet=TRUE)
        try(savehistory(Sys.getenv("R_HISTFILE")))
     }
}


# Prompt showing current clock
# source: http://stackoverflow.com/questions/4222476/r-display-a-time-clock-in-the-r-command-line
# Don't use Tcl/Tk beacuse starting X11 leading to R crash when X11 connection is killed
myprompt <- "%H:%M R> "
.startup$updatePrompt <- function(..., quietly=FALSE) {
    # Update time
    options(prompt=format(Sys.time(),myprompt))

    # Update terminal column value for display in R terminal
    # source: http://stackoverflow.com/questions/1172485/how-to-increase-the-number-of-columns-using-r-in-linux
    options(width=as.integer(system("stty -a | head -n 1 | awk '{print $7}' | sed 's/;//'", intern=TRUE)))

    if (quietly == FALSE) {return(TRUE)}
}
addTaskCallback(.startup$updatePrompt)
.startup$updatePrompt(quietly=TRUE)


# Exit
.startup$bye <- function () {q("no")}

# Less emulation
# source: http://stackoverflow.com/questions/3503811/is-there-an-r-equivalent-to-the-bash-command-more
.startup$less <- function(x) {
    file <- tempfile()
    sink(file); on.exit(sink())
    print(x)
    file.show(file, delete.file = T)
}



#=========#
# Aliases #
#=========#

# Moving (bash-like command)
.startup$cd <- setwd
.startup$pwd <- getwd


# Attach all the variables above
attach(.startup)

} # End of interactive statement