14

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
  • 67,710
  • 18
  • 144
  • 161
Vivek Kumar
  • 283
  • 1
  • 3
  • 8

3 Answers3

19

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
  • 67,710
  • 18
  • 144
  • 161
7

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
  • 26,263
  • 3
  • 40
  • 75
Aiden Johnson
  • 71
  • 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
6

You could try

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