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")
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.
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 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.
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:
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.
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 present a selection of options to the users. These options are presented in a different order every time the question is seen.
Add a message question using wq_multiple().
Here’s an example multiple choice question:
The users see the following in the R console:
1: Toronto
2: Montreal
3: Ottawa
4: Vancouver
For complete documentation about writing swirl courses and lessons see the swirlify website: http://swirlstats.com/swirlify/
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.
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.
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
| | 0%
…
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.
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.
0%
1: Circle
2: Hexagon
3: Square
When I selected a wrong option, I got the following prompt:
|You should know this!
1: Square
2: Circle
3: Hexagon
Selection: |
When I selected the right option the following prompt appeared:
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
I went ahead and created a new r script
3 . We then make required changes in the .yaml prompt, which looks as follow:
Follow the steps 1 through 5.
I changed the new into add for the FigureType.
That works.
Follow the instruction and try it for yourself.