R Under development (unstable) (2024-10-14 r87233 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(qwraps2) > e <- new.env() > example("file_check", local = e) fl_chc> # create example files fl_chc> fl_chc> relative_example_file1 <- fl_chc+ basename( fl_chc+ tempfile( fl_chc+ pattern = "QWRAPS2_EXAMPLE_1" fl_chc+ , fileext = ".txt" fl_chc+ , tmpdir = getwd() fl_chc+ ) fl_chc+ ) fl_chc> relative_example_file2 <- fl_chc+ basename( fl_chc+ tempfile( fl_chc+ pattern = "QWRAPS2_EXAMPLE_2" fl_chc+ , fileext = ".txt" fl_chc+ , tmpdir = getwd() fl_chc+ ) fl_chc+ ) fl_chc> absolute_example_file <- tempfile() fl_chc> cat("example file.", file = relative_example_file1) fl_chc> cat("Another example file.", file = relative_example_file2) fl_chc> cat("Another example file.", file = absolute_example_file) fl_chc> # Check that you have access to the files in the working directory. fl_chc> test1 <- file_check(c(relative_example_file1, relative_example_file2)) fl_chc> test1 [1] TRUE fl_chc> # By default, when the checks return TRUE the details of the checks are not fl_chc> # printed. You can view the details of the checks as follows: fl_chc> attr(test1, "checks") path QWRAPS2_EXAMPLE_1e6d45e0a2002.txt QWRAPS2_EXAMPLE_1e6d45e0a2002.txt QWRAPS2_EXAMPLE_2e6d42a6f5d1c.txt QWRAPS2_EXAMPLE_2e6d42a6f5d1c.txt absolute_path accessible case_match QWRAPS2_EXAMPLE_1e6d45e0a2002.txt FALSE TRUE TRUE QWRAPS2_EXAMPLE_2e6d42a6f5d1c.txt FALSE TRUE TRUE current_md5sum QWRAPS2_EXAMPLE_1e6d45e0a2002.txt 7a3409e17f9de067740e64448a86e708 QWRAPS2_EXAMPLE_2e6d42a6f5d1c.txt 798e52b92e0ae0e60f3f3db1273235d0 expected_md5sum md5check QWRAPS2_EXAMPLE_1e6d45e0a2002.txt NA QWRAPS2_EXAMPLE_2e6d42a6f5d1c.txt NA fl_chc> # access to absolute_example_file will generate a warning about fl_chc> # absolute_paths by default fl_chc> test2 <- file_check(absolute_example_file) fl_chc> test2 <- file_check(absolute_example_file, absolute_paths = "silent") fl_chc> test2 [1] TRUE fl_chc> # Case Match fl_chc> test_case_match <- fl_chc+ file_check( fl_chc+ c(relative_example_file1, tolower(relative_example_file1)) fl_chc+ ) fl_chc> test_case_match [1] FALSE attr(,"checks") path QWRAPS2_EXAMPLE_1e6d45e0a2002.txt QWRAPS2_EXAMPLE_1e6d45e0a2002.txt qwraps2_example_1e6d45e0a2002.txt qwraps2_example_1e6d45e0a2002.txt absolute_path accessible case_match QWRAPS2_EXAMPLE_1e6d45e0a2002.txt FALSE TRUE TRUE qwraps2_example_1e6d45e0a2002.txt FALSE TRUE FALSE current_md5sum QWRAPS2_EXAMPLE_1e6d45e0a2002.txt 7a3409e17f9de067740e64448a86e708 qwraps2_example_1e6d45e0a2002.txt 7a3409e17f9de067740e64448a86e708 expected_md5sum md5check QWRAPS2_EXAMPLE_1e6d45e0a2002.txt NA qwraps2_example_1e6d45e0a2002.txt NA fl_chc> # If one or more files is not accessable then return is FALSE and the meta data fl_chc> # is printed by default. fl_chc> test_non_existent_file <- fl_chc+ file_check( fl_chc+ c("UNLIKELYFILENAME", relative_example_file1, relative_example_file2) fl_chc+ ) fl_chc> test_non_existent_file [1] FALSE attr(,"checks") path UNLIKELYFILENAME UNLIKELYFILENAME QWRAPS2_EXAMPLE_1e6d45e0a2002.txt QWRAPS2_EXAMPLE_1e6d45e0a2002.txt QWRAPS2_EXAMPLE_2e6d42a6f5d1c.txt QWRAPS2_EXAMPLE_2e6d42a6f5d1c.txt absolute_path accessible case_match UNLIKELYFILENAME FALSE FALSE FALSE QWRAPS2_EXAMPLE_1e6d45e0a2002.txt FALSE TRUE TRUE QWRAPS2_EXAMPLE_2e6d42a6f5d1c.txt FALSE TRUE TRUE current_md5sum UNLIKELYFILENAME QWRAPS2_EXAMPLE_1e6d45e0a2002.txt 7a3409e17f9de067740e64448a86e708 QWRAPS2_EXAMPLE_2e6d42a6f5d1c.txt 798e52b92e0ae0e60f3f3db1273235d0 expected_md5sum md5check UNLIKELYFILENAME NA QWRAPS2_EXAMPLE_1e6d45e0a2002.txt NA QWRAPS2_EXAMPLE_2e6d42a6f5d1c.txt NA fl_chc> # Or have an error thrown: fl_chc> ## Not run: fl_chc> ##D file_check( fl_chc> ##D c("UNLIKELYFILENAME", relative_example_file1, relative_example_file2) fl_chc> ##D , stop = TRUE fl_chc> ##D ) fl_chc> ## End(Not run) fl_chc> fl_chc> # Verify the md5sums as well as file access: fl_chc> md5_check1 <- file_check(relative_example_file1, "7a3409e17f9de067740e64448a86e708") fl_chc> md5_check1 [1] TRUE fl_chc> # If you only need to verify a subset of md5sums then use an NA in the md5sums fl_chc> # argument: fl_chc> md5_check2 <- fl_chc+ file_check(c(relative_example_file1, relative_example_file2), fl_chc+ c("7a3409e17f9de067740e64448a86e708", NA)) fl_chc> md5_check2 [1] TRUE fl_chc> # Verify all the md5sums fl_chc> md5_check3 <- fl_chc+ file_check(c(relative_example_file1, relative_example_file2), fl_chc+ c("7a3409e17f9de067740e64448a86e708", "798e52b92e0ae0e60f3f3db1273235d0")) fl_chc> md5_check3 [1] TRUE fl_chc> # clean up working directory fl_chc> unlink(relative_example_file1) fl_chc> unlink(relative_example_file2) fl_chc> unlink(absolute_example_file) Warning message: In file_check(absolute_example_file) : Absolute path used. It would be preferable if you used a relative path. > ls(envir = e) [1] "absolute_example_file" "md5_check1" "md5_check2" [4] "md5_check3" "relative_example_file1" "relative_example_file2" [7] "test1" "test2" "test_case_match" [10] "test_non_existent_file" > > ################################################################################ > # Test 1 # > stopifnot(isTRUE(e$test1)) > stopifnot(identical(attr(e$test1, "checks")[, c("absolute_path")], c(FALSE, FALSE))) > stopifnot(identical(attr(e$test1, "checks")[, c("case_match")], c(TRUE, TRUE))) > stopifnot(identical(attr(e$test1, "checks")[, c("current_md5sum")], c("7a3409e17f9de067740e64448a86e708", "798e52b92e0ae0e60f3f3db1273235d0"))) > stopifnot(identical(attr(e$test1, "checks")[, c("expected_md5sum")], c(NA_character_, NA_character_))) > stopifnot(identical(attr(e$test1, "checks")[, c("md5check")], c(NA,NA))) > > ################################################################################ > # Test 2 # > > # stopifnot( > # identical( > # e$test2 > # , > # TRUE > # ) > # ) > > ################################################################################ > # test case match # > stopifnot(all.equal(unclass(e$test_case_match), FALSE, check.attributes = FALSE)) > > ################################################################################ > e$md5check3 NULL > > ################################################################################ > > # verify error is thrown when stop = TRUE > x <- + tryCatch(file_check(c("UNLIKELYFILENAME", "QWRAPS2_EXAMPLE_1.txt", "QWRAPS2_EXAMPLE_2.txt"), stop = TRUE) + , error = function(e) e ) [1] FALSE attr(,"checks") path absolute_path accessible case_match UNLIKELYFILENAME UNLIKELYFILENAME FALSE FALSE FALSE QWRAPS2_EXAMPLE_1.txt QWRAPS2_EXAMPLE_1.txt FALSE FALSE FALSE QWRAPS2_EXAMPLE_2.txt QWRAPS2_EXAMPLE_2.txt FALSE FALSE FALSE current_md5sum expected_md5sum md5check UNLIKELYFILENAME NA QWRAPS2_EXAMPLE_1.txt NA QWRAPS2_EXAMPLE_2.txt NA > stopifnot(!is.null(x)) > stopifnot(inherits(x, "error")) > > proc.time() user system elapsed 0.26 0.06 0.31