Create a new blank .csv file at 23:59 everyday with column headers

Viewed 35

I have some data which looks like:

# A tibble: 20 × 2
   collectionDate collectionTime     
   <date>         <dttm>             
 1 2022-09-18     2022-09-18 20:52:56
 2 2022-09-18     2022-09-18 20:52:56
 3 2022-09-18     2022-09-18 20:52:56
 4 2022-09-18     2022-09-18 20:52:15
 5 2022-09-18     2022-09-18 20:51:38
 6 2022-09-18     2022-09-18 20:49:15
 7 2022-09-18     2022-09-18 20:49:15
 8 2022-09-18     2022-09-18 20:49:15
 9 2022-09-18     2022-09-18 20:48:34
10 2022-09-18     2022-09-18 20:46:35
11 2022-09-18     2022-09-18 20:46:35

It is constantly updating and saving to a file based on the collectionDate (one file for each date) - currently I have to manually create a new empty csv file in order for the data to continue into the next day...

I want to create a function which just immediately after 24:00:00 it creates a new empty .csv file (or timed perfectly to create the .csv file at 24:00:00.

Note:

Since the data is being appended to a .csv file I use the following to read in the "most recent" data.

  files = file.info(list.files("myDir/myDir2", full.names = T))
  mostRecentlySavedFile = rownames(files)[which.max(files$mtime)]
  data = read_csv(mostRecentlySavedFile)  

This allows me to "automate" the continuous appending to that days file. I have problems at 24:00:00 - I want to read in the most recently updated file but that would be the newly created file for the following day...

I append the data as:

write_csv(., append = TRUE, paste("myDir", gsub("-", "_", Sys.Date()), ".csv", sep = "_"))

so it looks like

/myDir/2022_18_09.csv

(I want to create /myDir/2022_19_09.csv... etc.

I thought about using something like:

myDataNew = tibble(
  collectionDate = NA,
  collectionTime = NA
)

if(Sys.time() + lubridate::minutes(1) == paste(Sys.Date(), "24:00:00")){
  file.create(myDataNew)
}

But creating the file 1 minute before 24:00:00 gives me the problem that I will read in the "most recent" data on the wrong day.

Question: How can I create a new file exactly on 24:00:00 so that the new day can continue collecting the data and saving it as .csv.

This function will be inserted into the function which updates continuously (every 40 seconds) so I can check the Sys.time() on each iteration.

Data:

data = structure(list(collectionDate = structure(c(19253, 19253, 19253, 
19253, 19253, 19253, 19253, 19253, 19253, 19253, 19253, 19253, 
19253, 19253, 19253, 19253, 19253, 19253, 19253, 19253), class = "Date"), 
    collectionTime = structure(c(1663534376, 1663534376, 1663534376, 
    1663534335, 1663534298, 1663534155, 1663534155, 1663534155, 
    1663534114, 1663533995, 1663533995, 1663533995, 1663533878, 
    1663533800, 1663533762, 1663533722, 1663533722, 1663533684, 
    1663533449, 1663533449), tzone = "UTC", class = c("POSIXct", 
    "POSIXt"))), row.names = c(NA, -20L), class = c("tbl_df", 
"tbl", "data.frame"))
0 Answers
Related