1 USDA Computer Setup

R and RStudio are available on the Software Center for download and CEE-approved once a year. Be sure to install or update to the latest versions. The CCE versions maybe one to three releases behind the latest version. Having an outdated version of R rarely creates a problem, although warnings will appear. NASIS by comparison is pre-loaded on NRCS Soil Scientists computers and updated automatically. Many examples use NASIS, but it is not required. If you lack NASIS ask your mentor for a temporary URL to load pedons from NASIS.

2 Create Workspace

Make a local folder C:\workspace2 to use as a working directory for this course. Use all lower case letters please.

3 Configure RStudio

Open RStudio, and edit the “Global Options” (Main menu: Tools → Global Options).

3.1 Essentials

These options are important for pleasant, reproducible and efficient use of the RStudio environment:

  1. Change the default working directory to C:\workspace2 (R General Tab)

  2. Uncheck “Restore .Rdata into workspace at startup” (R General Tab) VERY IMPORTANT

Figure 1: Example of RStudio General settings.

RStudio detects the available R installations on your computer.

Individual versions are certified for the Software Center as they become available, and sometimes there is a more recent version available for download. It is worth taking the time before installing packages to get the latest version of R available to you. This is to minimize compatibility issues which arise over time.

3.2 Personalization

Figure 2: Example of RStudio Code/Editing settings.

  • Optional: Check “Soft-wrap R source files” (Code/Editing Tab)

  • Optional: Show help tooltips, control auto-completion and diagnostics (Code/Completion and Diagnostics Tabs)

  • Optional: Update code font size, colors and theme (Appearance)

  • Optional: Use RStudio Projects (top-right corner) to manage working directories

4 Install .RProfile

The code you run next will establish a safe location for your R package library. Your package library should ideally be on a local disk with about 1 - 2 GB of free space. We want to prevent installs to ~ (your $HOME directory) which is typically on a network share (such as H:/), not a local disk.

Copy the following code in the box below and paste into the R console panel after the command prompt (>) and press enter.

Hint: the R console is the lower left or left window in RStudio with a tab labeled “Console”.


An updated set of library paths will be printed. Close and re-open RStudio, or Restart R (Main menu: Session → Restart R; or Ctrl+Shift+F10), before continuing to the next steps.

Figure 3: Example of RStudio Console - the R library paths are on a local drive “C:/”

When your .Rprofile is set up correctly you will see output in a new R console/session confirming your library paths are:

  1. on a local drive (such as C:/)

  2. specific to the version number of R installed (such as 4.0)

5 Install Required Packages

Packages can be installed by name from the Comprehensive R Archive Network (CRAN) using the base R function install.packages There are a lot of packages out there – many more than you will download here, and many of which are useful for Soil Survey work.

The first time you install packages, R will ask you if you want to create a local repository in your User Documents folder. Click Yes.

For example, to download and install the remotes package from CRAN:


To install the R packages used in this class copy all of the code from the box below and paste into the R console window. Paste after the command prompt (>) and press enter.

Downloading and configuring the packages will take a while if you are installing or upgrading all of the packages in the list below.

# ipkCRAN: a helper fuction for installing required packages from CRAN
# - p: vector of package names
# - up: logical - upgrade installed packages? Default: TRUE
ipkCRAN <- function(p, up = TRUE){
  message('installing packages from CRAN...')
  if (up) {
    # default is to re-install everything
    install.packages(p, dependencies = TRUE)
  } else {
    # but if up != TRUE install just what is needed
    new.pkg <- p[! (p %in% installed.packages()[, "Package"])]
    if (length(new.pkg) > 0) {
      install.packages(new.pkg, dependencies = TRUE)
  # finally, check and see if any failed
  missing.pkg <- p[! (p %in% installed.packages()[, "Package"])]
  if (length(missing.pkg) > 0) { 
     warning(sprintf('\033[31mOne or more packages failed to install!\033[39m\n%s',
             sprintf("Restart R then try `\033[35minstall.packages(c(%s))\033[39m`",
             paste0(sprintf('"%s"', missing.pkg), collapse = ","))), call. = FALSE)

## character vector of packages
packages <- c(
    # soil
    "aqp", "soilDB", "sharpshootR", "soiltexture",
    # gis
    "rgdal", "raster", "sp", "sf", "terra", "gdalUtils", 
    "rgrass7", "RSAGA", "exactextractr", "fasterize",
    # data management
    "dplyr", "tidyr", "devtools", "roxygen2", "Hmisc", "circular", "DT", "remotes",
    # databases
    "DBI", "odbc", "RSQLite", "RODBC",
    # graphics
    "ggplot2", "latticeExtra", "maps", "spData", "tmap", 
    "mapview", "plotrix", "rpart.plot", "visreg",
    # modeling
    "car", "rms", "randomForest", "ranger", "party", "caret", "vegan", "ape", "shape",
    # sampling

## install vector of CRAN packages "safely"
##  up = TRUE to download all packages 
##  up = FALSE to download only packages you don't already have installed
ipkCRAN(packages, up = TRUE)

The ipkCRAN function will let you know if any of the above packages fail to install.

Whenever you run some code always check the console output for warnings and errors before continuing. It may be easiest to send commands individually to learn about and inspect their output, rather than running the entire file and wondering where an error occurred.

5.1 Dealing With Errors

If a lot of output is produced by a command you should scroll up and sift through it as best you can. Copy and paste parts of the error message to use in internet searches, and try to find cases where folks have encountered problems.

5.1.1 No output is produced after pasting into console

If you do not have a new command prompt (>) and a blinking cursor on the left hand side of your console, but instead see a + after you run a command, R may think you are still in the middle of submitting input to the “read-eval-print-loop” (REPL). If this is not expected you are possibly missing closing quotes, braces, brackets or parentheses. R needs to know you were done with your expression, so you may need to supply some input to get the command to be complete. You can use the shortcut Ctrl+C to get out of a partially-complete command.

Pasting code line-by-line is useful but prone to input errors with multi-line expressions. Alternately, you can run commands or an entire file using the GUI or keyboard shortcuts such as Ctrl+Enter. You have a chance to try this in the example at the end.

5.1.2 ‘SOMEPACKAGE’ is not available (for R version X.Y.Z)

This means either:

  1. A package named ‘SOMEPACKAGE’ exists but it is not available for your version of R

  2. CRAN does not have a package with that name

You can try again, but first check for spelling and case-sensitivity. When in doubt search the package name on Google or CRAN to make sure you have it right. Note that not all R packages are available on CRAN: there are many other ways that you can deliver packages (including GitHub described below).

5.2 Packages not on CRAN

To install the latest version of packages from the Algorithms for Quantitative Pedology (AQP) suite off GitHub we use the remotes package.

The AQP packages are updated much more frequently on GitHub than they are on CRAN.

Generally, the CRAN versions (installed above) are the “stable” releases whereas the GitHub rep