R で tm パッケージと並列計算を使用しているときに問題が発生しました。何かばかげたことをしているのか、それともバグなのかわかりません。
再現可能な小さな例を作成しました:
# Load the libraries
library(tm)
library(snow)
# Create a Document Term Matrix
test_sentence = c("this is a test", "this is another test")
test_corpus = VCorpus(VectorSource(test_sentence))
test_TM = DocumentTermMatrix(test_corpus)
# Define a simple function that returns the matrix for the i-th document
test_function = function(i, TM){ TM[i, ] }
この例を使用して単純なラップリーを実行すると、問題なく期待どおりの結果が得られます。
# This returns the expected list containing the rows of the Matrix
res1 = lapply(1:2, test_function, test_TM)
しかし、並行して実行すると、次のエラーが発生します。
最初のエラー: 次元数が正しくありません
# This should return the same thing of the lapply above but instead it stops with an error
cl = makeCluster(2)
res2 = parLapply(cl, 1:2, test_function, test_TM)
stopCluster(cl)