### This is the system Rprofile file. It is always run on startup.
### Additional commands can be placed in site or user Rprofile files
### (see ?Rprofile).

### Notice that it is a bad idea to use this file as a template for
### personal startup files, since things will be executed twice and in
### the wrong environment since user profiles are run in .GlobalEnv.

.GlobalEnv <- globalenv()
attach(NULL, name = "Autoloads")
.AutoloadEnv <- as.environment(2)
assign(".Autoloaded", NULL, env = .AutoloadEnv)
T <- TRUE
F <- FALSE
R.version <- structure(R.Version(), class = "simple.list")
version <- R.version            # for S compatibility
R.version.string <-
    paste(paste("R version",
                paste(version[c("major", "minor")], collapse = ".")),
          paste(version[c("year", "month","day")], collapse = "-"),
          sep=", ", collapse=" ")

options(na.action = "na.omit")
options(show.signif.stars = TRUE)
options(show.coef.Pvalues = TRUE)
options(keep.source = interactive())
options(warn = 0)
options(help.try.all.packages = FALSE)
options(CRAN = "http://cran.r-project.org")
options(BIOC= "http://www.bioconductor.org")
options(repositories=function() {
                           c(CRAN=contrib.url(getOption("CRAN")),
                                BIOC=contrib.url(getOption("BIOC")))})

options(timeout = 60)
options(internet.info = 2)
options(encoding = native.enc)
options(show.error.messages = TRUE)
options(scipen = 0)
options(locatorBell = TRUE)
options(ts.eps = 1e-5)   # default as S
local({dp <- as.vector(Sys.getenv("R_DEFAULT_PACKAGES"))
       if(identical(dp, "")) # marginally faster to do methods last
           dp <- c("utils", "graphics", "stats", "methods")
       else if(identical(dp, "NULL")) dp <- character(0)
       else dp <- strsplit(dp, ",")[[1]]
       dp <- sub("[[:blank:]]*([[:alnum:]]+)", "\\1", dp) # strip whitespace
       options(defaultPackages = dp)
    })

.First.sys <- function()
{
    last <- rev(getOption("defaultPackages"))[1]
    for(pkg in getOption("defaultPackages")) {
        res <- require(pkg, quietly = TRUE, warn.conflicts = FALSE,
                       character.only = TRUE, save = FALSE)
        if(!res)
            warning("package ", pkg,
                    ' in options("defaultPackages") was not found', call.=FALSE)
    }
    sch <- search()
    if(! "package:utils" %in% sch) {
        autoload("example", "utils")
        autoload("methods", "utils")
        autoload("str", "utils")
    }
    if(! "package:stats" %in% sch) {
        autoload("aov", "stats")
        autoload("lm", "stats")
        autoload("lowess", "stats")
        autoload("model.frame", "stats")
        autoload("predict", "stats")
        autoload("quantile", "stats")
        autoload("rexp", "stats")
        autoload("rnorm", "stats")
        autoload("rpois", "stats")
        autoload("rt", "stats")
        autoload("runif", "stats")
        autoload("ts", "stats")
        autoload("var", "stats")
    }
    if(! "package:graphics" %in% sch) {
        autoload("barplot", "graphics")
        autoload("boxplot", "graphics")
        autoload("contour", "graphics")
        autoload("coplot", "graphics")
        autoload("hist", "graphics")
        autoload("identify", "graphics")
        autoload("image", "graphics")
        autoload("layout", "graphics")
        autoload("lines", "graphics")
        autoload("matplot", "graphics")
        autoload("pairs", "graphics")
        autoload("par", "graphics")
        autoload("persp", "graphics")
        autoload("plot", "graphics")
        autoload("points", "graphics")
        autoload("text", "graphics")
        autoload("xy.coords", "graphics")
    }
}
###-*- R -*- Unix Specific ----

.Library <- file.path(R.home(), "library")
invisible(.libPaths(unlist(strsplit(Sys.getenv("R_LIBS"), ":"))))
options(papersize = as.vector(Sys.getenv("R_PAPERSIZE")))
options(printcmd = as.vector(Sys.getenv("R_PRINTCMD")))
options(latexcmd = as.vector(Sys.getenv("R_LATEXCMD")))
options(dvipscmd = as.vector(Sys.getenv("R_DVIPSCMD")))
options(unzip = as.vector(Sys.getenv("R_UNZIPCMD")))
options(browser = as.vector(Sys.getenv("R_BROWSER")))
options(editor = as.vector(Sys.getenv("EDITOR")))
options(pager = file.path(R.home(), "bin", "pager"))
options(pdfviewer = as.vector(Sys.getenv("R_PDFVIEWER")))
if(interactive()) {
    if (.Platform$GUI == "AQUA")
	options(device = "quartz")
    else if (!is.null(Sys.info) && (Sys.info()["sysname"] == "Darwin") && (Sys.getenv("DISPLAY") != ""))
	options(device = "X11")
    else if (Sys.getenv("DISPLAY") != "")
	options(device = switch(.Platform$GUI, "Tk" = "X11",
		"X11" = "X11", "GNOME" = "X11", "postscript"))
    else options(device = "postscript")
} else options(device = "postscript")
## the next two must be set for x11 to be used, even non-interactively
options(X11colortype = "true")
options(X11fonts = c("-adobe-helvetica-%s-%s-*-*-%d-*-*-*-*-*-*-*",
	"-adobe-symbol-*-*-*-*-%d-*-*-*-*-*-*-*"))
options(mailer = "mailx")

MacRoman <- as.integer(c(0:126, 32, 196, 197, 199, 201, 209, 214, 220,
225, 224, 226, 228, 227, 229, 231, 233, 232, 234, 235, 237, 236, 238,
239, 241, 243, 242, 244, 246, 245, 250, 249, 251, 252, 32, 176, 162,
163, 167, 32, 182, 223, 174, 169, 32, 146, 152, 32, 198, 216, 32, 177,
32, 32, 165, 181, 32, 32, 32, 32, 32, 170, 186, 32, 230, 248, 191, 161,
172, 32, 32, 32, 32, 171, 187, 32, 32, 192, 195, 213, 32, 32, 32, 32,
32, 32, 96, 39, 247, 32, 255, 32, 32, 32, 32, 32, 32, 32, 32, 183, 32,
32, 32, 194, 202, 193, 203, 200, 205, 206, 207, 204, 211, 212, 32, 210,
218, 219, 217, 144, 147, 148, 149, 150, 151, 154, 155, 157, 158, 159))

ISOLatin1 <- 0:255

WinAnsi <- as.integer(c(0:126, 32, 32, 32, 32, 32, 32, 32, 32, 32,
147, 32, 32, 32, 32, 32, 32, 32, 32, 96, 39, 32, 32, 32, 32, 32, 148,
32, 32, 32, 32, 32, 32, 32, 32, 161:255))

## non standard settings for the Aqua GUI of the Darwin port
if(.Platform$GUI == "AQUA") {

    ## this is set to let RAqua use both X11 device and
    ## X11/TclTk
    if (Sys.getenv("DISPLAY") == "")
	Sys.putenv("DISPLAY" = ":0")

    ## sets initial current working directory to user's root
    ## as R.app is installed at system level

    ## this is to allow g77 compiler to work
    Sys.putenv("PATH" = paste(Sys.getenv("PATH"),":/usr/local/bin",sep = ""))

    if( !file.exists("~/Library/R") )
	dir.create(path.expand("~/Library/R"))
    if( !file.exists("~/Library/R/library") )
	dir.create(path.expand("~/Library/R/library"))
    .libPaths(path.expand("~/Library/R/library"))
}## end "Aqua"
