130 likes | 266 Views
S Programming in R. Bill Venables CSIRO Mathematics and Information Sciences Auckland, 7 July 2006. Session 1. Introduction Background, basic features and. The S Language. “S is a language for manipulating objects” A function language An object oriented language
E N D
S Programming in R Bill Venables CSIRO Mathematics and Information Sciences Auckland, 7 July 2006
Session 1 Introduction Background, basic features and
The S Language • “S is a language for manipulating objects” • A function language • An object oriented language • Objects are the vehicle for information in S • Objects may be • constructed • interrogated • displayed • compared • modified, … S Programming in R
Objects and classes • “In S everything is an object and every object has a class”. • Objects: • data (vectors, matrices, character vectors, …) • objects constructed from data (fitted models, graphics objects, &c) • functions and other language elements • connections (file, text, …) • classes themselves, meta data, • … S Programming in R
How are objects located? • In one sense, on the search path > search() [1] ".GlobalEnv" ".R_Store" [3] "package:fortunes" "package:rpart" [5] "package:MASS" "package:ASOR" [7] "package:methods" "package:stats" [9] "package:graphics" "package:grDevices" [11] "package:utils" "package:datasets" [13] ".R_Utils" "package:svIDE" [15] "package:tcltk" "Autoloads" [17] "package:base" S Programming in R
What happens at startup? > ?.Rprofile Startup package:base R Documentation Initialization at Start of an R Session Description: In R, the startup mechanism is as follows. ..... S Programming in R
More on startup • Rprofile.site • File in R_HOME/etc • Invoked for all R sessions on the current machine • Probably best left alone • .Rprofile • File in R_USER or current working directory • Invoked for all R sessions for this user (or with this working directory) • Very useful for setting options • .Renviron, Rconsole, Rdevga S Programming in R
Getting Help in General • Help on a specific object or entity: • ?object ## and variants • help(object) • help.start() • RSiteSearch("keyword or phrase") • Help on packages is similarly available, • For a few packages • vignette("package_name") Gives an overview of what's available. S Programming in R
Adding to the search path • library(MASS) • require(MASS) • attach(list_object_or_data_frame) • detach(position_or_name) • Locally adding to the path • value <- with(object, <expression>) • dataset <- transform(dataset, v1 = x1, v2 = x2, ...) • dataset <- subset(dateset, condition, select = ...) • The 'data' argument in fitting functions, lattice, &c S Programming in R
More convenience features The (unofficial) ASOR package • Based on a CRAN package, 'gdata', by David Brahm, but not yet an official package – yet, • Allows you to 'offload' objects from memory, while remaining visible on the search path, • If objects are needed, they are placed back in memory • Attach() – makes saved objects visible • Save(...) – removes objects form .GlobalEnv • Objects() – lists offloaded objects attaching if necessary • Remove(...) – permanently removes saved objects. • Uses the idea of a "promise" to load the object, if necessary. • Not restricted to large objects S Programming in R
Some extracts from an .Rprofile options(show.signif.stars = FALSE, length = 999999) options(CRAN = "http://cran.ms.unimelb.edu.au") options(repos = c(CRAN = getOption("CRAN"), CRANextra = "http://www.stats.ox.ac.uk/pub/RWin")) options(save.defaults = list(compress = TRUE, ascii = FALSE, safe = TRUE)) autoload("xyplot", "lattice") autoload("odbcConnectExcel", "RODBC") autoload("stepAIC", "MASS") S Programming in R
Front ends • Tinn-R • Windows only • Now requires R to run in SDI mode • Very useful front end for working with R and keeping a working script • Emacs/ESS • Offers similar functionality • Available for Windows/Linux/Unix/Mac? • Rcmdr and SciViews both offer front ends with strengths and weaknesses. S Programming in R
Some input and output • scan() • read.table(), read.csv(), read.delim(), ... • write.table(), write.csv(), ... • sink(), print(), cat() – standard • source() • data.dump(), data.restore() – hardly needed • save(), save.image(), load(), ... • Connections – to external files or character string vectors • RODBC package allows external connections to data bases • con <- odbcConnectExcel("microsoft.xls") S Programming in R