R functions and packages

Organising software components

Software organises sequences of instructions in components that perform a specific task. For example, some practice in Become familiar with R Studio and basic concepts of R, a basic function was created to prevent coding again the same line of codes. Functions can be grouped together and packaged as some units of code, that can be shared and used again.

Like most programming languages, R provides some pre-written set of functions, that are kept in libraries. 

Let's starts with functions in spreadsheets

Spreadsheets provides some functions that uses some values as input and returns a value. Each of these functions completes some specific computations that should be closed to their unique and meaningful names.  Often some new functions can be added with some modules. 

Additional reading list

Functions in R

R functions have a unique name and uses some arguments to pass some values into a function. Most of the time, functions return a value. Sometimes, a function complete some computations, but do not return a value. The latter may set some environmental set up, for example. The sample function has the following signature; a name and some arguments. Some arguments have been set by defaults to some values; those have the symbol "=" next to their name.  So, unless, the users changes their values,  replace and prob arguments will be set to FALSE and NULL. The help of R provide this information. This function was used earlier (see Become familiar with R Studio and basic concepts of R).

R is made up of thousands of functions that you use to manipulate and analyse some data.  These functions are the equivalent to mathematical and statistical functions made available in a spreadsheet. For example, both spreadsheet software provide a standard deviation and an arithmetic average for example. Some R functions can also integrate with some proprietary software, such as (ArcGIS, Stata).  Some functions emulate the behaviour of the Sequential Query Language and can extract information stored in many standard formats; i.e., .txt, .csv, .sav.

Packages in R

All the built-in functions are provided in the Base package; a complete list is provided in the additional reading section.  A package groups some functions together; the latter share a common purpose or aim. In other programming languages, this concept of grouping sub-routines is can be referred libraries or modules. It worth noting, in R the library() makes available the content of a package into a script. However, the keyword library should not be used interchangeably for package. 

Some packages can be installed from an online repository, to add some additional functionality and share with other users. DataShield is one example. It is worth noting many packages and functions can be found online. It was checking whether the packages are compatible  with a certain version of R before installation. The development of packages require a lot of work in testing and documenting in details. Sharing functions with some scripts can store some user-written functions and share with others users more easily. The latter requires less work than a package.

By default, packages are installed in lib folder of the R framework. However, some advanced users may consider to install packages in another folder. However, more settings may need to changed, each time some scripts are executed.  

Practice - using some functions from some scripts

The function is not working...

  1. In the console, type  large.numbers <- sample.large.number()

The function can not be found and called

  1. Download the following file: some-functionsv2.R
  2. Open it in R studio. You should see two functions named sample.large.numbers and sample.negative.numbers
  3. In the help, search for the source function. Read description of the function and the list of arguments. 
  4. Go back to the script and click on the source button. In the console, a call the source function is made. In the environment, a list of functions should have appeared. Can you see the two new functions?
  5. Call the the two functions. Try
    1. large.numbers <- sample.large.numbers() 
    2. negative.numbers <- sample.negative.numbers()
    3. large.numbers <- sample.large.numbers(n = 10) 
    4. negative.numbers <- sample.negative.numbers(n= 10, min.number = 100) (You should see an error message !)
    5. large.numbers <-sample.large.numbers(max.number = -10) (You should see an error message !)

Some of these calls displayed some error messages and some others have returned some values, in a vector. However, the function was found in the Environment and behaved as they were coded. 

Practice - Installing and using packages

The script is not working

  1. Download the following file: packages.R
  2. Open the script in R Studio. 
  3. Execute the script by clicking on the source button. The script should not be working, as it cannot find the compoisson package.

Installation of a package

  1. In the help, find the function install.packages() and remove.packages(). Read each function description and information. 
  2. Make sure a connection to the Internet is live.
  3. Installation of the compoisson package: in the console type install.packages("compoisson",dependencies = TRUE)

Load and attach add-on packages

  1. In the help, find the function search for the library function. Read the help file.
  2. Make available all the compoisson package using the library function. Try library(compoisson)
  3. Execute packages.R again. Click on the source button. The script should be running without any problems

Remove the package from R

  1. Type remove.packages("compoisson")
  2. Execute the script by clicking on the source button. The script should not be working, as it cannot find the compoisson package.

DataSHIELD Wiki by DataSHIELD is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Based on a work at http://www.datashield.ac.uk/wiki