#!/usr/bin/Rscript --vanilla

# Initialize dataset
library(Matrix)
set.seed(0)
filler <- list(sample(1:1000,1000), sample(1:1000,1000), runif(1000,0,100))
sampledata <- sparseMatrix(i = filler[[1]], j = filler[[2]], x = filler[[3]], dims = c(1000,1000))

# Works: image on a sparse matrix outside for loop
item <- "a"
png(paste("img/out_of_for_",item,".png", sep = "", collapse = ""), width = 1000, height = 1000)
image(sampledata)
dev.off()
remove(item)

for(item in c("a")){
  # Does not work: image on a sparse matrix inside for loop
  png(paste("img/in_for_img_",item,".png", sep = "", collapse = ""), width = 1000, height = 1000)
  image(sampledata)
  dev.off()
  # Works: image on a dense matrix inside for loop
  png(paste("img/in_for_hist_",item,".png", sep = "", collapse = ""), width = 1000, height = 1000)
  image(as.matrix(sampledata))
  dev.off()
}
