Presentation on theme: "S Programming in R Bill Venables CSIRO Mathematics and Information Sciences Auckland, 7 July 2006."— Presentation transcript:
S Programming in R Bill Venables CSIRO Mathematics and Information Sciences Auckland, 7 July 2006
Session 1 Introduction Background, basic features and
7 July 2006S Programming in R3 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, …
7 July 2006S Programming in R4 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, –…
7 July 2006S Programming in R5 How are objects located? In one sense, on the search path > search()  ".GlobalEnv" ".R_Store"  "package:fortunes" "package:rpart"  "package:MASS" "package:ASOR"  "package:methods" "package:stats"  "package:graphics" "package:grDevices"  "package:utils" "package:datasets"  ".R_Utils" "package:svIDE"  "package:tcltk" "Autoloads"  "package:base"
7 July 2006S Programming in R6 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......
7 July 2006S Programming in R7 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
7 July 2006S Programming in R8 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.
7 July 2006S Programming in R9 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 ) –dataset <- transform(dataset, v1 = x1, v2 = x2,...) –dataset <- subset(dateset, condition, select =...) The ' data ' argument in fitting functions, lattice, &c
7 July 2006S Programming in R10 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
7 July 2006S Programming in R12 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.
7 July 2006S Programming in R13 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 ")