0

broken image[![][1]] 2

I was not able to get a batch of images with tiff() R function inside for loop.

I have x86_64-w64-mingw32/x64 (64-bit) platform with installed Windows 8.1 and RStudio Version 1.0.143 with R version 3.6.0 (2019-04-26) -- "Planting of a Tree". I tried to obtaine a batch of tiff images by putting my code inside the for loop. Unfortunately that resulted in the numerous broken files (in attachment). But if i type exactly the same code in the RStudio console i got normal (valid) image.

#Fragment of the script
    for (i in 1:nrow(BH))
    {
    ...
    tiff( paste(pos$rs[1],"tiff",sep = '.'))
    ggplot(df_g_,aes(x=factor(g),fill=factor(O)))+geom_bar(stat="count")+xlab("")+labs(fill='')
    dev.off()
    }

#If i perform the following code in console after script execution i got a valid image

    tiff( paste(pos$rs[1],"tiff",sep = '.'))
    ggplot(df_g_,aes(x=factor(g),fill=factor(O)))+geom_bar(stat="count")+xlab("")+labs(fill='')
    dev.off()
Denis
  • 315
  • 4
  • 11

1 Answers1

2

The solution was to envelop ggplotfunction by base print function in the loop:

print(ggplot(df_g_,aes(x=factor(g),fill=factor(O)))+geom_bar(stat="count")+xlab("")+labs(fill=''))

So, i've used the solution from the other post: enter link description here

Denis
  • 315
  • 4
  • 11