Can I save my R output (.jpeg) to an FTP server?

夙愿已清 提交于 2020-08-08 13:51:08

问题


(optional read) Greater Objective: PowerBI Web doesn't support a few R packages when published on the internet. It throws the below error ("Missing R Package"). Hence, I am working towards saving the output from R as an image (.jpeg) to a remote location (such as FTP) or cloud storage (secure and open source) and then import it to PowerBI. This workaround might resolve the package conflict (hoping).

Specific Objective*: The below code illustrates a trivial way of saving an R output(.jpeg) image locally. However, is there a way to save the image directly to the FTP server, provided I have the username/password etc? (unfortunately, I cannot share the server details)

library(outbreaks)
library(incidence)

cases = subset(nipah_malaysia, select = c("perak", "negeri_sembilan", "selangor",
                                           "singapore"))

i = as.incidence(cases, dates = nipah_malaysia$date, interval = 7L)
jpeg(file = "plot.jpeg")
plot(i)
dev.off()

I did come across this post on employing ftpUpload function from the "rcurl" package. However, to upload it to FTP, I might still need to save it locally which defeats my purpose in this use-case.

Any suggestions would be helpful.


回答1:


If saving a temporary file (as you suggested in a comment) is an option, then you can do that with the following code:

library(outbreaks)
library(incidence)
library(RCurl)

cases = subset(nipah_malaysia, select = c("perak", "negeri_sembilan", "selangor",
                                           "singapore"))

i = as.incidence(cases, dates = nipah_malaysia$date, interval = 7L)
jpeg(file = filename <- tempfile())
plot(i)
dev.off()
ftpUpload(filename, "ftp://User:Password@FTPServer/destfile.jpeg")



回答2:


If you're ok with having the output in PNG format (EDIT: I updated the code to show output to JPEG format) try the code below, with chunks borrowed from this answer that discusses how to save an image in memory:

EDIT: Updated to output to jpeg format

library(outbreaks)
library(incidence)

cases = subset(nipah_malaysia, select = c("perak", "negeri_sembilan", "selangor",
                                          "singapore"))

orig_i = as.incidence(cases, dates = nipah_malaysia$date, interval = 7L)
plot(orig_i)

#### This section adapted from
#### https://stackoverflow.com/questions/7171523/in-r-how-to-plot-into-a-memory-buffer-instead-of-a-fileinstead-of-a-file
#### loads image data to memory rather than a file

library(Cairo)
library(png)
library(ggplot2)

Cairo(file='/dev/null')

plot(orig_i) #your plot

# hidden stuff in Cairo
i = Cairo:::.image(dev.cur())
r = Cairo:::.ptr.to.raw(i$ref, 0, i$width * i$height * 4)

dev.off()

dim(r) = c(4, i$width, i$height) # RGBA planes
# have to swap the red & blue components for some reason
r[c(1,3),,] = r[c(3,1),,]

# now use the jpeg library to write the raw vector
library(jpeg)
p = writeJPEG(r, raw()) # raw JPEG bytes

#DEBUGGING - check that this actually works
#Note: Windows 10 has an error that might report this as a file system error
#In windows, drag and drop the file into an open chrome window to see the image
writeBin(p, con= "yourpathhere/check_output.jpg")


#adapted code from @tfehring's example for the updload
library(RCurl)
ftpUpload(p, "ftp://User:Password@FTPServer/destfile.jpg")


来源:https://stackoverflow.com/questions/62775180/can-i-save-my-r-output-jpeg-to-an-ftp-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!