2

(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).

enter image description here

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.

  • Is there a way to use `tempfile` and then push it to FTP, instead of saving the plot locally? – pradeepvaranasi Jul 08 '20 at 06:54
  • 1
    I generally discourage FTP in favor of SSH (which can provide a ftp-like interface). You can use the [`ssh`](https://cran.r-project.org/web/packages/ssh/index.html) package to `scp_upload` a file. – r2evans Jul 12 '20 at 03:13
  • Any other recommendations where I can temporarily save the image on remote or cloud-based storage and import it to PowerBI? – pradeepvaranasi Jul 12 '20 at 07:35
  • Have you considered using https://pastebin.com/? @hrbrmstr has an R package for it, aptly named [`pastebin`](https://github.com/hrbrmstr/pastebin), I believe it is not yet on CRAN. (Caveat: I've never used it, and it's been a few years since he's pushed to that repo, not sure if it's maintained/functioning. I've done no testing.) – r2evans Jul 12 '20 at 18:56
  • I suppose it is limited to text. [Ref.](https://whatis.techtarget.com/definition/pastebin) – pradeepvaranasi Jul 13 '20 at 04:52
  • You can serialize, but you're right. Perhaps imgur is useful? – r2evans Jul 13 '20 at 11:47

2 Answers2

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")
tfehring
  • 394
  • 2
  • 6
  • Correct. However, I learnt that 'tempfile' is eventually stored locally. If the code that imports an image runs on PowerBI web, it won't be able to navigate to local storage. – pradeepvaranasi Jul 12 '20 at 07:39
0

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")
Roger-123
  • 2,232
  • 1
  • 13
  • 33