Datestamp prefix with `here`

,

Hi RStudio Community,

I often like to datestamp my outputs by prefixing the filename with YYYY-MM-DD_. Currently, I use the wonderful here package combined with base r paste0() which feels bulky. I've also looked into datestamps with the fs package, but haven't found any solutions there. It would be great to have an optional prefix = Sys.Date() argument when building filepaths with here, but this may be beyond the package scope.

I'm wondering whether others have come across this and have other workarounds you like and would recommend.

Reprex below.

Thanks for your thoughts!

Best,

Maia

library(here)
#> here() starts at /myfolder

# No datestamp
here("myfile.txt")
#> [1] "/myfolder/myfile.txt"

# Datestamp but with "/" separator
here(Sys.Date(), "myfile.txt")
#> [1] "/myfolder/2020-07-20/myfile.txt"

# Datestamp but with "_" separator
here(paste0(Sys.Date(), "_myfile.txt"))
#> [1] "/myfolder/2020-07-20_myfile.txt"

Created on 2020-07-20 by the reprex package (v0.3.0)

In R you can create your own functions and use them freely.

#define your own function
hered <- function(x){  here(paste0(Sys.Date(),"_",x))}

#use it at your convenience
hered("myfile.txt")
hered("myfile2.txt")
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.