Making Our Own Course

To be able to make our own program like ‘Swirl’, we need to install package called ‘swirlify’ using the /install.packages(“swirlify”)/ syntax.

I want to make sure that I know my working folder. If it is not on the folder of my choice, I would like to set it using the /setwd()/ syntax.

library(swirlify)
getwd()
## [1] "C:/Users/nirma/Documents/Coursera/Data Science/Developing Data Products/Developing Data Products"
setwd("C:/Users/nirma/Documents/Coursera/Data Science/Developing Data Products")

Creating a New Lesson within a Course

We can now start writing new swirl course and new lessons with a new lesson function. Let’s name it lesson 1 start the process.

new_lesson(“Lesson 1”, “My First Course”)

Executing that function did did several things. The first thing that it did was it opened up the YAML file. All swirl lessons are written in YAML. It if a fairly easy to understand markup language. Each question in question is demarcated by a hyphen, and then there is a set of key value pairs that specifies each type of question, and what goes into that question in our swirl lesson.

Checking if the Lesson and Courses are Created

Now, we can go to the directory where we created this lesson. We can see that there’s a new folder called “My First Course”. Opening it up, there is one lesson inside of it, called Lesson_1. Each course is its own folder and each lesson is its own folder inside of a course. And then that new lesson function created four files.

The Files in the Course Folder

The first file ‘customTests.R’ is where we can specify our own functions for testing the correctness of swirl questions. We’ll talk about answer testing a little bit later. ‘Dependson.txt’ is a text file that we can use to list the names of R packages that are on CRAN. That’s where we’ll check to make sure R installed before the lesson begins. ‘initLesson.R’ is an R script, and whatever we write in that R script will happen before a lesson starts, so, we can load the data inside of initLesson.R or we can create functions that will soon be used in the lesson. And then the ‘lesson.yaml’ file. I like using RStudio for writing swirl lessons because having a text editor and an R console next to each other on the same screen works for me. I certainly recommend using RStudio but different people have their different work flows.

Adding Questions

Message Questions

Now we can start adding questions using the wq_*** series of functions that come with swirlify. We can include multiple types of questions as we desire, however, message are the simplest ones. If we select wq_message(), a message function gets added to the .yaml file. Remember, we select the question types in the R-console, not within the lesson.yaml file. That reads:

    • Class: text
  • Output: put your text output here

We do delete the put your text output here prompt and write our question.

If we want to add a command question, we can do so by selecting wq_command() syntax. Once we do that we see the following prompt added to the .yaml file.

    • Class: cmd_question
  • Output: explain what the user must do here
  • CorrectAnswer: EXPR or VAL
  • AnswerTests: omnitest(correctExpr=‘EXPR’, correctVal=VAL)
  • Hint: hint

We can change what we want our users to do in Output for example, Add 2 and 2 using the plus operator. CorrectAnswer, then would be simply 2+2. The AnswerTests is the function that tests the correctness of what the user enters into the console. omnitest comes with the Swirl package and for 80-90% we want to use this function. Thus, AnswerTests changes into something like omnitest(correctExpr=‘2+2’). The hint is something that you want your user to read if they struggle or if they want to make sure. Thus, the above prompt will change into something like this:

(do these things on the ‘lesson.yaml’ file)
* - Class: cmd_question
* Output: Add 2 and 2 using the plus operator.
* CorrectAnswer: 2+2
* AnswerTests: omnitest(correctExpr=‘2+2’)
* Hint: Just type 2+2

Multiple Choice Questions

Multiple choice questions present a selection of options to the users. These options are presented in a different order every time the question is seen.

  • The AnswerChoices should be a semicolon separated string of choices that the student will have to choose from.

Add a message question using wq_multiple().

Here’s an example multiple choice question:

  • - Class: mult_question
  • Output: What is the capital of Canada?
  • AnswerChoices: Toronto; Montreal; Ottawa; Vancouver
  • CorrectAnswer: Ottawa
  • AnswerTests: omnitest(correctVal=‘Ottawa’)
  • Hint: This city contains the Rideau Canal.

The users see the following in the R console:

What is the capital of Canada?

1: Toronto

2: Montreal

3: Ottawa

4: Vancouver

Other Question Types

For complete documentation about writing swirl courses and lessons see the swirlify website: http://swirlstats.com/swirlify/

Adding New Lesson to Course Manifest

