Presentation is loading. Please wait.

Presentation is loading. Please wait.

S Programming in R Bill Venables CSIRO Mathematics and Information Sciences Auckland, 7 July 2006.

Similar presentations


Presentation on theme: "S Programming in R Bill Venables CSIRO Mathematics and Information Sciences Auckland, 7 July 2006."— Presentation transcript:

1 S Programming in R Bill Venables CSIRO Mathematics and Information Sciences Auckland, 7 July 2006

2 Session 1 Introduction Background, basic features and

3 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, …

4 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, –…

5 7 July 2006S Programming in R5 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"

6 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 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

8 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.

9 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

10 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

11 7 July 2006S Programming in R11 Some extracts from an.Rprofile options(show.signif.stars = FALSE, length = ) 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")

12 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.

13 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 ")


Download ppt "S Programming in R Bill Venables CSIRO Mathematics and Information Sciences Auckland, 7 July 2006."

Similar presentations


Ads by Google