高级调试

R 并行编程

Nabeel Imam

Data Scientist

默认错误行为

一位程序员看着屏幕很沮丧,把桌上的文件都扔了。

R 并行编程

玩具示例

var_list <- list(1:10,
                 1:10,
                 c("a", "b", "c"))

lapply(var_list, sqrt)
Error in FUN(X[[i]], ...) : non-numeric
argument to mathematical function
  • 数值
  • 数值
  • 字符
R 并行编程

歧义

一台拟人化电脑因屏幕报错而困惑。

1 Designed by Freepik
R 并行编程

捕获错误

sqrt_custom <- function(var) {

tryCatch( # Expression to evaluate sqrt(var),
# What to do with an error error = function (e) return(e) )
}

一名工作人员用扳手捕捉并修复错误。

1 Designed by Freepik
R 并行编程

捕获错误

lapply(var_list, sqrt_custom)
[[1]]
 [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427 3.000000
[10] 3.162278

[[2]]
 [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427 3.000000
[10] 3.162278

[[3]]
<simpleError in sqrt(var): non-numeric argument to mathematical function>
R 并行编程

并行中捕获错误

cl <- makeCluster(3)
parLapply(cl, var_list, sqrt_custom)
stopCluster(cl)
[[1]]
 [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427 3.000000
[10] 3.162278

[[2]]
 [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427 3.000000
[10] 3.162278

[[3]]
<simpleError in sqrt(var): non-numeric argument to mathematical function>
R 并行编程

出生数据

print(ls_births)
$AK
 month  plurality
     1          1
     2          1
 ...
$AL
   month  plurality
      10          1
       9          1
 ...
...
R 并行编程

汇总函数

summarise_births <- function(df) {


tryCatch({
df %>% group_by(month) %>% summarise(total = sum(plurality))
},
error = function (e) "Error! Check data")
}
R 并行编程

并行 apply

cl <- makeCluster(4)
clusterEvalQ(cl, library(dplyr))

parLapply(cl, ls_births, summarise_births)
stopCluster(cl)
$AK
    month total
  1     1     7
  2     2    13
  ...

$AL
[1] "Error! Check data"

$AR
    month total
  1     1    28
  2     2    28
  ...
...
R 并行编程

检查来源

head(ls_births[["AL"]], n = 10)
     month plurality
263     10         1
335      9         1
473     12         1
474      6         1
475      9         1
839      9         1
1291    11     Twins
1369     4         1
1609     5         1
1610     5  Triplets
R 并行编程

future map

plan(multisession, workers = 4)
config <- furrr_options(packages = "dplyr")
future_map(ls_births, summarise_births,
           .options = config)
plan(sequential)
$AK
    month total
  1     1     7
  2     2    13
  ...

$AL
[1] "Error! Check data"

$AR
    month total
  1     1    28
  2     2    28
  ...
...
R 并行编程

foreach 案例

cl <- makeCluster(4)
registerDoParallel(cl)

foreach(df = ls_births,
        .packages = "dplyr"
       ) %dopar% {
  summarise_births(df)
}

stopCluster(cl)
$AK
    month total
  1     1     7
  2     2    13
  ...

$AL
[1] "Error! Check data"

$AR
    month total
  1     1    28
  2     2    28
  ...
...
R 并行编程

让我们来练习!

R 并行编程

Preparing Video For Download...