Search code examples
rloopsforeachmatrix-factorization

Save output of foreach loop in R NMF package


I apologise because I know this question has been asked before, but I've tried most variations of the answer to no help.

I am using the foreach package to run a loop of the NMF algorithm on a set of data. I am them trying to extract the set of basis names using the ExtractFeatures function, which outputs each run as a list of how many ranks you determine (in the example I have, 3).

Here is my code below (I've used a sample dataset from the NMF Vignette):

library("NMF")
library("doParallel")
library("foreach")

#load vignette dataset and shorten it for time's sake
data(esGolub)
esGolub <- esGolub[1:200,]

#output matrix
Extract_Genes <- matrix()

#set cores
registerDoParallel(cores = 3)

#Loop NMF runs and extract features
foreach(i =1:4, .packages = "NMF") %dopar%{
  i <- nmf(esGolub, 3, nrun = 1)
  Extract_Genes <- extractFeatures(i, format = "list")
}

This outputs the list of extracted genes like so:

[[1]]
[[1]][[1]]
[1]  43 120 128 130

[[1]][[2]]
[1]  94   1 112  42   8  64  96 182  59  41  69  25  26

[[1]][[3]]
[1]  39  74   2  91 190 167 103 129 174

3 times over for the 3 runs, but it does not save. Would anyone have any suggestions on how I save these outputs?

Thank you in Advance, J


Solution

  • Comment by Imo works fine:

    myList <- foreach(i =1:4, .packages = "NMF") %dopar%{