1

我在无头 linux 服务器上有 R,但我对文件系统没有写权限。这迫使我想将我的图片保存到数据库(在这种情况下为 PostgreSQL)。

我已经四处寻找解决方案,但我发现的每个解决方案通常都会先保存到文件中,然后将文件作为字节读回 R,然后将字节存储在数据库中。

这篇 10 年前的 stackoverflow 帖子指出,所有图形设备都是基于文件的。从那以后,R 是否发生了变化,这将允许我获取字节? 如何将 R 绘图图像保存到数据库?

这个解决方案看起来很有趣,但它仍然需要写权限: 如何在 R 中直接显示路径视图图(而不是另存为文件)?

我有尝试使用 text.connection() 或 capture.output() 的想法,希望它创建一个包含图像字节的文本向量。

这是我厌倦的两件事:

logmodel_solar <- glm(hasfire ~ cs_rh_min + cs_air_max_temp + cs_precip + cs_solar, data=df, family = binomial("logit"))
zz <- textConnection("foo", "w")
sink(zz)
plot(logmodel_solar)
sink()
close(zz)

这会使 R 崩溃或给我这个

> cat(zz, sep = "\n")
3

所以我尝试了这个:

logmodel_solar <- glm(hasfire ~ cs_rh_min + cs_air_max_temp + cs_precip + cs_solar, data=df, family = binomial("logit"))
yy <- capture.output(plot(logmodel_solar))

>  cat(yy, sep = "\n")

> yy
character(0)

所以我想我的问题是:

  1. R是否仍然只将图形输出到文件
  2. 如果不是,我如何获取字节
4

1 回答 1

0

我从一位同事那里得到了答案,他说这需要几个月的时间才能解决。它不直观,但它有效。

# https://www.rdocumentation.org/packages/Cairo/versions/1.5-12.2/topics/Cairo
# https://www.rdocumentation.org/packages/cairoDevice/versions/2.28.2
# An intermediate drawing device that can output to buffer but allows normal graphing commands to work
require(cairoDevice)

# https://www.rdocumentation.org/packages/RGtk2/versions/2.8.7
# Allows us to interact with the GTK buffer so we can get the bytes rather than saving to disc
require(RGtk2)

########## here we read in the data and run a logistic regression model
pg = dbDriver("PostgreSQL")
con = dbConnect(pg, user="postgres", password="password",
                host="localhost", port=5432, dbname="fire")

query_statement <- paste("select hasfire, date_time, cs_precip, cs_air_max_temp,",
                                       "cs_air_min_temp, cs_soil_max_temp, cs_soil_min_temp, cs_solar, cs_eto, cs_rh_max, cs_rh_min, hasfire",
                                       "from final.analysis")
df <- dbGetQuery(con, query_statement)

logmodel_solar <- glm(hasfire ~ cs_rh_min + cs_air_max_temp + cs_precip + cs_solar, data=df, family = binomial("logit"))

################## 现在我们从模型中绘制一个图形

# Define the size in pixels and bits per pixel of the image we want.
# We create a Gtk2 pixmap with these parameter
# In this case we are making a 500 pixel x 500 pixel image with 24 bits per pixel
# https://www.rdocumentation.org/packages/RGtk2/versions/2.8.5/topics/gdkPixmapNew
# https://developer.gnome.org/pygtk/stable/class-gdkpixmap.html
pixmap <- gdkPixmapNew(w=500, h=500, depth=24)

# Now we convert the pixmap to a Cairo graphics device. After that we can use the
# Cairo device like any normal R graphic device (i.e. R plot commands draw to it)
# Cairo https://www.rdocumentation.org/packages/Cairo/versions/1.5-12.2/topics/Cairo
# https://www.rdocumentation.org/packages/cairoDevice/versions/2.28.2/topics/asCairoDevice
asCairoDevice(pixmap)

#Normal plot command
plot(logmodel_solar)

# Convert out image of the plot to an RGB(A) representation in another buffer
# Since we want to go to a buffer the first parameter, dest, is null
# Next we give pixmap as the source and then we pass in the colormap from pixmap
# The first two 0s setting the origin of the image
# The next two are set to 0 because our destination is null
# The next two parameters are the width and height respectively to get from the image
# So if we wanted to subset our plot we could set different origin coordinates and smaller dimensions to get
# RGB(A) https://en.wikipedia.org/wiki/RGBA_color_model
# https://www.rdocumentation.org/packages/RGtk2/versions/2.20.31/topics/gdkPixbufGetFromDrawable
plot_pixbuf <- gdkPixbufGetFromDrawable(NULL, pixmap,pixmap$getColormap(),0, 0, 0, 0, 500, 500)

# Now we convert plot_pixbuf above to a binary object that is in the format of the image we want.
# first we pass in the PixBuffer from above, then we choose our conversion format
# Values for the format are currently "jpeg", "tiff", "png", "ico" or "bmp"
# For statistical graphs you should try to use a lossless format such as tiff or png
# The next two parameters set the option_keys and option_values respectively.
# We are not setting any so we just pass in a 0 length character vector.
# The $buffer on the end tells R we want the buffer attribute from the converted object
# https://developer.gnome.org/gdk-pixbuf/stable/gdk-pixbuf-File-saving.html
# https://www.rdocumentation.org/packages/RGtk2/versions/2.20.31/topics/gdkPixbufSaveToBufferv
buffer <- gdkPixbufSaveToBufferv(plot_pixbuf, "png",character(0),character(0))$buffer

# Now we return out buffer
return(buffer)
于 2020-09-24T00:10:44.113 回答