简体   繁体   中英

Add date and time to pdf output file name

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.

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

You could try

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

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM