I have a workflow where I need to generate some data in R, generate plots for the data, save the plots, then render them later. I am using SQLite for this. It works fine, but only for ggplot2
plots. When I try to save and re-render base R plots, it does not work. Any ideas? Using R version 3.3.0. Here is my code:
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
Partial answer to record my findings: I cannot say why this is happening, but this is not related to sqlite
. The same error occurs right after serializing the plot:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With