R代码在数百个数据帧之间循环,并根据识别号为每个帧创建和保存绘图时遇到问题

R代码在数百个数据帧之间循环,并根据识别号为每个帧创建和保存绘图时遇到问题,r,function,loops,lapply,R,Function,Loops,Lapply,通过搜索堆栈溢出,我找到了一些与我类似的问题,所以我应用了这些答案,我认为我已经非常接近实现我想要的 我根据一个标识号创建了大约400个不同的数据帧,如下所示: 现在,我需要能够循环遍历所有这些数据帧,从每个数据帧创建2个绘图,并通过标识号将这些绘图保存到文件夹中 仅生成一个图形的工作代码如下所示: # One example counts <- table(`100135`$`Score Label`, `100135`$`Question ID`) data_percentage

通过搜索堆栈溢出,我找到了一些与我类似的问题,所以我应用了这些答案,我认为我已经非常接近实现我想要的

我根据一个标识号创建了大约400个不同的数据帧,如下所示:

现在,我需要能够循环遍历所有这些数据帧,从每个数据帧创建2个绘图,并通过标识号将这些绘图保存到文件夹中

仅生成一个图形的工作代码如下所示:

# One example

counts <- table(`100135`$`Score Label`, `100135`$`Question ID`)
data_percentage <- apply(counts, 2, function(x){x*100/sum(x,na.rm=T)})

hotelname <- hotel_report$`Hotel (Q15 1)`[hotel_report$`Identification Number`=="100135"]

pdf(file = "100135.pdf", paper = "USr", width=8, height=7)
par(mar = c(5.1, 7, 4.1, 2.1))
breakdown <- barplot(data_percentage, main = "Breakdown of Property Score Distribution", sub = hotelname,
        col = coul, las = 1, cex.names = .6, horiz = TRUE, yaxs="i", xlab = "Percentage",
        cex.axis = .8, cex.lab = .8, cex.main = .8, cex.sub = .8)
dev.off()

# Make one that's the overall score alone

counts <- table(`100135`$`Score Label`)
data_percentage <- counts/sum(counts)*100

# Change names to make fit
names(data_percentage) <- c(paste("1 Extremely\n Poor" , sep=""), paste("2 Quite\n Poor" , sep=""),
                            paste("3 Slightly\n Poor" , sep=""), paste("4 Neither\n Good nor\n Poor" , sep=""),
                            paste("5 Slightly\n Good" , sep=""), paste("6 Quite\n Good" , sep=""),
                            paste("7 Extremely\n Good" , sep=""))

pdf(file = "100135_overall.pdf", paper = "USr", width=8, height=7)
overall <- barplot(data_percentage, main = "Overall Property Score Distribution", sub = hotelname, 
        col = coul, las = 1, cex.names = .65, horiz = TRUE, yaxs="i", xlab = "Percentage",
        cex.axis = .8, cex.lab = .8, cex.main = 1, cex.sub = .8)
text(0, overall, paste(round(data_percentage, 1),"%", sep =""),cex=.8,pos=4)
score <- as.numeric(hotel_report$`Overall Property Satisfaction`[hotel_report$`Identification Number`=="100135"])*100
text(15, 2,paste((score),"% Overall Score", sep = ""), cex = 1, col = "darkslategrey", font=2)
dev.off()

# Note, below is not base R to combine pdfs
library(pdftools)
pdf_combine(c("Instructions.pdf", "100135_overall.pdf", "100135.pdf"), output = "100135_final.pdf")

file.remove("100135.pdf")
file.remove("100135_overall.pdf")
有没有办法解决这个问题?理想情况下,我只需要使用base R,因为由于工作场所的限制,我无法更新R

谢谢大家!

# Create graphs in list
dflist <- list(Identification Numbers)

Make_Graphs = function(Data, name)
{

counts <- table(Data$`Score Label`, Data$`Question ID`)
data_percentage <- apply(counts, 2, function(x){x*100/sum(x,na.rm=T)})

# Create pdf of score breakdown

# For Hotel Name Subtitle
hotelname <- hotel_report$`Hotel (Q15 1)`[hotel_report$`Identification Number`==Data]

pdf(file = paste0(name, ".pdf"), paper = "USr", width=8, height=7)
par(mar = c(5.1, 7, 4.1, 2.1))
breakdown <- barplot(data_percentage, main = "Breakdown of Property Score Distribution", sub = hotelname, 
        col = coul, las = 1, cex.names = .6, horiz = TRUE, yaxs="i", xlab = "Percentage",
        cex.axis = .8, cex.lab = .8, cex.main = .8, cex.sub = .8)
dev.off()

# Make one that's the overall score alone

counts <- table(Data$`Score Label`)
data_percentage <- counts/sum(counts)*100

# Change names to make fit
names(data_percentage) <- c(paste("1 Extremely\n Poor" , sep=""), paste("2 Quite\n Poor" , sep=""),
                            paste("3 Slightly\n Poor" , sep=""), paste("4 Neither\n Good nor\n Poor" , sep=""),
                            paste("5 Slightly\n Good" , sep=""), paste("6 Quite\n Good" , sep=""),
                            paste("7 Extremely\n Good" , sep=""))

# Make the pdf of the overall score
pdf(file = paste0(name, "_overall.pdf"), paper = "USr", width=8, height=7)
overall <- barplot(data_percentage, main = "Overall Property Score Distribution", sub = hotelname, 
        col = coul, las = 1, cex.names = .65, horiz = TRUE, yaxs="i", xlab = "Percentage",
        cex.axis = .8, cex.lab = .8, cex.main = 1, cex.sub = .8)
text(0, overall, paste(round(data_percentage, 1),"%", sep =""),cex=.8,pos=4)
score <- as.numeric(hotel_report$`Overall Property Satisfaction`[hotel_report$`Identification Number`==Data])*100
text(15, 2,paste((score),"% Overall Score", sep = ""), cex = 1, col = "darkslategrey", font=2)
dev.off()

# Note, below is not base R to combine pdfs
library(pdftools)
pdf_combine(c("Instructions.pdf", paste0(name, "_overall.pdf"), paste0(name, ".pdf")), output = paste0(name, "_final.pdf"))

file.remove(paste0(Data, ".pdf"))
file.remove(paste0(Data, "_overall.pdf"))

}

plots <- lapply(dflist, Make_Graphs, names(dflist))
Error in plot.window(xlim, ylim, log = log, ...) : 
  need finite 'xlim' values
In addition: Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(w.l) : no non-missing arguments to min; returning Inf
4: In max(w.r) :
 Error in plot.window(xlim, ylim, log = log, ...) : 
  need finite 'xlim' values