我有一个工作流程,我需要在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
答案 0 :(得分:1)
记录我的发现的部分答案:我不能说为什么会发生这种情况,但这与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