The Big R-Book. Philippe J. S. De Brouwer
that this argument will get the default value, unless another value is supplied – in other words: if nothing is supplied then the default is used.
It is quite handy to have the possibility to assign a default value to a function. It allows to save a lot of typing work and makes code more readable, but it allows also to add a variable to an existing function and make it compatible with all previous code where that argument was not defined.
paste()
Example
The function paste()
collates the arguments provided and returns one string that is a concatenation of all strings supplied, separated by a separator. This separator is supplied in the function via the argument sep
. What is the default separator used in paste()
?
Creating functions with a default value
Example: default value for function
c_surface <- function(radius = 2) { radius ∧ 2 * pi } c_surface(1) ## [1] 3.141593 c_surface() ## [1] 12.56637
4.7 Packages
One of the most important advantages of R is that it allows you to stand on the shoulders of giants. It allows you to load a library of additional functionality, so that you do not waste time writing and debugging something that has been solved before. This allows you to focus on your research and analysis.
Unlike environments like spreadsheets, R is more like a programming language that is extremely flexible, modular, and customizable.
4.7.1 Discovering Packages in R
Additional functions come in “packages.” To use them one needs to install the package first with the function install.packages()
; this will connect to a server, download the functions and prepare them for use. Once installed on our computer, they can be loaded in the active environment with the function library()
or require()
install.packages()
library()
require()
Example: loading the package DiagrammeR
# Download the package (only once): install.packages(‘DiagrammeR’) # Load it before we can use it (once per session): library(DiagrammeR)
The number of packages availabe increases fast. At the time of writing there are about 15 thousand packages available (see the next “Further information” section). We can of course not explain each package in just one book. Below we provide a small selection as illustration and in the rest of the book we will use a selection of 60 packages (which contain a few hundred upstream packages). The choice of packages is rather opinionated and personal. R is free software and there are always many ways to achieve the same result.
More information about the packages as well as the packages themselves can be found on the CRAN server https://cran.r-project.org/web/packages
.
Useful functions for packages
Below we show some useful functions - note that the output is suppressed.
# See the path where libraries are stored: .libPaths() # See the list of installed packages: library() # See the list of currently loaded packages: search()
R provides also functionality to get a list of all packages – there is no need to use a web-crawling or scraper interface.
# available.packages() gets a list: pkgs <- available.packages(filters = “duplicates”) colnames(pkgs) ## [1] “Package” “Version” ## [3] “Priority” “Depends” ## [5] “Imports” “LinkingTo” ## [7] “Suggests” “Enhances” ## [9] “License” “License_is_FOSS” ## [11] “License_restricts_use” “OS_type” ## [13] “Archs” “MD5sum” ## [15] “NeedsCompilation” “File” ## [17] “Repository” # We don't need all, just keep the name: pkgs <- pkgs[,‘Package’] # Show the results: print(paste(‘Today, there are’, length(pkgs), ‘packages for R.’)) ## [1] “Today, there are 15477 packages for R.” available.packages()
We can use the function library()
to get a list of all packages that are installed on our machine.
# Get the list (only names): my_pkgs <- library()$results[,1] ## Warning in library(): library ‘/usr/local/lib/R/site-library’ contains no packages # Show the results: print(paste(‘I have’, length(my_pkgs), ‘packages for R.’)) ## [1] “I have 282 packages for R.”
Alternatively, you can use the function installed.packages()
library()
installed.packages()
4.7.2 Managing Packages in R
In the previous section, we learned how to install a package, and got a flavour of the available packages. It is also a good idea to keep the repository of packages stable during a big project, but from time to time update packages aswell as R.Not only there are bug fixes, but also new features.
# See all installed packages: installed.packages()
While in the rest of the book, most code is “active” in this sense that the output that appears under a line or the plot that appears close to it are generated while the book was compiled, the code in this book is “cold”: the code is not executed. The reason is that the commands fromthis sectionwould produce long and irrelevant output. The listswould be long, because the author's computer has many packages installed, but also little relevant to you, because you have certainly a different configuration. Other commandswould even change packages as a side effect of compiling this book.
A first step in managing packages is knowing which packages can be updated.
# List all out-dated packages: old.packages()
Once we know which packages can be updated, we can execute this update:
# Update all available packages: update.packages()
If you are very certain that you want to update all packages at once, use the ask
argument:
# Update all packages in batch mode: update.packages(ask