Issue
This Content is from Stack Overflow. Question asked by user113156
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 rcent” 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 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 itteration.
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"))
Solution
This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.
This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.