Library contents

R file.exists

Check whether the receipt path is visible before offering a reprint preview.

A simple example

file.exists

  1. Start with No file named a.txt
  2. Do this Open a.txt for reading
  3. Result File missing
The missing-path outcome lets the caller offer an empty starting state or ask for another name.

Syntax

R 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.R

Open 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.

ResultNo receipt yet
Lesson.R
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))
Output
No receipt yet

The program owns a fresh, empty directory, so in this run the named receipt is known to be absent.

Advertisements

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.

Givenrecord.txt exists
Worked resultFile 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.

Givenrecord.txt missing
Worked resultAsk for the file

What to watch for

An inaccessible entry may also appear absent to a status check. Perform the actual read with its own error handling.

Documentation for file.exists