Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Organising software components

Software organises sequences of instructions in components that perform a specific task. For 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

...

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 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 The latter requires less work than a package.

...

  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 In the console, a call the source function is made. In the environment,  a 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 !)

...