Edit1:我正在使用Windows Vista和R 2.12.1

我正在使用Maptools软件包中的SPPLOT绘制84幅地图。我正在使用两个视口,一个来绘制地图,另一个是随着时间的推移来绘制趋势的。我编写了一个循环,该循环遍历了数据的每个子集,并根据其分数编码的国家绘制地图,并将该趋势绘制在该分数上,而不是迭代。循环看起来像这样:

##Read in the shape file, it is 48mb
eu27 <- readShapeSpatial("C:/Users/Thomas/Documents/EU27_shapefile/eu27_cyp.shp",proj4string=CRS     ("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))

##Drop not used variables from the shapefile data table
eu27@data <- data.frame(Order = 1:dim(eu27)[1], Country = as.character(eu27@data[,c("NAME_ISO")]))

##Fix longitude and lattitude for plotting later
xl <- c(-15,36) 
yl <- c(33,73)

##Set the directory for storing the plots
setwd("C:/Users/Thomas/Documents/maps/")

##Define the breaks and  colors
brks <- seq(0,8,1)
colcode <- terrain.colors(length(brks))

##Count used in the loop to keep track of iterations
count <- 1

##Vector to store the trend in time
total <- vector()

##Beginning of loop
for(i in 2001:2003){
 for(j in 1:12){
                ##Subset the data and merge it with the shape file data table
  dat <- d3[d3$Year == i & d3$Month == j, c("No","No.Neg","Country")]
  eu27dat <- merge(eu27@data,dat, by="Country", all.x=TRUE)
  eu27@data <- eu27dat[order(eu27dat$Order),]
  eu27@data$tot <- eu27@data$No + eu27@data$No.Neg

                ##Store the map plot with countries color coded in the plot1 object
  plot1 <- spplot(eu27, "tot", main = paste("Year: ",i,", Month: ",j, sep = ""), xlim=xl, ylim=yl, at = brks, col.regions = colcode)

                ##Remove the variables created
  eu27@data$No <- NULL
  eu27@data$No.Neg <- NULL
  eu27@data$tot <- NULL

                ##Update the vector with trend data from the current iteration
  total[count] <- sum(dat$No.Neg, na.rm = T)

                ##Store the trend data plot in the plot2 object
  plot2 <-xyplot(total ~ 1:length(total), type = "b", xlab = "Time", ylab = "No Votes", ylim = c(-1,4))

                ##Open a PNG device
  png(file = paste("mapNo",count,".png", sep = ""),width=20, height=12, units="cm",bg="white", pointsize=20, res=300)

                ##Create two viewports for the two plots created above
  pushViewport(viewport(layout = grid.layout(1, 2, unit(c(2, 1), "null"))))

                ##Print plot1 in the first viewport
  pushViewport(viewport(layout.pos.col = 1, layout.pos.row = 1)) 
  print(plot1, newpage = F) 
  upViewport() 

                ##Print plot2 in the second viewport
  pushViewport(viewport(layout.pos.col = 2, layout.pos.row = 1)) 
  print(plot2, newpage = F) 
  upViewport()

                ##Close the device
  dev.off()

                ##Update the count
  count <- count + 1
 }
}

问题是R次迭代后崩溃,我怀疑我以某种方式消耗了大量的内存,但我真的不知道发生了什么。

EDIT2:我收到一个Windows错误消息,该消息说(从Germahn翻译):Windows前端的R不再运行

EDIT3:我一直在监视Windows任务管理器的内存使用,在8个迭代后,内存几乎完全用完了。

EDIT4:使用不同的图形设备(PNG,JPEG,PDF)时,我会遇到相同的错误。我能够在不使用视口的情况下运行循环,因此我怀疑这与视口有关。

最好,托马斯

有帮助吗?

解决方案

如果它可以与一个小的shapefile一起使用,但不能与大型文件一起使用(和48MB是一个大),那么是的,它将是内存。有时我看到的一件事是将所有内容都插入一个函数中,因此您的文件看起来像这样:

for(i in 2001:2003){
 for(j in 1:12){
   doit(i,j,[etc])
  }
}

每次打电话给doit()后,很多事情都会有望超出范围,从而收集了垃圾。我知道这个技巧与R(和Splus)的早期版本一起使用,但也许现在已经修复了。查看与内存有关的一些R功能,以获取您的流程使用情况。这个窗口还是Unix?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top