3.4 Commands
20200501 A command is a function (Section 3.5) for which we are generally more interested in the actions or side-effects that are performed by the command rather than the value returned by the command. For example, the base::library() command is run to attach a package from the library (the action) and consequently modifies the search path that R uses to find commands (the side-effect).
# Load package from the local library into the R session.
library(rattle)
A command is also a function and does in fact return a value. In the above example we do not see any value printed. Functions in R can be implemented to return values invisibly. This is the case for base::library(). We can ask R to print the value when the returned result is invisible using the function base::print().
# Demonstrate that library() returns a value invisibly.
print(library(rattle))
## [1] "readr" "rattle" "bitops" "tibble" "tidyr" "stringr"
## [7] "ggplot2" "dplyr" "magrittr" "stats" "graphics" "grDevices"
## [13] "utils" "datasets" "methods" "base"
We see that the value returned by base::library() is a
vector of character strings. Each character string names a package
that has been attached during this session of R. Notice that the
vector begins with item [1]
and then item [5]
continues
on the second line, and so on. We can save the resulting vector into a
variable (Section 3.18) and then index the variable to
identify packages at specific locations within the vector.
# Load a package and save the value returned.
<- library(rattle)
l
# Review one of the returned values.
7] l[
## [1] "ggplot2"
Your donation will support ongoing development and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984.
Copyright © 1995-2021 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0.