Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
R in Action, Second Edition.pdf
Скачиваний:
540
Добавлен:
26.03.2016
Размер:
20.33 Mб
Скачать

appendix B Customizing the startup environment

One of the first things that programmers like to do is customize their startup environment to conform to their preferred way of working. Customizing the startup environment allows you to set R options, specify a working directory, load commonly used packages, load user-written functions, set a default CRAN download site, and perform any number of housekeeping tasks.

You can customize the R environment through either a site-initialization file (Rprofile.site) or a directory-initialization file (.Rprofile). These are text files containing R code to be executed at startup.

At startup, R will source the file Rprofile.site from the R_HOME/etc directory, where R_HOME is an environment value. It will then look for an .Rprofile file to source in the current working directory. If R doesn’t find this file, it will look for it in the user’s home directory. You can use Sys.getenv("R_HOME"), Sys.getenv ("HOME"), and getwd() to identify the location of R_HOME, HOME, and current working directory, respectively.

You can place two special functions in these files. The .First() function is executed at the start of each R session, and the .Last() function is executed at the end of each session. An example of an Rprofile.site file is shown in listing B.1.

Listing B.1 Sample Rprofile.site file

options(papersize="a4")

 

options(editor="notepad")

 

options(tab.width = 2)

 

options(width = 130)

Sets common options

options(digits=4)

 

options(stringsAsFactors=FALSE)

 

options(show.signif.stars=FALSE)

 

 

 

538

APPENDIX B Customizing the startup environment

539

Sets path for local library

grDevices::windows.options(record=TRUE)

 

 

options(prompt="> ")

 

 

Sets R interactive prompt

 

options(continue="+ ")

 

 

 

 

 

 

.libPaths("C:/my_R_library")

 

 

local({r <- getOption("repos")

 

Sets CRAN mirror default

 

r["CRAN"] <- "http://cran.case.edu/"

options(repos=r)})

 

 

.First <- function(){ library(lattice) library(Hmisc)

source("C:/mydir/myfunctions.R") cat("\nWelcome at", date(), "\n")

}

.Last <- function(){

cat("\nGoodbye at ", date(), "\n")

}

Startup function

Session end function

There are several things you should note about this file:

Setting a .libPaths value allows you to create a local library for packages outside of the R directory tree. This can be useful for retaining packages during an upgrade.

Setting a default CRAN mirror site frees you from having to choose one each time you issue an install.packages() command.

The .First() function is an excellent place to load libraries that you use often, as well as source text files containing user-written functions that you apply frequently.

The .Last() function is an excellent place for any cleanup activities, including archiving command histories, program output, and data files.

There are other ways to customize the startup environment, including the use of com- mand-line options and environment variables. See help(Startup) and appendix B in the “Introduction to R” manual (http://cran.r-project.org/doc/manuals/R-intro.pdf) for more details.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]