(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.
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")
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")
Related
I'm creating a script to cluster my data in a server. I need to save the text output and the images as well. The text output works just fine but when I try to use the png() + plot() + dev.off() thing to save the plots, no image is created.
[ADDED FOR CLARIFICATION]
What I need to do is to SAVE the plot (i.e. generate an image file) in running mode. If I run the code step by step, the file is created.
I already tried to change the image format to PDF and JPG using the corresponding functions but I'm still getting no images as output when running the code as script. When stepping, it works great.
Since it takes a little while for R to render the image when I'm running step by step, I tried to add Sys.sleep(2) in between commands (code below) but nothing changed.
I think the problem might be related to the package that I'm using and the type of object it generates (library(NMF)). I looked at the documentation to see if there was something about the way the plot() function works with the type of object that the clustering algorithm generates but the text is vague:
"The result (of estim.r <- nmf(esGolub, 2:6, nrun=10, seed=123456) for example) is a S3 object of class NMF.rank, that contains a data.frame with the quality measures in column, and the values of r in row. It also contains a list of the consensus matrix for
each value of r".
"All the measures can be plotted at once with the method plot (Figure 1), and the function consensusmap generates heatmaps of the consensus matrix for each value of the rank".
There is another type of image that can be generated after the clustering runs: the consensusmap. This one works on both cases (stepping and running).
The script is pretty short. Here it is:
library(NMF)
data = read.csv('R.csv', header=TRUE, sep=";")
res1 <- nmf(data, rank=2:5, nrun=1, "brunet", "random")
# this always works
capture.output(summary(res1) ,file = "summary.txt", append = TRUE)
# this always works too
png(filename = 'consensus.png', width = 1366, height = 768, units = 'px')
consensusmap(res1)
dev.off()
# this does not work on 'running mode', only 'stepping mode'
png(filename = 'metrics.png', width = 1366, height = 768, units = 'px')
# added hoping it would fix the issue. It didn't
Sys.sleep(2)
plot(res1)
# added hoping it would fix the issue. It didn't
Sys.sleep(2)
dev.off()
The summary.txt file is generated, the consensus.png too. The metrics.png is not. What's going on here??
So far I created several pngs in R and used ani.options and im.convert from the animation package to create a gif animation (ImageMagick is installed on Windows). It works without any problems:
`ani.options(nmax = 100, loop = 1, interval = 0.1)
for(i in 1:100){
name = rename(i)
png(name)
plot(...)
dev.off()
}
im.convert("*.png", output ="animation.gif", convert = c("convert"),
cmd.fun = if (.Platform$OS.type == "windows") shell else system,
extra.opts = "",clean = TRUE)`
Insead of png-files I would like to convert pdfs to a gif animation. Again I generated and saved several pdfs in a for-loop without any problems. The challenge is now to convert these pdfs to a gif-animation. I tried different approaches but I can't figure out how to modify the im.convert command and how to set ani.options parameters to combine the pdfs in a gif-animation.
So far, I tried setting ani.type and ani.dev to pdf and changed ".png" in im.convert to ".pdf".
I am gratefull for any suggestions.
I found the answer here:
Error using magick R to import PDF
After installing the 64-bit version of GhostScript it worked.
I'm using DeployR server 8.0.5 as R API. I have R script deployed on that server which is using library : ggplot2. From what i know ggplot will store the plot on server only if i call
save(p, file = "plot.rdata")
or
ggsave("plot.png", width = 5, height = 5)
Looks like that print() function stores the svg into R database,here is an example how i'm generating SVG :
File_Name <- "MyPlot.svg"
myfile <- paste(File_Name)
svg(myfile,width=5, height=5, pointsize = 12)
print(My_Plot) #contains ggplot() result
dev.off()
result$plot <- myfile
return(result)
The problem is that the DeployR database is going huge. Looks like every response from DeployR is stored in db as byte[] in table : file_content .. i have a lot of request and respectively my DeployR database is going huge.
One possible solution is to clear the db manually from time to time , but in general i want to change the behaviour . I don't understand why result is stored in database ? I just want to return the result , no need of storing the data.
So what i found is gridSVG library ,but i'm not sure how to use it in my case ,i'm not able to find proper example? Also find one more library : svglite , but again i'm not able to use it in my case.
I'm using a kml package of R to cluster my data and I need to get in the end a csv file with a column including the number of clusters according to each id. The data has many missing values, so I can't use kmeans function without deleting all observations, but kml works nicely with that. My problem is that I use choice() to export the results and all I get is a graphical window, but no output files. Here is my code:
setwd("/Volumes/NATASHKA/api/R files")
statadata <-read.dta("Data_wide_withdemogr_auris_for_kml_negative.dta")
mydata <- data.frame(statadata)
cldDQ <- cld(mydata)
kml(cldDQ,c(2:6),20,toPlot="none")
plotAllCriterion(cldDQ)
par(mar = rep(2, 4))
X11(type = "Xlib")
choice(cldDQ, typeGraph = "bmp")
What do I do wrong?
I had the same problem and I solved it that way:
first, you need to choose the desired partition with the arrow
second, select it pressing “space”,
then press “Enter” and you can find all files in your work directory, check getwd().
Good luck.
I see that many examples for downloading binary files with RCurl are like such:
library("RCurl")
curl = getCurlHandle()
bfile=getBinaryURL (
"http://www.example.com/bfile.zip",
curl= curl,
progressfunction = function(down, up) {print(down)}, noprogress = FALSE
)
writeBin(bfile, "bfile.zip")
rm(curl, bfile)
If the download is very large, I suppose it would be better writing it concurrently to the storage medium, instead of fetching all in memory.
In RCurl documentation there are some examples to get files by chunks and manipulate them as they are downloaded, but they seem all referred to text chunks.
Can you give a working example?
UPDATE
A user suggests using the R native download file with mode = 'wb' option for binary files.
In many cases the native function is a viable alternative, but there are a number of use-cases where this native function does not fit (https, cookies, forms etc.) and this is the reason why RCurl exists.
This is the working example:
library(RCurl)
#
f = CFILE("bfile.zip", mode="wb")
curlPerform(url = "http://www.example.com/bfile.zip", writedata = f#ref)
close(f)
It will download straight to file. The returned value will be (instead of the downloaded data) the status of the request (0, if no errors occur).
Mention to CFILE is a bit terse on RCurl manual. Hopefully in the future it will include more details/examples.
For your convenience the same code is packaged as a function (and with a progress bar):
bdown=function(url, file){
library('RCurl')
f = CFILE(file, mode="wb")
a = curlPerform(url = url, writedata = f#ref, noprogress=FALSE)
close(f)
return(a)
}
## ...and now just give remote and local paths
ret = bdown("http://www.example.com/bfile.zip", "path/to/bfile.zip")
um.. use mode = 'wb' :) ..run this and follow along w/ my comments.
# create a temporary file and a temporary directory on your local disk
tf <- tempfile()
td <- tempdir()
# run the download file function, download as binary.. save the result to the temporary file
download.file(
"http://sourceforge.net/projects/peazip/files/4.8/peazip_portable-4.8.WINDOWS.zip/download",
tf ,
mode = 'wb'
)
# unzip the files to the temporary directory
files <- unzip( tf , exdir = td )
# here are your files
files