Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error printing plot extracted from SQLite blob

Tags:

sqlite

r

ggplot2

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
like image 659
user5359531 Avatar asked May 26 '18 01:05

user5359531


1 Answers

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
like image 114
Ralf Stubner Avatar answered Oct 13 '22 01:10

Ralf Stubner