如何使用for循环从多个文件r创建多个数据帧

时间:2017-11-16 21:07:23

标签: r loops markdown r-markdown flexdashboard

我有大约150张桌子,我希望制作尽可能多的flexdashboards,但不想手动编织。我在r脚本中使用for循环,希望将这些rmd文件呈现为.html

setwd("/Volumes/G-DRIVE mobile USB/r/tables 7")
file_list <- list.files()

library(rmarkdown)

for (file in file_list){
  render("template.Rmd",output_file = paste0('report.', file, '.html'))    
}

&#34; template.Rmd&#34;文件是我的flexdashboard创建的地方,但这是我卡住的地方。我应该将什么作为数据帧的最大值名称(____ $ var1)?还是我错过了别的什么?我尝试了一些事情,但没有什么能真正发挥作用。有些会生成所有.html文件,并且仪表板布局就在那里,但没有绘制任何内容。

---
title: "Example Dasboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
---

```{r setup, include=FALSE}
library(ggplot2)
library(plotly)
library(plyr)
library(flexdashboard)
library(RCurl)
library(ggthemes)


df<- data.frame(Name = (file),
                   Date = as.Date(file$date, format = "%m/%d/%Y"),
                   var1 = (file$var1),
                   var2 = (file$var2), 
                   var3 = (file$var3),
                   var3a = (file$var3/((file$var4/2.204)^0.66)),
                   var4 = (file$var4),
                   var5 = (file$var5Total),
                   var6 = (file$var6))


```

Objective Measures
=======================================================================

Row
-----------------------------------------------------------------------

### Var3
```{r}
p<-ggplot(df[which(df$Date>"2017-8-1"),],aes(Date,var3))+geom_point()+stat_smooth(color="red", fill="black")+
    theme(axis.title.x = element_blank()) + ylab("")+xlab("")+
    scale_x_date(date_breaks = "months", date_labels = "%b%y")+theme_hc()
ggplotly(p)
```

### var1
 ```{r}
p<-ggplot(df[which(df$Date>"2017-8-1"),],aes(Date,var1))+geom_point()+stat_smooth(color="red", fill="black")+
    theme(axis.title.x = element_blank()) + ylab("")+xlab("")+ 
    scale_x_date(date_breaks = "months", date_labels = "%b%y")+theme_hc()
ggplotly(p)
```

Row
-----------------------------------------------------------------------

### Var4

```{r}
p<-ggplot(df[which(df$Date>"2017-8-1"),],aes(Date,var4))+geom_point()+stat_smooth(color="red", fill="black")+
    theme(axis.title.x = element_blank()) + ylab("")+xlab("")+  scale_x_date(date_breaks = "months", date_labels = "%b%y")+theme_hc()
ggplotly(p)
```

### Var2
```{r}
p<-ggplot(df[which(df$Date>"2017-8-1"),],aes(Date,var2))+geom_point()+stat_smooth(color="red", fill="black")+
    theme(axis.title.x = element_blank()) + ylab("")+xlab("")+  
    scale_x_date(date_breaks = "months", date_labels = "%b%y")+theme_hc()
ggplotly(p)
```


### Var3a
```{r}
p<-ggplot(df[which(df$Date>"2017-8-1"),],aes(Date,var3a))+geom_point()+stat_smooth(color="red", fill="black")+
    theme(axis.title.x = element_blank()) + ylab("")+xlab("")+ 
    scale_x_date(date_breaks = "months", date_labels = "%b%y")+theme_hc()
ggplotly(p)
```

Row
-----------------------------------------------------------------------

### Var5
```{r}
p<-ggplot(df[which(df$Date>"2017-8-1"),], aes(x=Date, y=var5)) +
    geom_bar(stat="identity", fill = "orange") +xlab("") +ylab("") + scale_x_date(date_breaks = "months", date_labels = "%b%y")+theme_hc()
ggplotly(p)
```




Subjective Measures
=======================================================================

Row
-----------------------------------------------------------------------

### Var6
```{r}
p<-ggplot(df[which(df$Date>"2017-8-1"),], aes(x=Date, y=var6)) +
    geom_bar(stat="identity", fill = "green")+ylim(0,10) +xlab("")+ylab("")+ scale_x_date(date_breaks = "months", date_labels = "%b%y")+theme_hc()
ggplotly(p)
```

0 个答案:

没有答案