等待 R 中上一个代码块的完成,请确保它已完成,然后再继续



R有没有办法等待第一个块的完成,然后再继续第二个块?还是确保它在转到下一行之前完成?

几乎完全可重现的代码在这里:ggmap上的ggplot饼图:标签破坏小图

该代码在 ggmap 上生成小的 ggplot。如果逐块执行,效果很好。但是如果我运行整个代码,它将无法生成绘图。

当我重新启动代码的第一部分时,我终于得到了之前发生的事情的消息:

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

我看到我不能同时运行下面的代码块,我需要运行第一个,看到光标提示指示我可以继续下一个:

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)}

我假设 R 不会等待第一个块的完成并继续第二个块。如何确保它已完成第一个并等待第二个块的执行?我尝试了各种解决方案来使脚本交互式或等待 sys.sleep(1(,但它不起作用。我认为命令迭代可能与此有关。

到目前为止我最好的镜头:

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?"))

一个简单的 ggplot 似乎需要大约 1.5 秒,但非常复杂的绘图可能需要大量额外的时间。

最新更新