0

I am trying to use Here to easy file referencing and it is working okay if I write pdf or csv files without any sys date. For example; pdf(here("output/first.pdf)) works but not if I add any of the formatting text. Appreciate if you can help me how to make below code work with Here. Thanks!

pdf(file=paste0(projPath,format(Sys.Date(),"%Y%b%d"),".pdf"),
    height = 6, width = 16)
write.csv(df,file=paste(projPath,"/output/first_",format(Sys.Date(),"%Y%b%d"),".csv",sep=""), quote=F, row.names = F, na=".")
MrFlick
  • 195,160
  • 17
  • 277
  • 295
nmfsci
  • 5
  • 2
  • Are you saying `pdf(here(paste0(format(Sys.Date(),"%Y%b%d"),".pdf")))` didn't work? Or what exactly did you try? You just need to pass a string to `here()`. You can build that string with `paste0()` or `format()` or whatever you like. – MrFlick Sep 02 '22 at 15:10
  • Thank you @MrFlick. It worked. I wasn't using _paste0_ with _here_. – nmfsci Sep 02 '22 at 18:29

1 Answers1

0

I recommend that you use the glue library, it makes injecting additions into strings a breeze. See this example

library(here)
library(glue)

(mydate <- format(Sys.Date(),"%Y%b%d"))
here(glue("something_{mydate}.pdf"))
Nir Graham
  • 2,567
  • 2
  • 6
  • 10