我如何在多个文件夹上运行脚本

问题描述 投票:1回答:1

我开发了一个脚本,可以对温度,降水量文件和预报进行操作和图形处理。我有3个工作站,我将必须执行相同的步骤,这是脚本的一部分,该脚本会更改文本文件名和CSV中的工作站名称。我的问题是如何在3个文件夹上运行脚本并保存每个电台同时显示每个号码

该脚本:

library(tidyr)
library(readxl)
library(dplyr)
library(lubridate)
library(frost)
setwd("C:/Users/majd/Documents/laval")
REF_CNRM <- read.table("**LAVAL_CNRM_Aladin_REF_1950-2005.txt**",header=TRUE,dec=".",sep=" ", encoding="UTF-8")
summary(REF_CNRM)

colnames(REF_CNRM)[1] <-"date"
colnames(REF_CNRM)[4] <-"Tasmin"
colnames(REF_CNRM)[5] <-"Tasmax"
colnames(REF_CNRM)[6] <-"Pre"
colnames(REF_CNRM)[7] <-"Neige"


REF_CNRM$date <- as.Date(as.character(REF_CNRM$date), format = "%Y%m%d")
REF_CNRM$year <- year(ymd(REF_CNRM$date))
REF_CNRM$month <- month(ymd(REF_CNRM$date)) 
REF_CNRM$day <- day(ymd(REF_CNRM$date))
REF_CNRM<- REF_CNRM[,c(8,9,10,1,2,3,4,5,6,7)]
REF_CNRM <- REF_CNRM[,-4]

REF_CNRM = subset(REF_CNRM,REF_CNRM$year>1970)
REF_CNRM = subset(REF_CNRM,REF_CNRM$year<2006)
REF_CNRM = subset(REF_CNRM,REF_CNRM$month>3)
REF_CNRM = subset(REF_CNRM,REF_CNRM$month<10)
summary(REF_CNRM)
#convert to celecius

REF_CNRM$Tasmoy = (REF_CNRM$Tasmin+REF_CNRM$Tasmax)/2
Tasmoy <- convert.temperature(from="K", to="C",REF_CNRM$Tasmoy)
REF_CNRM <- cbind(REF_CNRM,Tasmoy)
REF_CNRM <- REF_CNRM[,-10]
CNRM = aggregate(REF_CNRM[,10],FUN=mean,by=list(REF_CNRM$year))

#precipitation moyenne annuelle 

CNRM_Pre = aggregate(REF_CNRM[,8],FUN=mean,by=list(REF_CNRM$year))


# DAta IPSL
REF_IPSL <- read.table("LAVAL_IPSL_WRF_REF_1971-2005.txt",header=TRUE,dec=".",sep=" ")
summary(REF_IPSL)

colnames(REF_IPSL)[1] <-"date"
colnames(REF_IPSL)[4] <-"Tasmin"
colnames(REF_IPSL)[5] <-"Tasmax"
colnames(REF_IPSL)[6] <-"Pre"
#colnames(REF_IPSL)[7] <-"Neige"

#Date 
REF_IPSL$date <- as.Date(as.character(REF_IPSL$date), format = "%Y%m%d")
REF_IPSL$year <- year(ymd(REF_IPSL$date))
REF_IPSL$month <- month(ymd(REF_IPSL$date)) 
REF_IPSL$day <- day(ymd(REF_IPSL$date))
REF_IPSL<- REF_IPSL[,c(7,8,9,1,2,3,4,5,6)]
REF_IPSL <- REF_IPSL[,-4]

REF_IPSL = subset(REF_IPSL,REF_IPSL$year>1970)
REF_IPSL = subset(REF_IPSL,REF_IPSL$year<2006)
REF_IPSL = subset(REF_IPSL,REF_IPSL$month>3)
REF_IPSL= subset(REF_IPSL,REF_IPSL$month<10)
summary(REF_IPSL)
#convert to celecius
REF_IPSL$Tasmoy=(REF_IPSL$Tasmin+REF_IPSL$Tasmax)/2
Tasmoy <- convert.temperature(from="K", to="C",REF_IPSL$Tasmoy)
REF_IPSL <- cbind(REF_IPSL,Tasmoy)
REF_IPSL <- REF_CNRM[,-9]
IPSL = aggregate(REF_IPSL[,9],FUN=mean,by=list(REF_IPSL$year))

