Bonus

If you are already done all the previous exercises and we have extra time, - or on your own after the workshop, try these additional exercises.

{testthat}

testthat is one of the main R packages for testing functions (specifically designed for R packages).

Briefly, the approach is to write test scripts that have a series of expectations for the functions. For example, does the function return a particular class or does the function return an error when expected?

library(testthat)
grep('expect_', lsf.str('package:testthat'), value = TRUE)
 [1] "expect_condition"          "expect_contains"          
 [3] "expect_cpp_tests_pass"     "expect_equal"             
 [5] "expect_equal_to_reference" "expect_equivalent"        
 [7] "expect_error"              "expect_failure"           
 [9] "expect_false"              "expect_gt"                
[11] "expect_gte"                "expect_identical"         
[13] "expect_in"                 "expect_invisible"         
[15] "expect_is"                 "expect_known_hash"        
[17] "expect_known_output"       "expect_known_value"       
[19] "expect_length"             "expect_less_than"         
[21] "expect_lt"                 "expect_lte"               
[23] "expect_mapequal"           "expect_match"             
[25] "expect_message"            "expect_more_than"         
[27] "expect_named"              "expect_no_condition"      
[29] "expect_no_error"           "expect_no_match"          
[31] "expect_no_message"         "expect_no_warning"        
[33] "expect_null"               "expect_output"            
[35] "expect_output_file"        "expect_reference"         
[37] "expect_s3_class"           "expect_s4_class"          
[39] "expect_setequal"           "expect_silent"            
[41] "expect_snapshot"           "expect_snapshot_error"    
[43] "expect_snapshot_file"      "expect_snapshot_output"   
[45] "expect_snapshot_value"     "expect_snapshot_warning"  
[47] "expect_success"            "expect_that"              
[49] "expect_true"               "expect_type"              
[51] "expect_vector"             "expect_visible"           
[53] "expect_warning"           

See the reference and vignettes here: https://testthat.r-lib.org/

Iteration

Try iterating functions over lists or groups using the apply family or purrr::map family.

Object-oriented programming

Read about object-oriented programming classes in R:

Lexical scoping

Try the lexical scoping exercises here: