#### Transcript Lecture 7

Introduction to R Functions, Packages Andrew Jaffe 10/18/10 Overview Functions Packages Questions Functions Everything you use in R is a built-in function You can download new functions (packages – next section) You can add your own to a script Functions Built in functions (note the lack of parenthesis – this is also why you don’t want to name variables as functions): > rep function (x, ...) .Primitive("rep") >c function (..., recursive = FALSE) .Primitive("c") Functions Syntax: functionName <- function(inputs) { body of the function which returns something} You must run/submit the function before you can use it Note that variable naming within a function is protected, and not altered by whatever your script is doing Functions Input # tests if a number if positive pos = function(x) { if(x >= 0) out = 1 if(x < 0) out = 0 return(out) } Output Functions Run the function first > pos = function(x) { + if(x >= 0) out = 1 + if(x < 0) out = 0 + return(out) + } > pos(5) [1] 1 > pos(-5) [1] 0 Functions The value that the function returns can be stored as a new variable > y = pos(4) > y [1] 1 Functions > # protected – x is the # input to pos > x = 3 > pos(6) [1] 1 > x [1] 3 Functions Using return() explicitly tells the function what to return – safe Otherwise, the function will return the last value that it works with or assigns This can be an issue if you use a lot of logical statements Functions Adding a ‘verbose’ logical variable gives the user some feedback > pos = function(x,verbose=TRUE) { + if(x >= 0) out = 1 + if(x < 0) out = 0 + + if(verbose) cat("finished running","\n") + return(out) + } > y = pos(5) finished running > y [1] 1 Functions You can change the input into functions when you run them You can name each input, or just put their values in order > y = pos(5, verbose = FALSE) > y = pos(5, FALSE) # same thing > y [1] 1 Functions Here, we set verbose to be TRUE by default, ie that that function says what its doing Using verbose statements throughout your functions makes it easier to use (especially if other people are going to use your function) Functions Note: really long ‘for’ loops also benefit from using verbose statements %%: mod, the remainder of the first number divided by the second Print a period every 10,000 iterations for(i in 1:1e5) { if(i %% 10000 == 0) cat(".") # do other stuff } Functions Why do functions? Provides modularity to your code You can solve small problems using functions Keeps your actual analysis script cleaner You can distribute your code script to other people Functions For example, download the lecture notes, and type source(“lecture_notes.R”) The ‘pos’ function should be added into your working directory Another function, ‘mypar’ was added, but requires a …package! Functions Make a function that takes two numbers as inputs, and returns their sum Make a function that takes three inputs as inputs and returns their product Functions summ <- function(a,b) { out = a+b return(out) } prodd <- function(a,b,c) { out = a*b*c return(out) } Overview Functions Packages Questions Packages R can access and install a huge library of packages The full list is here: http://cran.rproject.org/web/packages/ You only need to install a package once, then you can use it whenever you want Packages ?install.packages Let’s try downloading “RColorBrewer” install.packages(“RColorBrewer”) A list of repository directories should popup select USA (basically any of them will work) I think the Maryland one is Hopkins… Packages Some installation popups occurs and the package should be installed on your computer Then, library(package_name) library(RColorBrewer) – quotes are optional Packages ?RColorBrewer: “Creates nice looking color palettes especially for thematic maps” Top left corner: “RColorBrewer {RColorBrewer}“ Top left in mean: “mean {base}” The thing in {} is the package Packages Now, resource the lecture 7 notes so that the mypar() function gets read in correctly This function will alter the plot parameters to make the color palette more divergent, changes the margin sizes, and can allow you to plot multi-panel plots Packages mypar() # default is 1 x 1 plot space mypar(2,2) # 2 rows, 2 columns > data(cars) > mypar() > plot(cars, type = "b", col = 5) Packages I usually google “R + [something I’m trying to do]” and sometimes links can direct you to existing packages The full list is on the CRAN website Some Epi ones are “survival” – survival analysis, “lme4” – longitudinal data analysis, “Epi” – some epi functions Packages Try installing those packages now Remember, to use a package, you must invoke the library(package) command Packages library(survival) ?survival ??survival – “fuzzy search” ?Surv Overview Functions Packages Questions Questions? Any burning R questions? Open floor…