A Brief Introduction To R, R Studio and Microfiniter: 1.1 The R Programming Language
A Brief Introduction To R, R Studio and Microfiniter: 1.1 The R Programming Language
Developed in the mid-90s at the University of Auckland by Ross Ihaka and Robert
Gentleman, R is closely associated with hardcore computational statistics and it is
often seen as an alternative to statistical packages such as Stata and SAS [1–3].
However, the language has emerged as a quality open-source platform for a variety
of scientific programming tasks [4]. In recent years, R’s appeal has spread beyond
the domain of statistical computations. This is reflected in book series dedicated to
spreading the knowledge of its usage across different fields [5, 6]. In the area of
numerical analysis, the books by Soetaert et al. [7] and Griffiths [8] presented exten-
sive coverages of solution techniques to a multitude of differential equations by well-
tested, high-precision integrators developed in R. Different concepts on numerical
quadrature, numerical differentiation, optimization and curve fitting procedures are
covered in Bloomfield [9]. Schiesser [10] features the use of the language for solu-
tions to differential equations that originate from the field of Biomedical Science and
Engineering. Recently, a demonstration of the capability of the language for finite
element analyses is presented by Mustapha [11].
One of the main challenges faced by many newcomers to R is its large ecosystem
and the question of where to start. The good news is that there are many accessible
texts on the language for beginners. In fact, some of these texts can be accessed for free
via https://bookdown.org/. Besides, a gentle introduction to scientific computing in R
can be found in Jones et al. [12]. For an example-driven approach to learning R, Albert
and Rizzo [13], Kabacoff [14] and Teetor [15] are recommended. In-depth coverage
of R can be sought in Adler [16] and Wickham [17]. Meanwhile, Allerhand [18]
© The Author(s), under exclusive license to Springer Nature Singapore Pte Ltd. 2019 1
K. B. Mustapha, R for Finite Element Analyses of Size-dependent Microscale
Structures, SpringerBriefs in Computational Mechanics,
https://doi.org/10.1007/978-981-13-7014-4_1
2 1 A Brief Introduction to R, R Studio and MicrofiniteR
provides a quick introduction to R in just 89 pages! There are also many excellent
web resources, e.g.:
• https://www.statmethods.net/index.html
• https://www.r-bloggers.com/
• http://www.cookbook-r.com/Basics/.
1.2.1 R Installer
The first step to using the language is to obtain the R installer, which can be down-
loaded from the official home of the R programming language called CRAN1 : https://
cran.r-project.org/. A screenshot of the homepage is shown in Fig. 1.1. As revealed
in this figure, the installer is available for different operating systems. Clicking on
the link that matches your operating system will re-direct you to the relevant website.
As an example, clicking on the Download R for Windows will re-direct you to the
website for Windows installer (shown in Fig. 1.2). Follow the prompts to download,
save and run the files.
1.2.2 RStudio
Once R has been installed, the next step is to install RStudio which is the popular
integrated development environment (IDE) for working with R. RStudio can be
downloaded from: https://www.rstudio.com/products/rstudio/. After the installation,
look for and click on the RStudio icon (shown in Fig. 1.3) and you should see
something similar to Fig. 1.4.
Click on the small symbol pointed to by the arrow in Fig. 1.4 to reveal the full
layout of the RStudio GUI as revealed in Fig. 1.5.
Environment
Editor
Files Pane
Console
Prompt Symbol
The window pane named (Console) in Fig. 1.5 is typically used to display results of
computations or error messages. However, it can also be used as a powerful calculator,
which is a good way to start experimenting with R.
Type each of the expressions below in the console and hit the Enter key after each
(ignore the part of the expression with #).
1.3 Overview of Basic Operations 5
• ˆ or ** (Exponentiation)
• / (Division)
• Examples of built-in functions in R
• round()
• min ()
• max()
• c()
• rep()
• sin()
• exp()
• sqrt()
• Variables and the assignment operators (there are more than two in R, the second
is the most preferred by hardcore R users)
• =
• <-
• Creating simple vectors via
• c() (combine function)
• : (colon operator)
A comprehensive discussion on these and related concepts can be explored further
in one of the many excellent references highlighted in Sect. 1.1. However, the R
documentation is also amazingly rich in content that knowing how to get help from
it is a necessary step to proficiency in R.
There are several ways to get help in R. For a start, the manuals, resources and
documentations on a myriad of functions can be accessed by typing the following in
R console:
> help.start()
Hit the Enter key after typing help.start(), and the file pane (labelled in
Fig. 1.5) will reveal links to several resources as shown in Fig. 1.6. Further, if you
click on the “Home” symbol, which is pointed to using an arrow in Fig. 1.6, the file
pane will also provide you with access to RStudio resources as shown in Fig. 1.7.
To know more about a specific function, a very handy method is to use a question
mark followed by the name of the function. This functionality is especially useful
to find more detail information about all the functions developed for this book. For
1.3 Overview of Basic Operations 7
Fig. 1.6 Accessing manuals and references on R through the RStudio GUI
Fig. 1.7 Accessing manuals and references on RStudio through the GUI
instance, say we wish to know more about the function called plot (apparently used
for plotting), we do this:
?plot
The result will be information about this function as shown in the file pane in
Fig. 1.8. Meanwhile, after populating the console with many commands and you
wish to clear it, use Ctrl+L.
8 1 A Brief Introduction to R, R Studio and MicrofiniteR
The RStudio’s editor is appropriate for dealing with situations that demand that
multiple lines of code be executed at once rather than interactively as we have been
doing. For instance, type the following commands in the editor (as shown in Fig. 1.9).
After typing the codes, highlight all the lines and click the “run” button as depicted
in Fig. 1.10. The output will be printed in the console (see Fig. 1.11). All the codes
used in the analyses presented in this book are first written in the editor and then
run as demonstrated here. It is therefore a straightforward matter to replicate all the
analysis reported in this book by copying the codes to the editor and running them.
a = 40; b = -20; d = 100 #Define three variables
x = a + b + c # Assign the sum of the variables to x
vecy = c(12, 89, 21, 23, 100, 12) # A simple vector with c()
matrixA = matrix(vecy, nrow = 2, byrow = T) # Convert vecy to a matrix
vecy[1,] # Extract first element of vecy
matrixA[1,] # Extract first element of matrixA
1.4 Packages in R
In R, packages are the “fundamental unit of shareable code” [19]. Primarily, a package
is a collection of functions along with the associated documentation, but it may also
include other details beyond these two.
1.4 Packages in R 9
Fig. 1.11 Outputs are produced in the console for the lines of codes run in the editor
Executing the above will connect to the CRAN website to download and install
devtools. After a package has been installed, it can be called by using the
library() function.
1.5 Package—MicrofiniteR
A package named microfiniteR is developed for this book to ease the com-
putational tasks. As illustrated in Fig. 1.12, the package is a collection of relevant
functions developed in Chaps. 2–4. There are three benefits to having the package.
First, readers only need to load the package once, and all the implemented functions
for the finite element models in this book will be loaded in the computer memory.
Second, the description of the functions, the meanings of the input parameters and the
1.5 Package—MicrofiniteR 11
value returns by each of the developed functions can be accessed by using the query
approach (question mark, followed by the function name) demonstrated in sect. 1.3.2.
Third, it is likely that there will be future updates to the functions. In this case, getting
the update will be a straightforward step of downloading the updated package from the
code’s repository on GitHub https://github.com/khameelbm/microfiniteR. Nonethe-
less, for first time usage, the package can be installed using the following command:
devtools::install_github(“khameelbm/microfiniteR”)
1.6 Summary
In this chapter, a brief historical account and a short introduction to R are provided.
Instructions on how to install the required tools for R are discussed. A limited number
of built-in functions in R are examined via the RStudio console and editor. A highlight
of the package developed for this book is presented. A list of references that can be
consulted for further explorations follows this summary.
References
1. R. Ihaka, R. Gentleman, R: a language for data analysis and graphics. J. Comput. Graph. Stat.
5, 299–314 (1996)
2. M.J. Crawley, The R Book (Wiley, Hoboken, 2007)
12 1 A Brief Introduction to R, R Studio and MicrofiniteR
3. N. Matloff, The Art of R Programming: A Tour of Statistical Software Design (No Starch Press,
2011)
4. R.C. Team, R: A Language and Environment for Statistical Computing, ed. Vienna, Austria: R
Foundation for Statistical Computing
5. Use R! Available: https://www.springer.com/series/6991
6. The R Series. Available: https://www.crcpress.com/go/the-r-series
7. K. Soetaert, J. Cash, F. Mazzia, Differential equations, in Solving Differential Equations in R
(Springer, 2012), pp. 1–13
8. G.W. Griffiths, Numerical Analysis Using R (Cambridge University Press, Cambridge, 2016)
9. V.A. Bloomfield, Using R for Numerical Analysis in Science and Engineering (Chapman and
Hall/CRC, 2014)
10. W.E. Schiesser, Differential Equation Analysis in Biomedical Science and Engineering: Ordi-
nary Differential Equation Applications with R (Wiley, Hoboken, 2014)
11. K.B. Mustapha, Finite Element Computations in Mechanics with R: A Problem-Centered Pro-
gramming Approach (CRC Press, Boca Raton, 2018)
12. O. Jones, R. Maillardet, A. Robinson, Introduction to Scientific Programming and Simulation
Using R (CRC Press, Boca Raton, 2009)
13. J. Albert, M. Rizzo, R by Example (Springer, New York, 2012)
14. R. Kabacoff, R in Action: Data Analysis and Graphics with R (Manning, 2015)
15. P. Teetor, R Cookbook (O’Reilly, 2011)
16. J. Adler, R in a Nutshell: A Desktop Quick Reference (O’Reilly Media, 2010)
17. H. Wickham, Advanced R (Taylor & Francis Limited, Abingdon, 2017)
18. M. Allerhand, A Tiny Handbook of R (Springer, Heidelberg, 2011)
19. H. Wickham, R Packages (O’Reilly Media, 2015)