Domanda

Edit1: io sto usando Windows Vista e R 2.12.1

sto tramando 84 mappe con spplot dal pacchetto MapTools. Sto usando due finestre, una per tracciare la mappa, e uno per tracciare un andamento nel tempo. Ho scritto un ciclo che passa attraverso ogni sottoinsieme dei dati, traccia la mappa con i paesi codice colore in base al loro punteggio, e le trame del trend in quel punteggio sopra iterazioni. Gli sguardi ciclo come questo:

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

Il problema è che R si blocca dopo 8 iterazioni, ho il sospetto che in qualche modo sto usando enormi quantità di memoria, ma davvero non ho idea di quello che sta succedendo.

EDIT2: ricevo un messaggio di errore di Windows che dice (tradotto da Germahn): L'R per Windows front-end non funziona più

Edit3: ho tenuto sotto controllo l'utilizzo della memoria da Task Manager di Windows, e dopo 8 iterazioni la memoria è quasi esaurita del tutto.

edit4: ottengo lo stesso errore quando si utilizzano diversi dispositivi grafici (png, jpeg, pdf). Sono stato in grado di eseguire il ciclo senza utilizzare le finestre, in modo da sto ritenendo sospetto che questo è legato alle finestre.

Best, Thomas

È stato utile?

Soluzione

Se funziona con un piccolo shapefile, ma non con un grande (e 48Mb è un grande) allora sì, sarà la memoria. Una cosa che ho visto a volte l'aiuto è quello di attaccare tutto ciò che in un ciclo in una funzione, quindi il tuo aspetto di file come questo:

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

Dopo ogni chiamata a doit () un sacco di cose, si spera, essere in corso fuori del campo di applicazione e, quindi, garbage collection. So che questo trucco lavorato con le prime versioni di R (e Splus), ma forse è stato tutto risolto. Scopri alcune delle funzioni R legate alla memoria per ottenere un handle sul vostro uso del processo. È questo Windows o Unix?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top