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_all_equal"          "expect_all_false"         
 [3] "expect_all_true"           "expect_condition"         
 [5] "expect_contains"           "expect_cpp_tests_pass"    
 [7] "expect_disjoint"           "expect_equal"             
 [9] "expect_equal_to_reference" "expect_equivalent"        
[11] "expect_error"              "expect_failure"           
[13] "expect_false"              "expect_gt"                
[15] "expect_gte"                "expect_identical"         
[17] "expect_in"                 "expect_invisible"         
[19] "expect_is"                 "expect_known_hash"        
[21] "expect_known_output"       "expect_known_value"       
[23] "expect_length"             "expect_less_than"         
[25] "expect_lt"                 "expect_lte"               
[27] "expect_mapequal"           "expect_match"             
[29] "expect_message"            "expect_more_than"         
[31] "expect_named"              "expect_no_condition"      
[33] "expect_no_error"           "expect_no_failure"        
[35] "expect_no_match"           "expect_no_message"        
[37] "expect_no_success"         "expect_no_warning"        
[39] "expect_null"               "expect_output"            
[41] "expect_output_file"        "expect_r6_class"          
[43] "expect_reference"          "expect_s3_class"          
[45] "expect_s4_class"           "expect_s7_class"          
[47] "expect_setequal"           "expect_shape"             
[49] "expect_silent"             "expect_snapshot"          
[51] "expect_snapshot_error"     "expect_snapshot_failure"  
[53] "expect_snapshot_file"      "expect_snapshot_output"   
[55] "expect_snapshot_value"     "expect_snapshot_warning"  
[57] "expect_success"            "expect_that"              
[59] "expect_true"               "expect_type"              
[61] "expect_vector"             "expect_visible"           
[63] "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: