0

Is there a way for R to wait for the completion of the first chunk before proceed to the second? Or make sure it completes before going to the next line?

Almost complete reproducible code is here: ggplot piecharts on a ggmap: labels destroy the small plots

The code generates small ggplots on a ggmap. It works great if executed chunk by chunk. But if I run the whole code it fails to generate the plot.

When I restart the first section of the code I finally get the message of what happened earlier:

Warning message:
In unit %in% c("grobx", "groby", "grobwidth", "grobheight", "grobascent",  :
  reached elapsed time limit

I see that I cannot run chunks of the code below both at once, I need to run the first, see the cursor prompt indicate that I can go ahead with the next:

df.grobs <- df %>% 
  do(subplots = ggplot(., aes(1, sales, fill = component)) + 
       geom_bar(position = "stack", alpha = 0.5, colour = "white", stat="identity") + 
       geom_text( aes(label = round(sales), y=sales), position = position_stack(vjust = 0.5), size = 2.5)  +
       coord_polar(theta = "y") + 
       scale_fill_manual(values = c("green", "red"))+
       theme_void()+ guides(fill = F)) %>% 
  mutate(subgrobs = list(annotation_custom(ggplotGrob(subplots), 
                                           x = lon-Potential_Sum/300, y = lat-Potential_Sum/300, 
                                           xmax = lon+Potential_Sum/300, ymax = lat+Potential_Sum/300))) 



df.grobs %>%
{p + 
    .$subgrobs + 
            #geom_text(data=df, aes(label = round(Potential_Sum,0)), size=2.5) + 
            geom_col(data = df,
             aes(0,0, fill = component), 
             colour = "white")+ geom_text(data=df, aes(label = Miasto),nudge_y = -0.15, size=2.5)}

I presume R does not wait for the completion of the first chunk and proceeds to the second. How to make sure it is done with the first and wait with the execution of the second chunk? I tried various solutions for making the script interactive or wait sys.sleep(1) but it did not work. I presume do command iterations may have something to do with it.

Jacek Kotowski
  • 620
  • 16
  • 49

1 Answers1

0

My best shot thus far:

  Delay <- function(wait = 1000) {
    n <- as.numeric(Sys.time()) + wait / 1000
    while (as.numeric(Sys.time()) < n) {}
  }

  plot(graph)
  ### Time-out to finish preview of plot
  Delay(some time in milliseconds)
  ### Plot to file if result is OK
  sel_yesno <- tk_messageBox(type = "yesno", paste("Save plot to file?"))

around 1.5 seconds for an easy ggplot seems needed but very complex plots can take a whole lot of added time.