R saving blank empty plots when executed in a loop

49 Views Asked by At

I am trying to run the following code, which makes use of the library "kaos" which has its own cgr.plot() function. The goal is to generate an image for each 'seq' in the dataframe. I can obtain the plots for particular cases (for example, i=5), however when I run the for loop, it ends up saving empty images.

What is happening? Are there background processes that need to be terminated in each loop? I have tried with dev.copy(png, save_path) before dev.off() but it still doesn't work.

I am new to R, all help is welcome.

library("kaos")

file_path <- "saved_experiment_Rdata.csv"

# Read the CSV file
df <- read.csv(file_path)

folder_path <- "xxx/images"    # i have here the actual address

# Iterate through rows
for (i in 1:nrow(df)) {
  # Access values in each row
  name <- df[i,'IsolateName']
  seq <- strsplit(df[i,'seq'],"")[[1]]

  file_name <- paste0(name,'.jpg')
  
  save_path <- file.path(folder_path, file_name)
  ### encoding the sequence
  seq.cgr = cgr(seq, res = 100)
  
  # Save the plot to a PNG file
  png(save_path)

  cgr.plot(seq.cgr, mode = "matrix")

  dev.off()
  
}

I have searched the forum, tried different ways but not working.

0

There are 0 best solutions below