Static and Dynamic Book Exercises with R

Exercises from the book Analyzing Financial and Economic Data with R

In the new edition of my R book, to be released in early 2020 (see current TOC, new packages and notification form), I’m giving special attention to its use in the classroom. For that, I’ve created class slides and R exercises in the static and dynamic form. All the extra content will be freely available in the internet and distributed with package afedR. Anyone can use it, without the need of purchasing the book (but off course it would help).

To access the files, first install the package from Github with devtools (ignore warning messages about long paths):

devtools::install_github('msperlin/afedR')

You can copy all book content to a local folder using the following function:

my_tempdir <- tempdir()
afedR::afedR_get_book_files(path_to_copy = my_tempdir)

The static exercises for all chapters are available in the afedR files/eoc-exercises folder:

list.files(file.path(my_tempdir, 'afedR files/eoc-exercises/'))
##  [1] "afedR-Solutions-Chapter-01-Introduction.Rmd"                   
##  [2] "afedR-Solutions-Chapter-02-Basic Operations.Rmd"               
##  [3] "afedR-Solutions-Chapter-03-Writing-Research-Scripts.Rmd"       
##  [4] "afedR-Solutions-Chapter-04-Importing-Exporting-Local-Files.Rmd"
##  [5] "afedR-Solutions-Chapter-05-Importing-Data-Internet.Rmd"        
##  [6] "afedR-Solutions-Chapter-06-Dataframes-and-others.Rmd"          
##  [7] "afedR-Solutions-Chapter-07-Basic-Classes.Rmd"                  
##  [8] "afedR-Solutions-Chapter-08-Programming-with-R.Rmd"             
##  [9] "afedR-Solutions-Chapter-09-Cleaning-Structuring-Data.Rmd"      
## [10] "afedR-Solutions-Chapter-10-Figures.Rmd"                        
## [11] "afedR-Solutions-Chapter-11-Financial-Econometrics.Rmd"         
## [12] "afedR-Solutions-Chapter-12-Reporting-Results.Rmd"              
## [13] "afedR-Solutions-Chapter-13-Optimizing-Code.Rmd"

Every .Rmd file is self-contained and should compile without any problems in your computer.

Now, the real benefit of the package is in the dynamic R exercises you can create with package exams. Back in 2017 I already talked about my admiration and use of exams in all of my university classes. In short, you can use exams to create an unique version of a exercise for each student by randomizing numbers and text. All questions are written in .Rmd/.Rnw files and, since its all RMarkdown code, you can make it as dynamic as possible. The amount of hours it saved me so far in creating and grading exams is unbelievable! I even changed the structure of all my classes to a more activity-oriented coursework based on single-choice exercises. The feedback I get from the students has been very positive.

As a result of using exams for many years, I wrote a significant database of R single-choice questions that I use in my university courses. It amounts to 91 questions, covering R basics, functions, class objects, programming, econometrics, and much more. All of these exam questions are included in the package and I’ll add more with time. You can find all of them in a compiled html file in this link.

Creating a Dynamic Exam

Creating a dynamic R exam is simple. All you need is the names of all students among other options. Function afedR_build_exam will grab all exercise templates, compile each exam, and output a different .html file for each student. Have a look at a reproducible example:

library(afedR)
library(tidyverse)

set.seed(1)

student_names <- c('Roger Federer', 'John Wick', 'Robert Engle', 
                   'Getulio Vargas', 'Mario Quintana', 'Elis Regina') 
my_ids <- c(sample(seq(length(student_names)))) # ids (usually a numeric and unique symbol given by the university)
class_name <- 'R Workshop'
exercise_name <- 'Introduction to R'
temp_dir <- file.path(tempdir(), 'html exams') # where to create exam files

l_out <- afedR_build_exam(students_names = student_names, 
                          students_ids = my_ids, 
                          class_name = class_name, 
                          exercise_name = 'Introduction to R',
                          chapters_to_include = 2, # single chapter for simplicity (it goes from 1-13)
                          dir_out = temp_dir)
