Package: base


Function: file.exists()


1. Check to see if today’s file (“summary_obs_YYYY-MM-DD.xlsx”) already exists in a folder (“all_files”). If it does not, copy the file.

To prevent duplication of efforts each day, I want to build a function that lets people know if the current day’s file has already been moved over.

Let’s view files in today’s observation folder (today is 2023-01-19).

base::basename(fs::dir_ls(here::here("file-system", "obs_2023-01-19")))
[1] "obs1.xlsx"        "obs2.xlsx"        "obs3.xlsx"        "summary_obs.xlsx"

I see “summary_obs.xlsx” exists in today’s folder but I’m not sure if it has been moved over to the “all_files” folder yet and renamed to “summary_obs_2023-01-19”.

  1. To check this, I first need to set the path to where the file should live if it has already been moved. I assign this to the object summary_files.
  • Note: I could just simply list the name of the file like this.
summary_file <- here::here("file-system", "all_files",  "summary_obs_2023-01-19.xlsx")

However, because I want to use this function every day, I need to write this in a way that updates based on the current day. Here I use base::paste0() to combine “summary_obs_”, with the current date in YYYY-MM-DD format, grabbed using lubridate::today().

summary_file <- here::here("file-system", "all_files",  base::paste0("summary_obs_", lubridate::today(), ".xlsx"))
  1. Next, I need to set the path of where the file originates from. I assign this to an object called file_to_copy.
file_to_copy <- here::here("file-system",  base::paste0("obs_", lubridate::today()), "summary_obs.xlsx")
  1. Last, I write an if..else statement to say:
    If the “summary_obs” file does not exist (we check using base::file.exists()), copy the “summary_obs” file from today’s observation folder, add today’s date as a suffix, and copy to the “all_files” folder. If the file already exists, give me a message that says “Data already exists”.
  • Note: We are using fs::file_copy() to copy a file from the existing location to the new location. We use base::cat() to print out our message.

  • Note: I could build this same kind of statement for files that need to be downloaded rather than copied as well.

if(!file.exists(summary_file)) {
  fs::file_copy(path = file_to_copy , new_path = summary_file)
} else {
  cat("File already exists")
}
File already exists

And the message is correct, the file already exists.

base::basename(fs::dir_ls(here::here("file-system", "all_files")))
[1] "summary_obs_2022-03-15.xlsx" "summary_obs_2022-03-16.xlsx"
[3] "summary_obs_2023-01-19.xlsx"
  • Note: If we were not worried about possible changes in files, we could skip the if..else statement, just use fs::file_copy(), and add the argument overwrite = TRUE to overwrite existing files in the folder. NOTE: The function will only overwrite the file if changes have been made. The function appears to verify the checksum first. Then if the file remains unchanged, the function will not overwrite the existing file.
fs::file_copy(path = file_to_copy, new_path = summary_file, overwrite = TRUE)

Package: base


Function: exists()


1. Check to see if an object, “summary_obs”, already exists in your environment.

Similar to checking if a file exists, I think its worth pointing out that you can also check to see if objects exist in your environment using base::exists(). For example if you run a series of functions, you might want to confirm certain objects were created.

So for example, our hypothetical function should create the object summary_obs by binding each days observations into one large data frame. Let’s see if our object already exists.

if(!exists("summary_obs")) {
  summary_obs <- fs::dir_ls(path = here::here("file-system", "all_files"), glob ="*.xlsx") %>%
  purrr::map_dfr(readxl::read_excel)
} else {
  cat("Data already exists")
}

We can see that the statement confirmed that the object does exist. [At this time I am not getting the message to render in the html document but it does appear in R!]

Return to File System