首页 > 解决方案 > 为什么future::future() 不能与闪亮的dbAppendTable 一起使用?

问题描述

您可以使用 future() 来获得它的副作用,例如写一个文件:

library(promises)
library(future)
plan(multiprocess)
future({write.csv(mtcars,"mtcars.csv")})

但你不能使用数据库调用:

library(RSQLite)
library(promises)
library(future)
plan(multiprocess)
con <- dbConnect(RSQLite::SQLite(), ":memory:")
future({
dbCreateTable(con, "iris", iris)
})
dbReadTable(con, "iris") # gives error

(无论是持久写入还是在内存中都不会创建表。)

标签: rshinypromisefuture

解决方案


DBI包及其所有实现(如RSQLite)确实使用不可导出的对象(基本上是内存指针),请参阅:

https://cran.r-project.org/web/packages/future/vignettes/future-4-non-exportable-objects.html

当您futures像这样配置时,您会看到一条错误消息:

options(future.globals.onReference = "error")
# ... your code goes here

# Error in FALSE : 
# Detected a non-exportable reference (‘externalptr’) in one of the globals (‘con’ of class ‘SQLiteConnection’) used in the future expression
# Timing stopped at: 0.028 0 0.028

推荐阅读