Home > Back-end >  Shiny and parallel package
Shiny and parallel package

Time:08-30

I'm writing a shiny app, which runs a function over a set of parameters, so I figured I could use multiple cores.

For some reason it can't feed in the variables to the cluster, I get an error: "var_mean" not found. I've tried isolate but that didn't seem to help.

The code below is a very simple example which reproduces the behaviour.

Thanks for any help.

library(shiny)
library(parallel)

ui <- fluidPage(

    # Application title
    titlePanel("Test parallel in Shiny app"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            numericInput("n","N",value = 100),
            numericInput("mean","Mean",value = 1000),
            checkboxInput("parallel","Parallel?",value=FALSE)
         ),
        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot")
        )
    )
)

server <- function(input, output) {

   simulate <- reactive({
      
      mean = input$mean
      
      sim = rnorm(input$n,mean=mean,sd = 100)
      
      return(sim)
      
   })
   
   p_simulate<-reactive({
      
      cl = makeCluster(detectCores()-1)
      
      var_mean = input$mean
      
      clusterExport(cl,varlist="var_mean")
      
      sim = parSapply(cl, 
                      1:input$n,
                      function(x) rnorm(1,mean=var_mean,sd = 100)
                      )
      
      stopCluster(cl)

      sim
      
   })
   
    output$distPlot <- renderPlot({
        
       if(input$parallel){
          x = p_simulate()
       } else x = simulate()
       
        # draw the histogram with the specified number of bins
        hist(x)
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

CodePudding user response:

Your "clusterExport" command is missing the "envir" flag:

clusterExport(cl,varlist="var_mean", envir = environment())

That got it running for me.

  • Related