rr-packagerparallel

Using mclapply in a function in custom R package


I used R Studio to create a package using these two sites as guides:

https://support.rstudio.com/hc/en-us/articles/200486488-Developing-Packages-with-RStudio https://www.r-bloggers.com/building-a-package-in-rstudio-is-actually-very-easy/

The only modification is when I created the package using R Studio I used the option to create an R package with Rcpp.

One of the functions in the package is the following:

GenerateSims = function(num.sim, alpha0, alpha1, beta1, gamma1, delta, n.sim, covariates, burnin, type)
{
  print("In GenerateSims")

  sims = mclapply(1:num.sim, function(z) {
    GenerateData(alpha0, alpha1, beta1, gamma1, delta, n.sim, covariates, burnin, type)
  },
  mc.cores = 35)

  print("Leaving GenerateSims...")
  return(sims)
}

When mc.cores = 1 it runs fine. When I set mc.cores to something other than 1 the console prints out "In GenerateSims" but stops there as if it's in an infinite loop.

The function GenerateData only uses one function from a non base package gamlss.dist::rDPO.

My DESCRIPTION file is as follows (not sure if this helps identify the issue or not):

Package: Summer2017Package
Type: Package
Title: What the Package Does (Title Case)
Version: 0.1.0
Author: Who wrote it
Maintainer: The package maintainer <yourself@somewhere.net>
Description: More about what it does (maybe more than one line)
    Use four spaces when indenting paragraphs within the Description.
License: What license is it under?
Encoding: UTF-8
LazyData: true
Imports: Rcpp (>= 0.12.9), gamlss.dist, parallel, moments, stats
LinkingTo: Rcpp

EDIT: I am running this on a machine running Debian.


Solution

  • So I figured out that within the GenerateData function there was a print statement. I got rid of this and it fixed my issue. I also came across this post, Printing from mclapply in R Studio, that mentioned this isn't an issue when running an R script from the command line. So that's an easier fix than getting rid of my print statements.