在r中循环tempfile()

在r中循环tempfile(),r,ggplot2,base64,temporary-files,R,Ggplot2,Base64,Temporary Files,我不知道为什么这是错误的。我基本上是在尝试使用ggplot在HTML模板上创建一个进度条。为此,我需要创建一个for循环,并将其存储在r中的temp文件夹中。然后将png文件编码为base64格式,这样我就可以将其放在HTML图像标记上 library(catools) a <- seq(1:10) 1:length(a) for (i in 1:length(a)){ total_questions <- 8 current_question<- i+1 group

我不知道为什么这是错误的。我基本上是在尝试使用ggplot在HTML模板上创建一个进度条。为此,我需要创建一个for循环,并将其存储在r中的temp文件夹中。然后将png文件编码为base64格式,这样我就可以将其放在HTML图像标记上

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
}
库(catools)

a您可以使用
ggsave
。它可以保存任何ggplot2绘图,默认为最后一个绘图。它会根据提供的文件路径的扩展名自动检测要写入的文件类型(此处为.png)

此外,手动设置大小(宽度和高度)可以让您更可靠地保存打印(否则,打印大小是您当前未设置的png设备的大小,默认为打印屏幕IIRC的当前大小)


tec所以这对我来说很有效。我能够创建每个绘图并将其保存到一个临时文件中,然后继续获取base64代码,并随后将其链接到html中的图像标记。因此,每当人们点击下一个问题时,新网页中的条形图将增加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
}
require(网格)
需要(ggplot2)

a不确定您的问题是什么,但使用
ggsave
肯定可以省去故障排除
p当我将
base64Encode
替换为
base64Encode
(在Windows上)时,它对我来说效果很好。@antoine sac效果很好。非常感谢。所以,把它写下来并做正确的标记。
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
}