r - 从 SQLite blob 中提取的错误打印图

标签 r sqlite ggplot2

我有一个工作流程,我需要在 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

关于r - 从 SQLite blob 中提取的错误打印图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50538656/

相关文章:

r - 使用汽车包装中的散点图并排绘制

c - 我应该如何处理 sqlite 错误?

r - 在 ggplot2 中混合 facet_grid() 和 facet_wrap()

r - 用于准确预测的数据集中的最小行数

r - 在 plotly 中将箭头段添加到散点图

java - 如何在后台线程中正确执行SQL查询?

SQLite 选择两列(带空格)作为一列

r - ggplot2:只使用图例中的颜色(没有符号用于绘图)

r - 防止 spData::world 国家环绕 map /裁剪 map 区域

r - 按公共(public)值组合 R 中的透视行