Compiling book exercises

Exporting exercises to pdf | Moodle | Blackboard

The third edition of Analyzing Financial and Economic Data with R provides a total of 98 end-of-chapter exercises. All activities are freely available in the exams format, meaning that any R tutor can export the same exercises and solutions to use in their own class. In this post I’ll show how to compile exercises to pdf, html, Moodle and blackboard.

Installation

The first step is to install package afedR3 with devtools:

if (!require(devtools)) install.packages('devtools')

devtools::install_github('msperlin/afedR3')

Another requirement is a working Latex instalation. For that, use tinytex:

tinytex::install_tinytex()

Compiling Exercises

How it works?

All book exercises are written in the exams format: each exercise is a .Rmd file containing code, exercise text and solution. The files themselves can be found in the installation directory of the book package, and each folder contains the exercise for a particular chapter:

eoc_dir <- afedR3::get_EOC_dir()

eoc_chapters <- fs::dir_ls(eoc_dir)
basename(eoc_chapters)
##  [1] "CH01-intro"        "CH02-basic"        "CH03-research"    
##  [4] "CH04-imp-local"    "CH05-imp-internet" "CH06-df"          
##  [7] "CH07-basic"        "CH08-programming"  "CH09-cleaning"    
## [10] "CH10-figures"      "CH11-fin-econ"     "CH12-reporting"   
## [13] "CH13-optimizing"

Let’s have a deeper look at the exercises of the first chapter:

eoc_files <- fs::dir_ls(eoc_chapters[1])
basename(eoc_files)
##  [1] "afedR_Chap-01_01_SPLUS.Rmd"            
##  [2] "afedR_Chap-01_02_Authors-R.Rmd"        
##  [3] "afedR_Chap-01_03_About-R.Rmd"          
##  [4] "afedR_Chap-01_04_name-R.Rmd"           
##  [5] "afedR_Chap-01_05_about-R.Rmd"          
##  [6] "afedR_Chap-01_06_Tecnology-R.Rmd"      
##  [7] "afedR_Chap-01_07_rtools.Rmd"           
##  [8] "afedR_Chap-01_08_Groups.Rmd"           
##  [9] "afedR_Chap-01_09_RBloggers.Rmd"        
## [10] "afedR_Chap-01_10_Infrastructure-TI.Rmd"

We can also read one of the files to show the structure of the exercise in code and text:

readLines(eoc_files[1])
##  [1] "```{r datageneration, echo = FALSE, results = \"hide\"}"                                                                                                                                
##  [2] "my_answers <- c('S', "                                                                                                                                                                  
##  [3] "                'C++',"                                                                                                                                                                 
##  [4] "                'Python',"                                                                                                                                                              
##  [5] "                'Julia',"                                                                                                                                                               
##  [6] "                'Javascript')"                                                                                                                                                          
##  [7] ""                                                                                                                                                                                       
##  [8] "#check_answers(my_answers)"                                                                                                                                                             
##  [9] "```"                                                                                                                                                                                    
## [10] ""                                                                                                                                                                                       
## [11] "Question"                                                                                                                                                                               
## [12] "========"                                                                                                                                                                               
## [13] ""                                                                                                                                                                                       
## [14] "The R language was developed based on what other programming language?"                                                                                                                 
## [15] ""                                                                                                                                                                                       
## [16] "```{r questionlist, echo = FALSE, results = \"asis\"}"                                                                                                                                  
## [17] "exams::answerlist(my_answers, markup = \"markdown\")"                                                                                                                                   
## [18] "```"                                                                                                                                                                                    
## [19] ""                                                                                                                                                                                       
## [20] "Solution"                                                                                                                                                                               
## [21] "================"                                                                                                                                                                       
## [22] ""                                                                                                                                                                                       
## [23] "Straight from the book, section **What is R**: \"R is a modern version of S, a programming language originally created in Bell Laboratories (formerly AT&T, now Lucent Technologies).\""
## [24] ""                                                                                                                                                                                       
## [25] "Meta-information"                                                                                                                                                                       
## [26] "================"                                                                                                                                                                       
## [27] "extype: schoice"                                                                                                                                                                        
## [28] "exsolution: `r mchoice2string(c(TRUE, FALSE, FALSE, FALSE, FALSE), single = TRUE)`"                                                                                                     
## [29] "exname: \"S PLUS\""                                                                                                                                                                     
## [30] "exshuffle: TRUE"                                                                                                                                                                        
## [31] ""

