r语言 - 打印从 SQLite blob 中提取的绘图时出错



我有一个工作流程,我需要在R中生成一些数据,为数据生成绘图,保存绘图,然后稍后渲染它们。我为此使用 SQLite。它工作正常,但仅适用于ggplot2情节。当我尝试保存并重新渲染基本 R 图时,它不起作用。有什么想法吗?使用 R 版本 3.3.0。这是我的代码:

library("ggplot2")
library("RSQLite")
# test data
dat <- data.frame(x = rnorm(50, 1, 6), y = rnorm(50, 1, 8))
# make ggplot
g <- ggplot(dat, aes(x = x, y = y)) + geom_point()
# make base plot
pdf("test.pdf") # need open graphics device to record plot headlessly
dev.control(displaylist="enable")
plot(dat)
p <- serialize(recordPlot(), NULL)
dev.off()
# make data frame for db insertion
df1 <- data.frame(baseplot = I(list(p)), ggplot = I(list(serialize(g, NULL))))
# setup db
con <- dbConnect(SQLite(), ":memory:")
dbGetQuery(con, 'create table graphs (baseplot blob, ggplot blob)')
# insert the data
dbGetPreparedQuery(con, 'insert into graphs (baseplot, ggplot) values (:baseplot, :ggplot)', 
bind.data=df1)
# get the data back out
df2 <- dbGetQuery(con, "select * from graphs")
# print the ggplot; not sure why I need 'lapply' for this to work...
lapply(df2[["ggplot"]][1], "unserialize")
# print the base plot
lapply(df2[["baseplot"]][1], "unserialize")
# Error: NULL value passed as symbol address

记录我的发现的部分答案:我不能说为什么会发生这种情况,但这与sqlite无关。序列化绘图后立即发生相同的错误:

dat <- data.frame(x = rnorm(50, 1, 6), y = rnorm(50, 1, 8))
pdf("test.pdf") # need open graphics device to record plot headlessly
dev.control(displaylist="enable")
plot(dat)
p <- serialize(recordPlot(), NULL)
dev.off()
unserialize(p) # !! Error: NULL value passed as symbol address

最新更新