R file.exists
Check whether the receipt path is visible before offering a reprint preview.
A simple example
file.exists
- Start with No file named a.txt
- Do this Open a.txt for reading
- Result File missing
Syntax
file.exists(receipt)
Run these examples in R
Save one example at a time as Lesson.R. Use R 4.x.
Rscript --vanilla Lesson.ROpen the first receipt of the day
A new checkout starts with an empty receipt folder. When the cashier asks to reprint today's first receipt, the app should explain that a receipt has yet to be saved.
desk <- tempfile("checkout-")
stopifnot(dir.create(desk))
tryCatch({
receipt <- file.path(desk, "today.txt")
if (!file.exists(receipt)) cat("No receipt yet\n")
}, finally = unlink(desk, recursive = TRUE))
No receipt yet
The program owns a fresh, empty directory, so in this run the named receipt is known to be absent.
Read a saved shift record
The closing clerk saved the file before leaving. Open the expected file and show the desk whether it is available.
Handle an unsubmitted handover
The next shift opens an empty inbox before the first clerk submits a file. Open the expected file and show the desk whether it is available.
What to watch for
An inaccessible entry may also appear absent to a status check. Perform the actual read with its own error handling.