In a nutshell, we define all sections of a question – text, solution, alternatives – using a .Rmd template. Again, you can find more details about using package {exams} in its website.

Compiling to pdf

To help tutors compiling their own exercises, I wrote function afedR3::compile_pdf_exercises() . You’ll need the following information to use it:

  • (REQUIRED) name of students (will be printed in pdf)
  • (OPTIONAL) students ids (I usually use their university card number)
  • (OPTIONAL) Chapters to include (e.g 1:3)
  • (OPTIONAL) Exercise name (e.g. Activity I, Exercise II, ..)
  • (OPTIONAL) Course name (e.g. Tutorial in R)

Here’s an example:

library(afedR3)

names_students <- c('Michael Peterling', 'John Aspper', 'Mr. Beans')
ids_students <- sample(1:1000, length(names_students)) # probably id card?
class_name <- "Introduction to R"
exercise_name <- "Activity 01"
chapters <- 1:3 # chapters from 1 to 13
dir_output <- fs::file_temp('pdf-example_')

df_exams <- compile_pdf_exercises(students_names = names_students, 
                                  students_ids = ids_students, 
                                  class_name = class_name,
                                  exercise_name = exercise_name,
                                  chapters_to_include = chapters,
                                  dir_out = dir_output)

The output of compile_pdf_exercises is a table with the correct answers for schoice and num type of questions:

glimpse(df_exams)
## Rows: 69
## Columns: 4
## $ i_name   <chr> "Michael Peterling", "Michael Peterling", "Michael Peterling"…
## $ i_ver    <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ i_q      <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18…
## $ solution <chr> "b", "b", "a", "b", "e", NA, "c", NA, NA, NA, NA, NA, NA, NA,…

After compilation, all pdf files are available at folder dir_output:

fs::dir_ls(dir_output)
## /tmp/Rtmpo8a1iU/pdf-example_7a710296d8801/Activity 01_Ver 01_Michael Peterling.pdf
## /tmp/Rtmpo8a1iU/pdf-example_7a710296d8801/Activity 01_Ver 02_John Aspper.pdf
## /tmp/Rtmpo8a1iU/pdf-example_7a710296d8801/Activity 01_Ver 03_Mr. Beans.pdf

The final result will be as follows:

Exporting to Moodle

You can also export to e-learning platforms such as Moodle. The process is quite simple as exams package does all the heavy work:

library(afedR3)

eoc_folders <- afedR3::get_EOC_dir()

available_chapters <- afedR3::exercises_dir_list()

exercises_folders <- purrr::map_chr(
  available_chapters[1:3],
  afedR3::exercises_dir_get
)

exercises_files <- fs::dir_ls(exercises_folders)

dir_output <- fs::file_temp('moodle-test_')

exams::exams2moodle(file = exercises_files, 
                    name = 'MOODLE_afedR-eoc-chapters_01-03', 
                    dir = dir_output)

fs::dir_ls(dir_output)
## /tmp/Rtmpo8a1iU/moodle-test_7a7101ff66451/MOODLE_afedR-eoc-chapters_01-03.xml

The resulting .xml file can be imported in the database of any Moodle class you have access.

Exporting to Blackboard

Likewise, exporting to Blackboard is simple:

library(afedR3)
library(tth) # required for bb 

dir_output <- dir_output <- fs::file_temp('blackboard-test_')

exams::exams2blackboard(file = exercises_files, 
                        name = 'BB_afedR-eoc-chapters_01-03', 
                        dir = dir_output)

fs::dir_ls(dir_output)
## /tmp/Rtmpo8a1iU/blackboard-test_7a710dc108e0/BB_afedR-eoc-chapters_01-03.zip

This .zip file contains all exercises of chapters 01, 02 and 3, and can be imported in your blackboard account.

Marcelo S. Perlin
Marcelo S. Perlin
Associate Professor

My research interests include data analysis, finance and cientometrics.

comments powered by Disqus