10

I am exporting graph outputs from R to a pdf file.

I would like to add the Sys.time() and Sys.Date() to the outfile name.

For instance I have a statement

pdf("output filename.pdf", 8,10)

I would like to output to look like output filename 2010-03-25 2pm.pdf

or something similar.

Chase
  • 65,190
  • 17
  • 140
  • 160
Vivek Kumar
  • 243
  • 1
  • 3
  • 8

3 Answers3

15

Combine Sys.time() with some formatting to get what you want:

paste(format(Sys.time(), "%Y-%m-%d %I-%p"), "pdf", sep = ".")
[1] "2011-03-24 03-PM.pdf"

Formatting options can be found in ?strptime

Chase
  • 65,190
  • 17
  • 140
  • 160
6

You could try

pdf (file=paste (Sys.time(), ".pdf", sep=""))
plot (rnorm (100))
dev.off()
5

Break it into two steps for easy implementation on other docs.

st=format(Sys.time(), "%Y-%m-%d_%H:%M")
paste("filename_",st, ".pdf", sep = "")
[1] "filename_2018-06-19_11:20.pdf"
Ralf Stubner
  • 25,305
  • 3
  • 37
  • 71
Aiden Johnson
  • 51
  • 1
  • 2
  • Great idea! I had problem to use colon in a file name and therefore simply removed it. Instead I use st=format(Sys.time(), "%Y-%m-%d_%H%M"). – Johanna Sörensen Aug 04 '21 at 12:14