4 swirl
4.1 Description
swirl is an interactive R package that helps you self-teach the basics of R. It is run from directly from the R console.
This session guide follows the instructions provided by swirl. Visit the following link to access the full tutorial.
You can also find the full swirl course tutorial on GitHub.
4.2 Install swirl
swirl requires R 3.1.0 or later installed on your computer. It is also recommended that you have RStudio installed which will provide a user-friendly environment to work with.
For instructions on how to install R and RStudio, visit the Installations session guide page.
Once you have downloaded R and RStudio, perform the following steps:
1. Open RStudio.
2. In the RStudio console, type the following where you see the command prompt > :
install.packages("swirl")
4.3 Initialize swirl
Whenever you want to run swirl, you must load and initialize the package.
1. In the console, type the following:
library("swirl")
swirl()
2. Follow any prompts that come up in the console. i.e. if swirl asks "What shall I call you?", type your name and hit enter.
4.4 Install an interactive course
The first time you initialize swirl, you will need to install a course.
For the SATVI Computational Course, we recommend that those who are new to coding start with “R Programming”. This course will cover the basics of programming in R.
There are many courses to choose from, so those who are more advanced may opt for an intermediate or advanced course to work through in their own time. A repository with all available swirl courses can be found here: https://github.com/swirldev/swirl_courses#swirl-courses.
There is also an expansive swirl Network that expands further on open sourse interactive R lessons. You can access the Network and associated courses or become a swirl course author here: https://swirlstats.com/scn/
To install a course that is not part of the swirl course repository, type the following into the console:
?InstallCourses
4.5 Run swirl
For now, we will assume that we are starting with the basics and have chosen to install the “R Programming” course.
To run the interactive lessons:
Select a new lesson. The R Programming course offers 14 different short interactive lessons. Go through each one in order as the information from earlier lessons is required in later lessons.
4.6 Exit swirl
If at any time you need to exit a swirl lesson before it is complete, simply press the Esc key.
If you need to exit from a prompt, exit and save your work by typing: bye()
4.7 Interactive commands
While you are working in swirl, you may find that you want to skip a section that you are already comfortable with, or to work more on the current topic outside of an interactive session.
Below are some helpful commands for getting the most out of your swirl sessions:
From the R prompt (>):
To skip the current question: skip()
To experiment with R on your own without swirl interaction: play()
To re-initiate swirl interaction after playing: nxt()
To exit and save: bye()
To return to swirl's main menu: main()
To display these command options: info()
If you see a swirl output followed by … press Enter to continue.
4.8 Homework
As beginners, regular practice is critical! It is recommended that you go through one or two lessons daily to improve and retain these fundamentals.
Over the next week, in your own time, complete the 14 short interactive lessons from the “R Programming” swirl course.
4.9 FAQ
Q1: Can funcitons learned in swirl be applied when writing my own R scripts?
A: Absolutely! The functions that you use in swirl are all base R functions that can be used in any R script.
Q2: If I need to use an R package, do I need to install the package each time I start a new session?
A: Nope! Once a package is installed, you do not have to re-install when you open a new R session. Simply load the package from the library. For example, if I want to use ggplot2 and I have installed it before, load the package using >library(ggplot2). However, if you are running your script on a different machine that does not have the package installed (and are not working from a container - we'll get to this later in the course), you will need to install the package on that machine before loading it from the library.