## Exams generation initialized.
## 
## Output directory: /tmp/Rtmprsh4CX/exams files file29381321d46f
## Exercise directory: /mnt/HDD/Dropbox/11-My Website/www.msperlin.com-blog/content/post
## Supplement directory: /tmp/Rtmprsh4CX/file2938539ecdc8
## Temporary directory: /tmp/Rtmprsh4CX/file29385ddf98d
## Exercises: /home/msperlin/R/x86_64-pc-linux-gnu-library/3.6/afedR/extdata/exam_files/exercise_files/Chapter 02 - Basic Operations/Chapter_02-Intro-01-AboutPrint, /home/msperlin/R/x86_64-pc-linux-gnu-library/3.6/afedR/extdata/exam_files/exercise_files/Chapter 02 - Basic Operations/Chapter_02-Intro-04-AboutTypeFiles, /home/msperlin/R/x86_64-pc-linux-gnu-library/3.6/afedR/extdata/exam_files/exercise_files/Chapter 02 - Basic Operations/Chapter_02-Intro-05-AboutTypeFiles2, /home/msperlin/R/x86_64-pc-linux-gnu-library/3.6/afedR/extdata/exam_files/exercise_files/Chapter 02 - Basic Operations/Chapter_02-Intro-06-Sequence, /home/msperlin/R/x86_64-pc-linux-gnu-library/3.6/afedR/extdata/exam_files/exercise_files/Chapter 02 - Basic Operations/Chapter_02-Intro-08-AboutError2, /home/msperlin/R/x86_64-pc-linux-gnu-library/3.6/afedR/extdata/exam_files/exercise_files/Chapter 02 - Basic Operations/Chapter_02-Intro-09-About_ls, /home/msperlin/R/x86_64-pc-linux-gnu-library/3.6/afedR/extdata/exam_files/exercise_files/Chapter 02 - Basic Operations/Chapter_02-Intro-10-SelectingValues, /home/msperlin/R/x86_64-pc-linux-gnu-library/3.6/afedR/extdata/exam_files/exercise_files/Chapter 02 - Basic Operations/Chapter_02-Intro-11-AboutDim, /home/msperlin/R/x86_64-pc-linux-gnu-library/3.6/afedR/extdata/exam_files/exercise_files/Chapter 02 - Basic Operations/Chapter_02-Intro-12-listfiles, /home/msperlin/R/x86_64-pc-linux-gnu-library/3.6/afedR/extdata/exam_files/exercise_files/Chapter 02 - Basic Operations/Chapter_02-Intro-13-About_rm, /home/msperlin/R/x86_64-pc-linux-gnu-library/3.6/afedR/extdata/exam_files/exercise_files/Chapter 02 - Basic Operations/Chapter_02-Intro-14-setwd
## 
## Generation of individual exams.
## Exam 1: _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-01-AboutPrint (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-04-AboutTypeFiles (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-05-AboutTypeFiles2 (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-06-Sequence (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-08-AboutError2 (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-09-About_ls (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-10-SelectingValues (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-11-AboutDim (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-12-listfiles (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-13-About_rm (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-14-setwd (srt) ... w ... done.
## Exam 2: _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-01-AboutPrint (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-04-AboutTypeFiles (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-05-AboutTypeFiles2 (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-06-Sequence (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-08-AboutError2 (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-09-About_ls (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-10-SelectingValues (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-11-AboutDim (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-12-listfiles (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-13-About_rm (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-14-setwd (srt) ... w ... done.
## Exam 3: _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-01-AboutPrint (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-04-AboutTypeFiles (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-05-AboutTypeFiles2 (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-06-Sequence (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-08-AboutError2 (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-09-About_ls (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-10-SelectingValues (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-11-AboutDim (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-12-listfiles (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-13-About_rm (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-14-setwd (srt) ... w ... done.
## Exam 4: _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-01-AboutPrint (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-04-AboutTypeFiles (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-05-AboutTypeFiles2 (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-06-Sequence (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-08-AboutError2 (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-09-About_ls (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-10-SelectingValues (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-11-AboutDim (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-12-listfiles (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-13-About_rm (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-14-setwd (srt) ... w ... done.
## Exam 5: _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-01-AboutPrint (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-04-AboutTypeFiles (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-05-AboutTypeFiles2 (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-06-Sequence (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-08-AboutError2 (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-09-About_ls (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-10-SelectingValues (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-11-AboutDim (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-12-listfiles (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-13-About_rm (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-14-setwd (srt) ... w ... done.
## Exam 6: _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-01-AboutPrint (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-04-AboutTypeFiles (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-05-AboutTypeFiles2 (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-06-Sequence (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-08-AboutError2 (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-09-About_ls (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-10-SelectingValues (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-11-AboutDim (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-12-listfiles (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-13-About_rm (srt) _home_msperlin_R_x86_64-pc-linux-gnu-library_3.6_afedR_extdata_exam_files_exercise_files_Chapter 02 - Basic Operations_Chapter_02-Intro-14-setwd (srt) ... w ... done.

Done. All exams files are available in folder /tmp/Rtmprsh4CX/html exams:

list.files(temp_dir)
## [1] "Introduction to R_Elis Regina_Ver 06.html"   
## [2] "Introduction to R_Getulio Vargas_Ver 04.html"
## [3] "Introduction to R_John Wick_Ver 02.html"     
## [4] "Introduction to R_Mario Quintana_Ver 05.html"
## [5] "Introduction to R_Robert Engle_Ver 03.html"  
## [6] "Introduction to R_Roger Federer_Ver 01.html"

As an example of html output, file Introduction to R_Roger Federer_Ver 01.html is available in this link.

Going further, the output of afedR_build_exam is a list that includes the correct answers for each student/question:

print(l_out$answer_key)
## # A tibble: 6 x 13
##   i_name i_ver `1`   `2`   `3`   `4`   `5`   `6`   `7`   `8`   `9`   `10`  `11` 
##   <chr>  <int> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 Elis …     6 e     e     e     e     e     d     d     d     d     d     d    
## 2 Getul…     4 e     e     e     e     c     a     a     a     a     a     a    
## 3 John …     2 a     a     a     a     a     a     a     a     a     a     a    
## 4 Mario…     5 a     a     a     a     e     e     e     e     e     e     e    
## 5 Rober…     3 a     a     a     a     e     e     e     e     e     e     e    
## 6 Roger…     1 d     d     d     d     e     a     a     a     a     a     a

You can use this table for grading all exams. Currently I use Google Forms to register student’s answers with an online questionnaire. This helps because I can turn all answers in a single Google Spreadsheet, import it in R with package googlesheets4, and effortlessly grade all exams in a R script. Soon, in another post, I’ll write about my detailed workflow in using exams with Google Forms and Google Classroom.

I hope this content helps all R instructions around the world. But, it is a work in progress. If you find any issue, please let me know by email or posting an issue event at the github code.

The book is finally reaching its final version and I’m very excited about it. Its been a long journey. You can find more details about the new book here. If you want to be notified about the publication, just sign this form and I’ll email you as soon as the book becomes available.

Avatar
Marcelo S. Perlin
Associate Professor of Finance

Related

comments powered by Disqus