【发布时间】:2015-08-10 18:53:18
【问题描述】:
我正在为我创建的模拟器创建一个闪亮的应用程序。为了加快模拟速度,我使用了parallel 包。
我的应用程序在不并行化我的代码时运行良好,但速度很慢。但是,当我并行化时,出现以下错误:
Error in checkForRemoteErrors(val) :
3 nodes produced errors; first error: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
这里是我的 ui.R 和 server.R 的删减版本:
ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Simulator"),
fluidRow(
column(6,
fluidRow(
column(5,
helpText("Choose 9 bitcoins for firm 1"),
selectizeInput("firm1bit1", label = "Bitcoin 1:",
choices = data$bitcoin, options =
list(maxOptions = 7)),
selectizeInput("firm1bit2", label = "Bitcoin 2:",
choices = data$bitcoin, options =
list(maxOptions = 7)),
selectizeInput("firm1bit3", label = "Bitcoin 3:",
choices = data$bitcoin, options =
list(maxOptions = 7)),
selectizeInput("firm1bit4", label = "Bitcoin 4:",
choices = data$bitcoin, options =
list(maxOptions = 7)),
selectizeInput("firm1bit5", label = "Bitcoin 5:",
choices = data$bitcoin, options =
list(maxOptions = 7)),
selectizeInput("firm1bit6", label = "Bitcoin 6:",
choices = data$bitcoin, options =
list(maxOptions = 7)),
selectizeInput("firm1bit7", label = "Bitcoin 7:",
choices = data$bitcoin, options =
list(maxOptions = 7)),
selectizeInput("firm1bit8", label = "Bitcoin 8:",
choices = data$bitcoin, options =
list(maxOptions = 7)),
selectizeInput("firm1bit9", label = "Bitcoin 9:",
choices = data$bitcoin, options =
list(maxOptions = 7)),
helpText("Choose the maximum number of transactions for firm 1"),
selectizeInput("firm1transacts", label = "Firm 1 maximum number of transactions:",
choices = data$max_transactions, options =
list(maxOptions = 7))
),
column(5,
helpText("Choose 9 bitcoins for firm 2"),
selectizeInput("firm2bit1", label = "Bitcoin 1:",
choices = data$bitcoin, options =
list(maxOptions = 7)),
selectizeInput("firm2bit2", label = "Bitcoin 2:",
choices = data$bitcoin, options =
list(maxOptions = 7)),
selectizeInput("firm2bit3", label = "Bitcoin 3:",
choices = data$bitcoin, options =
list(maxOptions = 7)),
selectizeInput("firm2bit4", label = "Bitcoin 4:",
choices = data$bitcoin, options =
list(maxOptions = 7)),
selectizeInput("firm2bit5", label = "Bitcoin 5:",
choices = data$bitcoin, options =
list(maxOptions = 7)),
selectizeInput("firm2bit6", label = "Bitcoin 6:",
choices = data$bitcoin, options =
list(maxOptions = 7)),
selectizeInput("firm2bit7", label = "Bitcoin 7:",
choices = data$bitcoin, options =
list(maxOptions = 7)),
selectizeInput("firm2bit8", label = "Bitcoin 8:",
choices = data$bitcoin, options =
list(maxOptions = 7)),
selectizeInput("firm2bit9", label = "Bitcoin 9:",
choices = data$bitcoin, options =
list(maxOptions = 7)),
helpText("Choose the maximum number of transactions for firm 2"),
selectizeInput("firm2transacts", label = "Firm 2 maximum number of transactions:",
choices = data$max_transactions, options =
list(maxOptions = 7))
),
submitButton("Simulate")
))
)
))
服务器.R
cl <- makeCluster(detectCores()-1, 'PSOCK')
shinyServer(function(input, output, session){
firm1bits <- reactive({c(input$firm1bit1, input$firm1bit2, input$firm1bit3,
input$firm1bit4, input$firm1bit5, input$firm1bit6,
input$firm1bit7, input$firm1bit8, input$firm1bit9)})
firm2bits <- reactive({c(input$firm2bit1, input$firm2bit2, input$firm2bit3,
input$firm2bit4, input$firm2bit5, input$firm2bit6,
input$firm2bit7, input$firm2bit8, input$firm2bit9)})
firm1max <- reactive({input$firm1transacts})
firm2max <- reactive({input$firm2transacts})
reactive({clusterExport(cl, varlist=c("firm1bits", "firm2bits", "firm1max",
"firm2max"))})
gameResults <- reactive({parSapply(cl, 1:1000, function(i){
simulate_bitcoin_Twoway(firm1bits(), firm2bits(), firm1max(), firm2max())
})})
})
我想重申,当我不使用parSapply() 而是使用replicate() 时,该代码有效。问题不在其他函数中,例如simulate_bitcoin_Twoway()。
【问题讨论】:
-
我有用例和同样的问题。我正在使用
doParallel和foreach。
标签: r parallel-processing shiny