尝试使用knitr导出为pdf,需要很长时间才能完成

时间:2015-10-26 19:51:09

标签: r knitr

我有这个数据框。它是我的实际数据框的一个非常小的子集,但列名称的所有其他内容都是相同的:

这是包含数据的数据框:

dput(p)

structure(list(Hostname = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("server101", 
"server102", "server103", "server104", "test101", "app101d", 
"web25", "web26", "web111", "web11", "web123", "tomcat101", "tomcat103", 
"tomcat104"), class = "factor"), Date = structure(c(1441373431, 
1441372531, 1441737938, 1441337431, 1441374331, 1441367131), class = c("POSIXct", 
"POSIXt"), tzone = ""), Cpubusy = c(22, 21, 20, 28, 22, 20), 
    UsedPercentMemory = c(3L, 3L, 21L, 3L, 3L, 4L), App = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("WEB", "DB"), class = "factor"), 
    HA = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("server3456", 
    "backup101", "ha123", "No HA Host", "No HA Host", "server120", 
    "server234", "server666", "No HA Host"), class = "factor"), 
    DR = structure(c(5L, 5L, 5L, 5L, 5L, 5L), .Label = c("Cannot login to DR Host", 
    "dr101", "dr345", "dr444", "dr5678", "No DR Host", "drserver11", 
    "dr666", "No HA Host", "No HA Host", "No HA Host"), class = "factor")), .Names = c("Hostname", 
"Date", "Cpubusy", "UsedPercentMemory", "App", "HA", "DR"), row.names = c(NA, 
6L), class = "data.frame")

这是我的applition服务器映射数据框:

dput(c)
structure(list(App = structure(c(1L, 2L, 2L, 2L, 2L, 1L), .Label = c("WEB", 
"APP"), class = "factor"), Prod = structure(c(1L, 5L, 6L, 4L, 
3L, 2L), .Label = c("server101", "server102", "server104", "server106", 
"server107", "server109"), class = "factor"), HA = structure(c(1L, 
3L, 3L, 3L, 3L, 2L), .Label = c("server3456", "server3456", "No HA Host"
), class = "factor"), DR = structure(c(1L, 2L, 2L, 2L, 2L, 3L
), .Label = c("dr5678", "No DR Host", "No DR Host"), class = "factor")), .Names = c("App", 
"Prod", "HA", "DR"), row.names = c(NA, 6L), class = "data.frame")

这只是一小部分数据。我正在尝试运行knitr将我的分析导出到pdf文件,如下所示:

application<-unique(c$App, drop=TRUE)
application<-droplevels(application)

metrics<-unique(colnames(pp[,c(3:4)]))
env<-unique(colnames(c[,c(2:4)]))

我在knitr中有这个块来在每个部分创建图表:

```{r qplot,fig.width=10, fig.height=8, message=FALSE, results = 'asis', echo=FALSE, warning=FALSE, fig.cap="long caption", fig.scap="short"}


library(ggplot2)
library(knitr)



for (product in application){ 
  product<-gsub("\\(", "_",product)
  product<-gsub("\\)", "_",product)
  for(en in env){

     tryCatch({
        mergedData <- merge(pp, c, by.x=c("Hostname"),by.y=en)
        cat(paste(product, en, " - Environment"))
        cat("\n")

        for(m in metrics){

            p<-ggplot(mergedData,aes(Date, m, group=Hostname, colour=Hostname))+geom_line()+
              geom_smooth(method="lm", se=T, colour="blue")+
              facet_wrap(~Hostname)+theme_bw()+
              ggtitle(m)+
              theme(strip.background = element_rect(colour="blue", fill="#b2d8ff"),axis.text.x = element_text(angle = 30,hjust = 1),plot.background = element_rect(size = 1, colour="black",linetype = "solid"))+
              scale_fill_brewer(palette="RdYlGn")
              print(p)
              cat("\n")
       }
     },error=function(e) {
        print(paste(product, "does not have - ", en, "- Environment"))
       print("\n")
     })

  }

}

```

运行此操作需要很长时间,任何想法如何优化它以便在短时间内运行?

0 个答案:

没有答案