0

I'm trying to automatically name a jpg output using a character list. This is an Rmarkdown file I use for reporting. For each month I summarise groundwater statistics and produce a basic Rmardown html. I automatically save the maps to a folder I specify in the script. The maps always have the name GLmap.jpg. I would like to use an appropriate name such as the year and month. Is there a way to do this?? Here's what I've tried so far

Library(tmap)
data("World")

test_map <- tmap::tm_shape(World)+
  tmap::tm_polygons("HPI")

Now I specify the year and month that I want

Curdate <- Sys.time() %m+% months(-1) 
Curedatemnyr <- as.character(format(Curdate, '%Y %B'))

Now I try to save it with a name that references by year and month

tmap::tmap_save(test_map,"'Curedatemnyr'_GLmap.jpg")

This doesn't reference the year and month from the list just gives the exact name i.e. 'Curedatemnyr'_GLmap.jpg instead of 2020 March_GLmap.jpg.

Any ideas??

Simon
  • 295
  • 1
  • 4
  • 12

2 Answers2

1

Use paste0 :

tmap::tmap_save(test_map,paste0(Curedatemnyr, '_GLmap.jpg'))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • 1
    So simple :) ...thank you. I also shared this with a few other beginners and they LOVED the tip too - so thanks form them too – Simon Apr 07 '20 at 21:04
  • This worked perfectly but what if I want to save these images to a new folder?? ```tmap::tmap_save(test_map,paste0(Curdatemnyr, 'tmap_folder/_GLmap.jpg'))``` – Simon Oct 11 '20 at 19:42
  • okay I found out how to do this - this should work ```tmap::tmap_save(GLmap,paste0('tmap_folder/', Curdatemnyr,'_GLmap.jpg', sep =""))``` – Simon Oct 11 '20 at 21:06
0

I am getting errors on the line Sys.time() %m+% months(-1)

I am just trying to walk through your example and was getting errors, I don't know why. but I think Ronak Shah above gave you a the right answer

Daniel_j_iii
  • 3,041
  • 2
  • 11
  • 27
  • 1
    I just re-tested by copying the script above and it worked for me. The solution by Ronak worked perfectly. – Simon Apr 07 '20 at 20:51