All right, so we’ve written some questions, now time to start testing this lesson. Every time we add a lesson to a course, we should add it to what’s called the course manifest. We can do this very easily by using the add_to_manifest() function on R-Console.

Checking if the Lesson Added to Manifest

Go to the My first Course folder we can see a new file created alongside the Lesson_1 folder. And this manifest file just keeps track of the order that we want our lessons to be displayed in. And it also keeps track of what the lessons in the course are. That way if we want to put data or other files inside of this course folder, we can do that and those files won’t show up as a lesson that the users can select when they’re choosing a lesson to work on, inside of swirl. So, the manifest file just kind of keeps things organized.

Testing Our Lesson

Now we can test out the lesson by typing the test_lesson() function on R Console. We get the following output on the Console:

### Begin testing: Lesson 1 #####
##### End testing: Lesson 1 #####

We don’t see any message or warning here, which means this lesson passed the required tests, e.g., some formatting checks that come with ‘swirlify’.

Then, I want to actually use this lesson inside of swirl, so I’m going to use the demo_lesson function on R-Console. Let’s try that:

The following outputs were observed


Course Installed successfully!
|                               |   0%
Welcome to Lesson 1!


And when I do what ever the prompt says. I get the following message:

|You are quite good my friend!

|Lesson complete! Exiting swirl now …


So far, we have created our first lesson and tested it. Let’s get into some of the more complicated question types that we can ask in a swirl lesson. We want to create a new lesson. We can do so by simply closing the ‘lesson.yaml’ file.

Once we do so, I want to check my working lesson (take it as the general working directory checking) by using get_current_lesson() function.

Creating Second Lesson

We can create a second lesson using the new_lesson(“Lesson 2”, “My First Course”) syntax. Once again, if that is successful we can see a new lesson.yaml file created. The items we see on the .yaml file is the first question. It is called the meta question. The users are not able to see it, and we use it to provide some information about the lesson in this section. This is how it looks.

We are going to test some complicated types of questions types. Let’s begin with the Multiple Choice Questions.

  1. We type wq_multiple() on the Console.
  2. The new script gets added to the lesson.yaml file:
  1. We go ahead and make necessary changes. It now looks the following way:
  1. Add the lesson to the course manifest by typing add_to_manifest() syntax on R-Console
  2. Quickly run test lesson by typing test_lesson() on R-Console. We get the following prompt. There’s no error message.
Begin testing: Lesson 2
End testing: Lesson 2
  1. Run the demo lesson by typing demo_lesson() on R-Console. We see the following things:
Course Installed Successfully!

    0%

Which of these shapes has four sides?

1: Circle

2: Hexagon

3: Square

When I selected a wrong option, I got the following prompt:

That’s not the answer I was looking for, but try
again.

|You should know this!

1: Square

2: Circle

3: Hexagon

Selection: |

When I selected the right option the following prompt appeared:

That’s a job well done!
Lesson complete! Exiting swirl now…

Writing Figure Type Question

  1. This can be done by typing wq_figure() on R-Console.
  2. A new section gets added to the ‘lesson.yaml’ file, which looks like this:
  • Class: figure
  • Output: explain the figure here
  • Figure: sourcefile.R
  • FigureType: new
  1. we must mention a source file for the figure to appear. Figure type =new is okay. If the figure keeps building on every other question we have to delete new and write the buildup language. For the figure

  2. I went ahead and created a new r script

  1. I then wrote a simple syntax plot(1:10) on the r script file
  2. saved it as fig1.R in the same lesson folder
  3. called it in the lesson.yaml file.

3 . We then make required changes in the .yaml prompt, which looks as follow:

  • Class: figure
  • Output: This is a simple graph.
  • Figure: fig1.R
  • FigureType: new
  1. I tested it again using the demo_lesson() syntax on R-Console.
  2. It worked !!

Adding Figure Question that Builds on Previous Figure

Follow the steps 1 through 5.

  1. I created a new R-Script file called fig2.R and saved it on the Lesson 2 folder.
  2. I then, went ahead and called the file name on the lesson.yaml file. Here’s how it looks:
  • Class: figure
  • Output: I added a line!
  • Figure: fig2.R
  • FigureType: add

I changed the new into add for the FigureType.

  1. I then tested the lesson with demo_lesson() syntax on R-Console

That works.

Follow the instruction and try it for yourself.