简体   繁体   中英

looping tempfile() in r

I am not sure why is this wrong. I am basically trying to create a progress bar on an HTML template using ggplot. To do this I need to create a for loop and store it in the temp folder in r. The png file is than coded into a base64 format so that I can put it onto an HTML image tag.

library(catools)

a <- seq(1:10)
1:length(a)

for (i in 1:length(a)){

total_questions <- 8
current_question<- i+1

group <- rbind("Total Questions", "Current question")
progress <- rbind(total_questions, current_question)
colnames(progress)<- "Progress"
progress <- as.data.frame(progress)
progress_bar <- cbind(group,progress)


# save example plot to file

png(tec <- tempfile(paste0("image",i), fileext = ".png"))
ggplot(progress_bar, aes(group, Progress,fill=group), height=1000, width=800) + geom_histogram(stat="identity") 
dev.off()
# Base64-encode file
txt <- base64Encode(readBin(tec, "raw", file.info(tec)[1, "size"]), "txt")
txt
}

This is my error.

Error in file(con, "rb") : cannot open the connection In addition: Warning message: In file(con, "rb") : cannot open file '/var/folders/w0/lrdx2zvn2hgf_ds0f92hpy500000gn/T//RtmpWD4Ysl/image114459476144.png': No such file or directory

You could use ggsave instead. It can save any ggplot2 plot, defaulting to the last one. It automatically detects what type of file to write (here a .png) based on the extension of the provided filepath.

Also, setting the size (width and height) manually allow you to save your plots more reliably (otherwise, the plot size is the size of the png device which you currently do not set and which would default to the current size of the plotting screen IIRC).

tec <- tempfile(paste0("image",i), fileext = ".png")
p <- ggplot( // your plot here // ) 
ggsave(tec, p, width=5, height=5)

Choose your size carefully as it has a big impact on the font size. If your en-usage necessitates a 5x5 image, then saving to 10x10 will result in text twice as small after cropping. If you need a 10x10 image, saving to 5x5 will be ugly.

If you are developping some kind of software for which you need a progress bar image, you may want to save the image as pdf so that your images look good at any size.

So this worked well for me. I was able to create and save each plot into a temp file, and then went on to get the base64 code and subsequently link it to an image tag in html. So each time the person click onto the next question, the bar graph in the new webpage will increase by i.

require(grid) 
require(ggplot2)

a <- seq(1:10)
for(i in 1:length(a)){
total_questions <- 10
current_question<- i

group <- rbind("Total Questions", "Current question")
progress <- rbind(total_questions, current_question)
colnames(progress) <- "Progress"
progress <- as.data.frame(progress)
progress_bar <- cbind(group,progress)

# save example plot to file
png(tec <- tempfile(fileext = ".png"), height=200, width=300)
p <- ggplot(progress_bar, aes(group, Progress,fill=group)) +     geom_histogram(stat="identity") + coord_flip() + xlab("") + ylab("") + theme(legend.position="none") 
gt <- ggplot_gtable(ggplot_build(p))

# plot table without spacing. 
ge <- subset(gt$layout, name == "panel")
grid <- grid.draw(gt[ge$t:ge$b, ge$l:ge$r])
dev.off()

# Base64-encode file
library(RCurl)
txt <- base64Encode(readBin(tec, "raw", file.info(tec)[1, "size"]), "txt")
txt
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM