Part 05. R packages: Making R do more

What’s on this page?

  1. Load a new R package
  2. How to select a mirror site
  3. Retrieve and install the package
  4. Load the package functions
  5. Boost Rcmdr with plug-ins
  6. Update R packages after update to new R version
  7. Quiz

What to do

Complete the exercises on this page

  • Add new capabilities to R by adding packages
  • About CRAN mirror sites
  • Retrieve and install an R package from a CRAN mirror
  • To use a package, you must load the package with library()
  • Boost Rcmdr with plug-in packages
  • Update R packages after update to new R version

How to do it

For these exercises, you may work

  • within the Rcmdr script window
  • at the command line and R prompt
  • from a script document and the R GUI app
  • RStudio

Choose one way. The quickest way is to just just work with R Commander.

Let’s begin.

Before you start

A reminder, always set your working directory in R first (see Part 02. Getting started with R and Rcmdr), before starting to do your analyses. Makes life a lot easier. Working with R packages is also detailed in the Appendix of Mike’s Biostatistics Book.

1. Load a new R package

R is useful by itself, but it is the packages created by thousands of users that make it sing. R Commander is a package that provides a graphical user interface to work with many typical statistical functions available in R, and you should have already installed and loaded Rcmdr before starting this set of exercises. If not, please go back and complete 1. Getting started with R and Rcmdr.

If you are using R Commander, then you’ve already downloaded, installed, and started an R package. We will use additional packages during the semester, so it is important that you gain confidence with this procedure.

Here, I want you to install the package “password,” which provides a framework for you to generate passwords. These passwords could be used anywhere you need a decent password, and, because you generate the password locally (on your computer), there is no risk of someone grabbing your password from one of those online websites that claims to help you generate good passwords.

Before you can download a package you need to set the CRAN mirror site. Mirror sites contain the same files as the main site, https://cran.r-project.org/, but are used to reduce the amount of traffic that the main website receives. The full listing of current mirrors for R Project can be found at https://cran.r-project.org/mirrors.html. As a general rule I recommend use of the 0-Cloud site run by the folks at RStudio. (For more about this mirror site, read this post from 2013.)

2. How to select a mirror site

You may have already saved a CRAN mirror site when you downloaded and installed R Commander. In the event that you have not, then proceed to do so now. Set the mirror site via the R console or alternatively, call the function

chooseCRANmirror()

which will bring up a graphical list of mirror sites to choose from.

3. Retrieve and install the package

Once the mirror site is set, use the R console to locate and download the package, password. Alternatively, call the function directly by typing the command at the R prompt

install.packages("password")

Note also that if you know exactly the name of the package (i.e., you know it is “password” and not “passwords”), then you could simply tell R to grab the package from a particular mirror site directly by typing at the R prompt. This

install.packages("password", repos="https://cloud.r-project.org/")

Assuming all goes well, R output will look like

> install.packages("password")
--- Please select a CRAN mirror for use in this session ---
package 'password' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
C:\Users\USERNAME\AppData\Local\Temp\RtmpIh5woj\downloaded_packages

Of course, USERNAME will be replaced by your own username on your computer.

4. Load the package functions

After downloading and installation have completed, activate the password package. As you recall, we load most R packages by calling the library function.

library(password)

The first time you use a package you probably are not familiar with the command structure. The good news is that packages come with help files. So, let’s learn about password from the help file. At the R prompt type

help(password)

and an html file should load in your default browser.

From the help page for password package we learn that the basic command is

password(n = 8, numbers = TRUE, case = TRUE, special = c("?", "!", "&", "%", "$"))

There’s more about the function in the help page, but it seems pretty straight forward. We change the password length by changing n. The default is set to 8. If we want a password without numbers, then we would set numbers=FALSE. If we want all lower case letters, then we set case = FALSE. And finally, if we want to include special characters, then we add to, or subtract from, the list of characters in the “combined” special object. Only five of the possible 33 special characters are included in the default list of special (see Wikipedia for a full list).

Here’s a 12 letter password, with numbers, all lowercase, without special characters.

password(n = 12, numbers = TRUE, case = FALSE, special = c())

And the output is

[1] "3hsyb1av9ro4"

Try on your own. Try different combinations of password length, with and without numbers, upper and lower cases, special characters, both default and extended list of special characters. Which matters more, increasing the password type (i.e., the number of characters) or the length of the password?

Optional. Test the “strength” of your password with one of the many online services, e.g., https://lastpass.com/howsecure.php gives a ranking; https://random-ize.com/how-long-to-hack-pass/ does what is says. The site claims our password listed in output would take “53 years, 7 months” if a brute-force hack was attempted.

Update: Try against zxcvbn, an open source project used to check passwords and supported by DropBox. A demo version is available at https://www.bennish.net/password-strength-checker)

Try on your own. Calculate Shannon’s information entropy in bits (see Wikipedia for the logic). Let cardinality equal the number of elements in the space, e.g., all of the characters possible, and length equal the number of characters of the password. For our example password

#This will make a new password
myPass <- password(n = 12, numbers = TRUE, case = FALSE, special = c())
length <- nchar(myPass)
cardinality <- 36 #26 lower case + ten numbers from zero to nine + + no upper case + no special characters
entropy <- n*log(cardinality,2) #base 2, because "bits"
entropy #password strength

And the output from R is

[1] 62.0391

The larger the entropy, the stronger our password against a “brute-force-attack.” Our password would take 262.0391, or 4.74e+18 (4.74 quintillion!) attempts of a random, brute-force hacker attack to discover the password.

However, our password also is hard to remember, so in that sense, it’s not a good password. Consider the point of view expressed by xkcd (Figure 1)

https://xkcd.com/936/ (Links to an external site.) "Password strength"

Figure 1. https://xkcd.com/936/ “Password strength”

5. Boost Rcmdr with plug-ins

R Commander includes many packages, but can be further extended with a number of useful plugins. For example, the plugin KMggplot2 adds links to the popular ggplot2 package.

  1. If you are using Rcmdr, then close R Commander before proceeding.
  2. Download and install the package RcmdrPlugin.KMggplot2 as you did for the password package and other packages.
  3. Start Rcmdr.
    • Note that if you closed Rcmdr, it is still running in the background. To bring it up again, type Commander() at the R prompt instead of library(Rcmdr).
  4. When Rcmdr is back up and running, you’ll see the plug-in as an extra menu option.

In addition to RcmdrPlugin.KMggplot2, additional R Commander plugins you may use in this course include

RcmdrPlugin:doBy
RcmdrPlugin:EBM
RcmdrPlugin:survival
RcmdrPlugin:temis

I’m not asking you to install these now. You should, however, learn what kind of tasks these sets of functions will allow you to do. By learning what others have done, it may inspire you to think about the kinds of projects you can do — the software is just waiting to be used!

Package Description
RcmdrPlugin.aRnova R Commander Plug-in for Repeated-Measures ANOVA
RcmdrPlugin.BiclustGUI 'Rcmdr' Plug-in GUI for Biclustering
RcmdrPlugin.BWS1 R Commander Plug-in for Case 1 (Object Case) Best-Worst Scaling
RcmdrPlugin.BWS2 R Commander Plug-in for Case 2 Best-Worst Scaling
RcmdrPlugin.coin Rcmdr Coin Plug-in
RcmdrPlugin.cpd R Commander Plug-in for Complex Pearson Distributions
RcmdrPlugin.DCCV R Commander Plug-in for Dichotomous Choice Contingent Valuation
RcmdrPlugin.DCE R Commander Plug-in for Discrete Choice Experiments
RcmdrPlugin.depthTools R Commander Depth Tools Plug-in
RcmdrPlugin.DoE R Commander Plugin for (Industrial) Design of Experiments
RcmdrPlugin.EACSPIR Plugin de R-Commander para el Manual 'EACSPIR'
RcmdrPlugin.EBM Rcmdr Evidence Based Medicine Plug-in Package
RcmdrPlugin.EcoVirtual Rcmdr EcoVirtual Plugin
RcmdrPlugin.Export Export R Output to LaTeX or HTML
RcmdrPlugin.EZR R Commander Plug-in for the EZR (Easy R) Package
RcmdrPlugin.FactoMineR Graphical User Interface for FactoMineR
RcmdrPlugin.GWRM R Commander Plug-in for Fitting Generalized Waring Regression Models
RcmdrPlugin.HH Rcmdr Support for the HH Package
RcmdrPlugin.KMggplot2 R Commander Plug-in for Data Visualization with 'ggplot2'
RcmdrPlugin.MA Graphical User Interface for Conducting Meta-Analyses in R
RcmdrPlugin.MPAStats R Commander Plug-in for MPA Statistics
RcmdrPlugin.NMBU R Commander Plug-in for University Level Applied Statistics
RcmdrPlugin.orloca A GUI for Planar Location Problems
RcmdrPlugin.PcaRobust R Commander Plug-in for Robust Principal Component Analysis
RcmdrPlugin.RiskDemo R Commander Plug-in for Risk Demonstration
RcmdrPlugin.RMTCJags R MTC Jags 'Rcmdr' Plugin
RcmdrPlugin.ROC Rcmdr Receiver Operator Characteristic Plug-in Package
RcmdrPlugin.SCDA Rcmdr Plugin for Designing and Analyzing Single-Case Experiments
RcmdrPlugin.sosEfficiently search the R help pages
RcmdrPlugin.survival R Commander Plug-in for the 'survival' Package
RcmdrPlugin.TeachingDemos Rcmdr Teaching Demos Plug-in
RcmdrPlugin.TeachStat R Commander Plugin for Teaching Statistical Methods
RcmdrPlugin.temis Graphical Integrated Text Mining Solution
RcmdrPlugin.UCA UCA Rcmdr Plug-in
RcmdrPlugin.WorldFlora R Commander Plug-in for the 'WorldFlora' Package

6. Update R packages after update to new R version

R-project publishes major releases to R about once per year (R Developer page), with patches released as needed more frequently. Updating to new version of R does not update downloaded R packages; this needs to be accomplished by the user. There are several ways to update R packages; see R Packages in Appendix of Mike’s Biostatistics Book for more information.

7. Page quiz


R packages

Seven questions from this page