r语言 - 导出金融工具:::.工具,为工人解开包装



我正在执行一些并行处理,需要从并行处理生成的每个工作线程FinancialInstrument:::.instrument环境中访问仪器属性。

简单的instrument.list <- as.list(FinancialInstrument:::.instrument)并且.export参数与foreach一起使用不起作用(如下面的代码所示,当没有注册并行后端时它有效,而在注册并行后端时则不起作用)。请参阅下面的可重现示例:

library(FinancialInstrument)
library(parallel)
library(doParallel)
library(foreach)
currency(c("USD", "EUR")) # define some currencies
stock(c("SPY", "LQD", "IBM", "GS"), currency="USD") # define some stocks
exchange_rate("EURUSD") # define an exchange rate
ls_stocks() #get the names of all the stocks
ls_instruments() # all instruments
getInstrument("IBM")
instrument.list <- as.list(FinancialInstrument:::.instrument)
# First let's run this without parallel backend registered to show the code works
foreach(1:2,
        .packages="FinancialInstrument",
        .export="instrument.list"
        ) %dopar% {
            .instrument <- as.environment(instrument.list)
            message(paste0("Available instruments in .instrument environment: ", paste(ls_instruments(), collapse=", "), " ."))
        }

# Now, let's register a parallel backend
cl <- makeCluster(2, outfile="log.txt")
registerDoParallel(cl)
# And by looking in log.txt file we see that .instrument environment is not functioning properly. How to make this work?
foreach(1:2,
        .packages="FinancialInstrument",
        .export="instrument.list"
        ) %dopar% {
            .instrument <- as.environment(instrument.list)
            message(paste0("Available instruments in .instrument environment: ", paste(ls_instruments(), collapse=", "), " ."))
        }
stopCluster(cl)

我在worker中使用assignInNamespace函数得到了这个工作。下面是上面问题的工作代码:

library(FinancialInstrument)
library(parallel)
library(doParallel)
library(foreach)
currency(c("USD", "EUR")) # define some currencies
stock(c("SPY", "LQD", "IBM", "GS"), currency="USD") # define some stocks
exchange_rate("EURUSD") # define an exchange rate
ls_stocks() #get the names of all the stocks
ls_instruments() # all instruments
getInstrument("IBM")
instrument.list <- as.list(FinancialInstrument:::.instrument)
# First let's run this without parallel backend registered to show the code works
foreach(1:2,
        .packages="FinancialInstrument",
        .export="instrument.list"
        ) %dopar% {
            .instrument <- as.environment(instrument.list)
            message(paste0("Available instruments in .instrument environment: ", paste(ls_instruments(), collapse=", "), " ."))
        }

# Now, let's register a parallel backend
cl <- makeCluster(2, outfile="log.txt")
registerDoParallel(cl)
# And by looking in log.txt file we see that .instrument environment is not functioning properly. How to make this work?
foreach(1:2,
        .packages="FinancialInstrument",
        .export="instrument.list"
        ) %dopar% {
            assignInNamespace(".instrument", as.environment(instrument.list), "FinancialInstrument")
            message(paste0("Available instruments in .instrument environment: ", paste(ls_instruments(), collapse=", "), " ."))
        }
stopCluster(cl)

相关内容

  • 没有找到相关文章

最新更新