#precipitation moyenne annuelle IPSL
IPSL_Pre = aggregate(REF_IPSL[,8],FUN=mean,by=list(REF_IPSL$year))


# Données d'observations Laval
obs <- read.table("Laval.csv",header=TRUE,sep=";",dec=",", skip=3)
summary(obs)
colnames(obs)[2] <-"an"
colnames(obs)[3] <-"mois"
colnames(obs)[5] <-"Tasmax"
colnames(obs)[6] <-"Tasmin"
colnames(obs)[7] <-"Tasmoy"
colnames(obs)[8] <-"Pre"
summary(obs)
obs = subset(obs,obs$an>1970)
obs = subset(obs,obs$an<2006)
obs = subset(obs,obs$mois>3)
obs = subset(obs,obs$mois<11)
summary(obs)
OBS = aggregate(obs[,7],FUN=mean,by=list(obs$an))

#precipitation mean  IPSL

obs_Pre = aggregate(obs[,8],FUN=mean,by=list(obs$an))


#merge temperature 

CNRM_IPSL = merge(CNRM,IPSL, by="Group.1")
CNRM_IPSL_obs=merge(CNRM_IPSL,OBS, by ="Group.1")
colnames(CNRM_IPSL_obs)[1] <-"an"
colnames(CNRM_IPSL_obs)[2] <-"CNRM"
colnames(CNRM_IPSL_obs)[3] <-"IPSL"
colnames(CNRM_IPSL_obs)[4] <-"OBS_laval"

CNRMIPSL <- reshape2::melt(CNRM_IPSL_obs, id.var='an')
library(ggplot2)
laval <- ggplot(CNRMIPSL, aes(x=an, y=value, col=variable)) + geom_line()+xlab('Années') +
  ylab('Température Moyenne (°C)') 
laval + scale_x_continuous(name="Années", limits=c(1988, 2006)) +
  scale_y_continuous(name="Température Moyenne (°C)", limits=c(12.5, 17))

ggsave("Laval_CNRM_IPSL.png", width = 11, height = 8)

我有3个文件夹C:/ Users / majd / Documents / lavalC:/ Users / majd / Documents / ParisC:/ Users / majd / Documents / Toulouse

在3个文件夹中,我有3个文件:适用于laval:LAVAL_CNRM_Aladin_REF_1950-2005.txtLAVAL_IPSL_WRF_REF_1971-2005.txt拉瓦尔巴黎:PARIS _CNRM_Aladin_REF_1950-2005.txt....对于工作站3,相同文件

如何同时运行三个文件夹的脚本并同时使用ggplot保存三个工作站的图形

library(ggplot2)
laval <- ggplot(CNRMIPSL, aes(x=an, y=value, col=variable)) + geom_line()+xlab('Années') +
  ylab('Température Moyenne (°C)') 
laval + scale_x_continuous(name="Années", limits=c(1988, 2006)) +
  scale_y_continuous(name="Température Moyenne (°C)", limits=c(12.5, 17))

ggsave("Laval_CNRM_IPSL.png", width = 11, height = 8)
r tidyverse rscript
1个回答
0
投票

您可以使用已经创建的脚本来构建函数,然后将其应用于包含文件所在目录的向量。在函数内部,可以使用list.files搜索将要使用的文件的名称作为与某些模式匹配的文件。最后,您只需要将ggplot保存在正确的目录中,并使用工作站的名称命名该文件。这是您对我所做的修改的代码。我对未进行任何更改的所有部分进行了评论,以使其易于理解。希望它能工作!

