# create example files
relative_example_file1 <-
basename(
tempfile(
pattern = "QWRAPS2_EXAMPLE_1"
, fileext = ".txt"
, tmpdir = getwd()
)
)
relative_example_file2 <-
basename(
tempfile(
pattern = "QWRAPS2_EXAMPLE_2"
, fileext = ".txt"
, tmpdir = getwd()
)
)
absolute_example_file <- tempfile()
cat("example file.", file = relative_example_file1)
cat("Another example file.", file = relative_example_file2)
cat("Another example file.", file = absolute_example_file)
# Check that you have access to the files in the working directory.
test1 <- file_check(c(relative_example_file1, relative_example_file2))
test1
# By default, when the checks return TRUE the details of the checks are not
# printed. You can view the details of the checks as follows:
attr(test1, "checks")
# access to absolute_example_file will generate a warning about
# absolute_paths by default
test2 <- file_check(absolute_example_file)
test2 <- file_check(absolute_example_file, absolute_paths = "silent")
test2
# Case Match
test_case_match <-
file_check(
c(relative_example_file1, tolower(relative_example_file1))
)
test_case_match
# If one or more files is not accessable then return is FALSE and the meta data
# is printed by default.
test_non_existent_file <-
file_check(
c("UNLIKELYFILENAME", relative_example_file1, relative_example_file2)
)
test_non_existent_file
# Or have an error thrown:
if (FALSE) {
file_check(
c("UNLIKELYFILENAME", relative_example_file1, relative_example_file2)
, stop = TRUE
)
}
# Verify the md5sums as well as file access:
md5_check1 <- file_check(relative_example_file1, "7a3409e17f9de067740e64448a86e708")
md5_check1
# If you only need to verify a subset of md5sums then use an NA in the md5sums
# argument:
md5_check2 <-
file_check(c(relative_example_file1, relative_example_file2),
c("7a3409e17f9de067740e64448a86e708", NA))
md5_check2
# Verify all the md5sums
md5_check3 <-
file_check(c(relative_example_file1, relative_example_file2),
c("7a3409e17f9de067740e64448a86e708", "798e52b92e0ae0e60f3f3db1273235d0"))
md5_check3
# clean up working directory
unlink(relative_example_file1)
unlink(relative_example_file2)
unlink(absolute_example_file)
Run the code above in your browser using DataLab