Setting Up and Customizing R

March 7, 2012
By

[This article was first published on Jason Bryer » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

For the longest time I resisted customizing R for my particular environment. My philosophy has been that each R script for each separate analysis I do should be self contained such that I can rerun the script from top to bottom on any machine and get the same results. This being said, I have now encountered a situation where there are multiple researchers using multiple R instances on multiple platforms. Briefly, our setup involves a shared network drive that is accessed using Rstudio on Windows as well as Rstudio Server running on Linux. I have created a setup R script file that sets the library location to the shared drive (using the oDrive variable also set globally), install the collection of R packages we regularly use, or update them, and setup the .Rprofile script appropriately for the platform the script is running on. I have also included the Rprofile script we are using.

Please provide comments about how you customize R for your needs.

My setup R script.

#List of most used R packages that we wish to install.
libraries = c('cacheSweave', 'Deducer', 'devtools', 'doBy', 'foreign', 'gdata',
'ggplot2', 'Hmisc', 'JGR', 'maps', 'mapdata', 'mapproj',
'maptools', 'proto', 'psych', 'R2wd', 'RCurl', 'reshape',
'RODBC', 'roxygen2', 'seqinr', 'sm', 'sp', 'sqldf', 'survey',
'WriteXLS', 'XML', 'xtable')

#We will install packages from the main CRAN site
repos = 'http://cran.r-project.org'
#Site provides some prebuilt binaries for Windows
repos.win = 'http://www.stats.ox.ac.uk/pub/RWin'
#URL for the R-Forge repository.
repos.rforge = 'http://r-forge.r-project.org'

#Type of download for install.packages. Default to source
.type = 'source'

if(Sys.info()['sysname'] == 'Windows') {
oDrive = 'o:/'
memory.limit(size=4095) #Set to 4GB (for 64-bit Windows, this can be much larger)
#Set the R_LIBS environment varaible. This assumes the Setx program is installed
#(this is included with the Service Pack 2 Support Tools)
system(paste('Setx R_LIBS ', oDrive, 'R/library/Windows', sep=''))
#We will manually add to libPaths since the system variable will not take
#effect until R is restarted.
.libPaths(paste(oDrive, 'R/library/Windows', sep=''))
repos = c(repos, repos.win) #Add the Windows repos for use below
# Set system environment varaible R_PROFILE
system(paste('Setx R_PROFILE ', oDrive, 'R/Rprofile', sep=''))
.type = 'win.binary'
} else if(Sys.info()['sysname'] == 'Linux') {
oDrive = '/n01/OutcomesAssessment/'
file.copy(paste(oDrive, 'R/Rprofile', sep=''), '~/.Rprofile', overwrite=TRUE)
.libPaths(paste(oDrive, 'R/library/Linux', sep=''))
} else {
stop('Unsupported operating system!')
}

#Only load packages that are not already installed.
for(l in libraries) {
i = require(l, character.only=TRUE, lib.loc=.libPaths()[1], quietly=TRUE)
if(!i) {
install.packages(l, repos=repos, dep=TRUE, lib=.libPaths()[1], type=.type,
destdir=paste(oDrive, 'R/library/download', sep=''))
} else {
detach(paste('package:', l, sep=''), character.only=TRUE)
}
}

#Make sure we have the latest versions of the packages
update.packages(repos=repos, ask=FALSE, lib.loc=.libPaths()[1],
destdir=paste(oDrive, 'R/library/download', sep=''))

#Load some packages from Github. We won't bother checking if they are already
#installed since these tend to change more often.
library(devtools)
install_github('irutils', 'jbryer', lib.loc=.libPaths()[1])
install_github('ruca', 'jbryer', lib.loc=.libPaths()[1])
install_github('ipeds', 'jbryer', lib.loc=.libPaths()[1])
install_github('makeR', 'jbryer', lib.loc=.libPaths()[1])
install_github('qualtrics', 'jbryer', lib.loc=.libPaths()[1])

#Install ecir package
install(paste(oDrive, 'R/ecir', sep=''), lib.loc=.libPaths()[1])

message("Installation of R packages complete. Please restart R now...")
view raw
setup.r
This Gist brought to you by GitHub.

My custom .Rprofile file.

# .Rprofile -- commands in this file will be executed at the beginning of
# each R session. On Windows, the R_PROFILE environment variable must have value
# with the full path to this file. On Linux (or other Unix like systems) this file
# must be in the user's home directory.

# Set the default repository to the main CRAN site
options(repos=c(CRAN='http://cran.r-project.org'))

# Set the oDrive varaible and library path
if(Sys.info()['sysname'] == 'Windows') {
oDrive = 'o:/'
.libPaths(paste(oDrive, 'R/library/Windows', sep=''))
require(utils, quietly=TRUE)
print("Setting memory limit for R to 4GB...")
memory.limit(size=4095) #Set to 4GB (for 64-bit Windows, this can be much larger)
message(paste('R_LIBS: ', Sys.getenv('R_LIBS'), sep=''))
message(paste('R_PROFILE: ', Sys.getenv('R_PROFILE'), sep=''))
} else if(Sys.info()['sysname'] == 'Linux') {
oDrive = '/n01/OutcomesAssessment/'
.libPaths(paste(oDrive, 'R/library/Linux', sep=''))
} else if(Sys.info()['sysname'] == 'Darwin') {
oDrive = NULL
}

# Customize the default look and feel of ggplot2
if(require(ggplot2, quietly=TRUE)) {
theme_update(panel.background=theme_blank(),
panel.grid.major=theme_blank(),
panel.border=theme_blank())
}

# On Linux we will alter the default behavior of the makeR package.
if(Sys.info()['sysname'] == 'Linux') {
if(require(makeR, quietly=TRUE)) {
setAutoOpen(FALSE)
setDefaultBuilder(builder.rnw.native)
}
}

# Change the location of the SQL repository
if(require(irutils, quietly=TRUE)) {
setSQLRepos(paste(oDrive, 'R/ecir/data', sep=''))
}

view raw
Rprofile.R
This Gist brought to you by GitHub.

To leave a comment for the author, please follow the link and comment on their blog: Jason Bryer » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags:

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)