#Added two libraries
library(stringr)
library(ggplot2)

my_function<-function(dirs)
{
  #apply the same function for all the entries in the dirs vector
  sapply(dirs, function(workd){
    #Locate the file inside each directory that has "CNRM" and is a txt file
    CNRM_location<-list.files(path = workd, 
                              pattern = glob2rx("*CNRM*.txt"),
                              full.names = T)
    #read that file
    REF_CNRM <- read.table(CNRM_location, header=TRUE,dec=".",sep=" ", encoding="UTF-8")

    # summary(REF_CNRM)
    # 
    # colnames(REF_CNRM)[1] <-"date"
    # colnames(REF_CNRM)[4] <-"Tasmin"
    # colnames(REF_CNRM)[5] <-"Tasmax"
    # colnames(REF_CNRM)[6] <-"Pre"
    # colnames(REF_CNRM)[7] <-"Neige"
    # 
    # 
    # REF_CNRM$date <- as.Date(as.character(REF_CNRM$date), format = "%Y%m%d")
    # REF_CNRM$year <- year(ymd(REF_CNRM$date))
    # REF_CNRM$month <- month(ymd(REF_CNRM$date)) 
    # REF_CNRM$day <- day(ymd(REF_CNRM$date))
    # REF_CNRM<- REF_CNRM[,c(8,9,10,1,2,3,4,5,6,7)]
    # REF_CNRM <- REF_CNRM[,-4]
    # 
    # REF_CNRM = subset(REF_CNRM,REF_CNRM$year>1970)
    # REF_CNRM = subset(REF_CNRM,REF_CNRM$year<2006)
    # REF_CNRM = subset(REF_CNRM,REF_CNRM$month>3)
    # REF_CNRM = subset(REF_CNRM,REF_CNRM$month<10)
    # summary(REF_CNRM)
    # #convert to celecius
    # 
    # REF_CNRM$Tasmoy = (REF_CNRM$Tasmin+REF_CNRM$Tasmax)/2
    # Tasmoy <- convert.temperature(from="K", to="C",REF_CNRM$Tasmoy)
    # REF_CNRM <- cbind(REF_CNRM,Tasmoy)
    # REF_CNRM <- REF_CNRM[,-10]
    # CNRM = aggregate(REF_CNRM[,10],FUN=mean,by=list(REF_CNRM$year))
    # 
    # #precipitation moyenne annuelle 
    # 
    # CNRM_Pre = aggregate(REF_CNRM[,8],FUN=mean,by=list(REF_CNRM$year))


    # DAta IPSL
    #Locate the file inside each directory that has "IPSL" and is a txt file
    IPSL_location<-list.files(path = workd, 
                              pattern = glob2rx("*IPSL*.txt"),
                              full.names = T)
    #read that file
    REF_IPSL <- read.table(IPSL_location,header=TRUE,dec=".",sep=" ")

    # summary(REF_IPSL)
    # 
    # colnames(REF_IPSL)[1] <-"date"
    # colnames(REF_IPSL)[4] <-"Tasmin"
    # colnames(REF_IPSL)[5] <-"Tasmax"
    # colnames(REF_IPSL)[6] <-"Pre"
    # #colnames(REF_IPSL)[7] <-"Neige"
    # 
    # #Date 
    # REF_IPSL$date <- as.Date(as.character(REF_IPSL$date), format = "%Y%m%d")
    # REF_IPSL$year <- year(ymd(REF_IPSL$date))
    # REF_IPSL$month <- month(ymd(REF_IPSL$date)) 
    # REF_IPSL$day <- day(ymd(REF_IPSL$date))
    # REF_IPSL<- REF_IPSL[,c(7,8,9,1,2,3,4,5,6)]
    # REF_IPSL <- REF_IPSL[,-4]
    # 
    # REF_IPSL = subset(REF_IPSL,REF_IPSL$year>1970)
    # REF_IPSL = subset(REF_IPSL,REF_IPSL$year<2006)
    # REF_IPSL = subset(REF_IPSL,REF_IPSL$month>3)
    # REF_IPSL= subset(REF_IPSL,REF_IPSL$month<10)
    # summary(REF_IPSL)
    # #convert to celecius
    # REF_IPSL$Tasmoy=(REF_IPSL$Tasmin+REF_IPSL$Tasmax)/2
    # Tasmoy <- convert.temperature(from="K", to="C",REF_IPSL$Tasmoy)
    # REF_IPSL <- cbind(REF_IPSL,Tasmoy)
    # REF_IPSL <- REF_CNRM[,-9]
    # IPSL = aggregate(REF_IPSL[,9],FUN=mean,by=list(REF_IPSL$year))
    # 
    # #precipitation moyenne annuelle IPSL
    # IPSL_Pre = aggregate(REF_IPSL[,8],FUN=mean,by=list(REF_IPSL$year))


    # Données d'observations Laval
    #Locate the file inside each directory that is a csv
    Station_location<-list.files(path = workd, 
                                 pattern = glob2rx("*.csv"),
                                 full.names = T)
    #Read the file
    obs <- read.table(Station_location,header=TRUE,sep=";",dec=",", skip=3)

    #This is for extracting the name of the station, so you can save the plot with
    #that name
    Station_name<-list.files(path = workd, 
                             pattern = glob2rx("*.csv"),
                             full.names = F)
    #Remove the ".csv" part and stay only with the Station name
    Station_name <- strsplit(Station_name,".csv")[[1]][1]

    # summary(obs)
    # colnames(obs)[2] <-"an"
    # colnames(obs)[3] <-"mois"
    # colnames(obs)[5] <-"Tasmax"
    # colnames(obs)[6] <-"Tasmin"
    # colnames(obs)[7] <-"Tasmoy"
    # colnames(obs)[8] <-"Pre"
    # summary(obs)
    # obs = subset(obs,obs$an>1970)
    # obs = subset(obs,obs$an<2006)
    # obs = subset(obs,obs$mois>3)
    # obs = subset(obs,obs$mois<11)
    # summary(obs)
    # OBS = aggregate(obs[,7],FUN=mean,by=list(obs$an))
    # 
    # #precipitation mean  IPSL
    # 
    # obs_Pre = aggregate(obs[,8],FUN=mean,by=list(obs$an))
    # 
    # 
    # #merge temperature 
    # 
    # CNRM_IPSL = merge(CNRM,IPSL, by="Group.1")
    # CNRM_IPSL_obs=merge(CNRM_IPSL,OBS, by ="Group.1")
    # colnames(CNRM_IPSL_obs)[1] <-"an"
    # colnames(CNRM_IPSL_obs)[2] <-"CNRM"
    # colnames(CNRM_IPSL_obs)[3] <-"IPSL"

    #Paste the station name with "OBS_" to rename the column 4
    colnames(CNRM_IPSL_obs)[4] <- paste0("OBS_",Station_name)

    # CNRMIPSL <- reshape2::melt(CNRM_IPSL_obs, id.var='an')
    # library(ggplot2)
    # laval <- ggplot(CNRMIPSL, aes(x=an, y=value, col=variable)) + geom_line()+xlab('Années') +
    #   ylab('Température Moyenne (°C)') 
    # laval + scale_x_continuous(name="Années", limits=c(1988, 2006)) +
    #   scale_y_continuous(name="Température Moyenne (°C)", limits=c(12.5, 17))

    #Finally save the plot to the directory using the station name
    ggsave(paste0(workd,"/",Station_name,"_CNRM_IPSL.png"), width = 11, height = 8)
  })

}

#Set the directories where you want to apply your function
station_directories<-c("C:/Users/majd/Documents/laval",
                       "C:/Users/majd/Documents/Paris",
                       "C:/Users/majd/Documents/Toulouse")

#Apply your function
my_function(station_directories)
© www.soinside.com 2019 - 2024. All rights reserved.