我有一个循环迭代两次,在每次迭代中生成一个图形并将其写入png文件。它一直工作到昨天。然后,昨天,在做了一些不相关的更改(我不记得它们是什么)后,它停止了工作。具体来说,它不会崩溃甚至不会生成警告,它只是不会按要求创建png文件。
但是,如果我手动将循环变量i设置为1或2,然后选择并执行循环内的代码,它就可以正常工作。
我无法弄清楚为什么会发生这种奇怪的行为。我认为它可能与Rstudio有关,但是我尝试了没有Rstudio,都是从linux命令行运行,并通过source()命令从R命令行运行它,在这两种情况下结果是相同的。
我希望有人能说明出了什么问题。
我在64位PC上的Linux Mint上使用R版本3.3.1,通过Rstudio版本1.0.136。使用的R包包括gridExtra,gridGraphics,gtable和png。
这是循环:
for (i in 1:2){
temp<-subset(graph.dat,qtr.index==i)
png(paste(output_dir,'3.',c('a','b')[i],'.G.png',sep=''),width=1800,height=1152)
# Open file to write graph to. Save as 3.a.G in first iteration and 3.b.G in the second
print(i)
ggplot() +
geom_bar(data = temp, aes(x=Customer, y=pmax(0,Amount), fill=Category),stat = "identity",width=0.5) +
# this line plots the bars above the zero line
geom_bar(data = temp, aes(x=Customer, y=pmin(0,Amount), fill=Category),stat = "identity",width=0.5) +
# this line plots the bars below the zero line
scale_fill_brewer(type = "seq", palette = "Paired") +
labs(x='',y='Provision for tax')+
coord_cartesian(ylim = c(-15000, 15000))+
geom_point(data=subset(Estimate,qtr.index==i), aes(x=Customer, y=Amount),size=13,shape=15)+
# this line plots the point markers
ggtitle(paste('Stocktake - ',dates.df$dates.short[i],sep=''))
dev.off()
# Now save the graph to a .png file
}
这是数据。这是一个只有72行的数据框:
"Category" "Customer" "qtr.index" "Amount"
"Stock" "Cust 1" "2" -2178.34975
"Other Expenses" "Cust 1" "2" -1307.11426
"Other Income" "Cust 1" "2" -4556.12196
"Revenue" "Cust 1" "2" 2797.16517
"Other Expenses" "Cust 1" "2" 4283.00036
"Receivables" "Cust 1" "2" 388.23353
"Stock" "Cust 3" "2" -133.7925
"Other Expenses" "Cust 3" "2" -1056.23411
"Other Income" "Cust 3" "2" -46.64558
"Revenue" "Cust 3" "2" 75.5956
"Other Expenses" "Cust 3" "2" 1747.73888
"Receivables" "Cust 3" "2" -625.0622
"Stock" "Cust 4" "2" -366.3761465
"Other Expenses" "Cust 4" "2" -42.9030924
"Other Income" "Cust 4" "2" -225.5160776
"Revenue" "Cust 4" "2" 150.7087336
"Other Expenses" "Cust 4" "2" 7.2424856
"Receivables" "Cust 4" "2" 489.100618
"Stock" "Cust 6" "2" -1944.27337910587
"Other Expenses" "Cust 6" "2" -719.491754021401
"Other Income" "Cust 6" "2" -3506.80597312394
"Revenue" "Cust 6" "2" 2377.06949826214
"Other Expenses" "Cust 6" "2" 6272.82876949579
"Receivables" "Cust 6" "2" -3128.20591286837
"Stock" "Cust 7" "2" -3143.20946024247
"Other Expenses" "Cust 7" "2" -1191.70611887001
"Other Income" "Cust 7" "2" -4947.00046655931
"Revenue" "Cust 7" "2" 3672.57987260769
"Other Expenses" "Cust 7" "2" 5057.1723408229
"Receivables" "Cust 7" "2" -1777.46732478221
"Stock" "Cust 5" "2" -3096.57633
"Other Expenses" "Cust 5" "2" -1489.33705
"Other Income" "Cust 5" "2" -1788.19689
"Revenue" "Cust 5" "2" 2586.39059
"Other Expenses" "Cust 5" "2" 1974.36526
"Receivables" "Cust 5" "2" 754.04481
"Stock" "Cust 1" "1" -2248.1011
"Other Expenses" "Cust 1" "1" -1406.18579
"Other Income" "Cust 1" "1" -4804.24372
"Revenue" "Cust 1" "1" 3082.00792
"Other Expenses" "Cust 1" "1" 4621.07184
"Receivables" "Cust 1" "1" 49.54561
"Stock" "Cust 3" "1" -125.23352
"Other Expenses" "Cust 3" "1" -1010.1423
"Other Income" "Cust 3" "1" -50.4339
"Revenue" "Cust 3" "1" 75.24065
"Other Expenses" "Cust 3" "1" 1604.92511
"Receivables" "Cust 3" "1" -558.1197
"Stock" "Cust 4" "1" -353.344313
"Other Expenses" "Cust 4" "1" -41.970828
"Other Income" "Cust 4" "1" -195.553684
"Revenue" "Cust 4" "1" 154.670712
"Other Expenses" "Cust 4" "1" 5.998472
"Receivables" "Cust 4" "1" 432.343192
"Stock" "Cust 6" "1" -1995.81268914429
"Other Expenses" "Cust 6" "1" -748.13574488012
"Other Income" "Cust 6" "1" -3420.80676167929
"Revenue" "Cust 6" "1" 2491.28703802434
"Other Expenses" "Cust 6" "1" 5932.08646545911
"Receivables" "Cust 6" "1" -2980.3132874941
"Stock" "Cust 7" "1" -3176.99364830721
"Other Expenses" "Cust 7" "1" -1258.21862586926
"Other Income" "Cust 7" "1" -5264.98717587913
"Revenue" "Cust 7" "1" 4184.60759887337
"Other Expenses" "Cust 7" "1" 5555.32857632657
"Receivables" "Cust 7" "1" -2362.99537101079
"Stock" "Cust 5" "1" -3149.18544
"Other Expenses" "Cust 5" "1" -1521.17289
"Other Income" "Cust 5" "1" -2032.76354
"Revenue" "Cust 5" "1" 2807.38446
"Other Expenses" "Cust 5" "1" 2236.51582
"Receivables" "Cust 5" "1" 677.97416