Skip to content

Dynamic Creation and Use of Object Names

5 messages · Ivan Krylov, Dr Eberhard W Lisse, Bert Gunter

#
Hi,

I have a function PICTURE() and do something like

	COUNTRY = 'Namibia'
	NAKURVE = PICTURE(COUNTRY)
	NAKURVE
	ggsave(paste0(tolower(COUNTRY),".png"), width = 16, height = 9)

	COUNTRY = ('Germany')
	DEKURVE = PICTURE(COUNTRY)
	DEKURVE
	ggsave(paste0(tolower(COUNTRY),".png"), width = 16, height = 9)

	COUNTRY = ('Netherlands')
	NLKURVE = PICTURE(COUNTRY)
	NLKURVE
	ggsave(paste0(tolower(COUNTRY),".png"), width = 16, height = 9)

[...]

	COUNTRYGRID=grid.arrange(NAKURVE, DEKURVE, NLKURVE,
				 ncol=3)
	COUNTRYGRID
	ggsave(paste0("R7.incidence.", Sys.Date(), ".png"), COUNTRYGRID)

I am not able to figure out (and/or find on Google) how to do this in a
loop (of sorts), ie create the variables dynamically and add them to to
the grid (dynamically, ie adding more countries)

Any ideas?

greetings, el
#
On Mon, 23 Aug 2021 08:37:54 +0200
Dr Eberhard Lisse <nospam at lisse.NA> wrote:

            
In my opinion, creating variables in the global environment
programmatically may lead to code that is hard to understand and debug
[*]. A key-value data structure (a named list or a separate
environment) would avoid the potential problems from variable name
collision. How about the following:

1. Put the countries in a vector: c('Namibia', 'Germany', ...)

2. Use lapply() to get a list of objects returned from your PICTURE
   function

3. To save the pictures into individual files, loop over the list. You
   can use setNames on the step 1 or 2 to make it a named list and keep
   the country names together with their pictures:
   
   for (n in names(pictures)) {
     dev.new()
     print(pictures[[n]])
     ggsave(paste0(n, '.png'), ...)
     dev.off()
   }

   (You can also use the png() device and plot straight to the file,
   avoiding the need to draw the plot in the window for a fraction of a
   second and for ggsave().)

4. Use the grobs= argument of grid.arrange() to pass the list of
   objects to arrange instead of passing individual objects via ...
#
Thank you,

more to study :-)-O

el
On 23/08/2021 10:20, Ivan Krylov wrote:

  
    
#
... and to add to Ivan's suggestions, **depending on what you are
trying to show with your grid of graphs,**  you may wish to consider
using ggplot's "facet" capabilities to assure that any quantitative
variables that you are encoding in the maps (e.g. by color, density
shading, etc.) are depicted on the same scale with appropriate
legends.  (Of course, ignore if this is not the case).  If so, you
will need a different data structure for your data, I believe.

Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Mon, Aug 23, 2021 at 4:51 AM Dr Eberhard Lisse <nospam at lisse.na> wrote:
#
Thanks,

long weekend coming up :-)-O

el
On 2021-08-23 18:06 , Bert Gunter wrote:
[...]