From 0becaf7f5e03690fee04492d4ff57c4b852dc2db Mon Sep 17 00:00:00 2001 From: "louis.heraut" <louis.heraut@inrae.fr> Date: Mon, 15 Nov 2021 15:26:07 +0100 Subject: [PATCH] Taking into account of missing file --- processing/extract.R | 95 +++++++++++++++++++++++++------------------- script_install.R | 1 + 2 files changed, 56 insertions(+), 40 deletions(-) diff --git a/processing/extract.R b/processing/extract.R index 78df646..4f7f383 100644 --- a/processing/extract.R +++ b/processing/extract.R @@ -178,29 +178,37 @@ extract_info = function (computer_data_path, filedir, filename, verbose=TRUE) { # Get the file path to the data file_path = file.path(computer_data_path, filedir, filename) - # Extract all the header - infotxt = c(readLines(file_path, n=41)) - - # Create a tibble with all the information needed - df_info = - tibble(code=trimws(substr(infotxt[11], 38, nchar(infotxt[11]))), - nom=trimws(substr(infotxt[12], 39, nchar(infotxt[12]))), - territoire=trimws(substr(infotxt[13], 39, nchar(infotxt[13]))), - L93X=as.numeric(substr(infotxt[16], 38, 50)), - L93Y=as.numeric(substr(infotxt[16], 52, 63)), - surface=as.numeric(substr(infotxt[19], 38, 50)), - statut=iStatut[trimws(substr(infotxt[26], 38, 50))], - finalite=iFinalite[trimws(substr(infotxt[26], 52, 56))], - type=iType[trimws(substr(infotxt[26], 58, 58))], - influence=iInfluence[trimws(substr(infotxt[26], 60, 60))], - debit=iDebit[trimws(substr(infotxt[26], 62, 62))], - QBE=iQBE[trimws(substr(infotxt[26], 72, 72))], - QME=iQME[trimws(substr(infotxt[26], 74, 74))], - QHE=iQHE[trimws(substr(infotxt[26], 76, 76))], - file_path=file_path - ) - return (df_info) + if (file.exists(file_path)) { + + # Extract all the header + infotxt = c(readLines(file_path, n=41)) + + # Create a tibble with all the information needed + df_info = + tibble(code=trimws(substr(infotxt[11], 38, nchar(infotxt[11]))), + nom=trimws(substr(infotxt[12], 39, nchar(infotxt[12]))), + territoire=trimws(substr(infotxt[13], 39, nchar(infotxt[13]))), + L93X=as.numeric(substr(infotxt[16], 38, 50)), + L93Y=as.numeric(substr(infotxt[16], 52, 63)), + surface=as.numeric(substr(infotxt[19], 38, 50)), + statut=iStatut[trimws(substr(infotxt[26], 38, 50))], + finalite=iFinalite[trimws(substr(infotxt[26], 52, 56))], + type=iType[trimws(substr(infotxt[26], 58, 58))], + influence=iInfluence[trimws(substr(infotxt[26], 60, 60))], + debit=iDebit[trimws(substr(infotxt[26], 62, 62))], + QBE=iQBE[trimws(substr(infotxt[26], 72, 72))], + QME=iQME[trimws(substr(infotxt[26], 74, 74))], + QHE=iQHE[trimws(substr(infotxt[26], 76, 76))], + file_path=file_path + ) + + return (df_info) + + } else { + print(paste('filename', file_path, 'do not exist')) + return (NULL) + } } # Example @@ -270,24 +278,31 @@ extract_data = function (computer_data_path, filedir, filename, verbose=TRUE) { # Get the file path to the data file_path = file.path(computer_data_path, filedir, filename) - # Extract the data as a data frame - df_data = read.table(file_path, - header=TRUE, - na.strings=c(' -99', ' -99.000'), - sep=';', - skip=41)[,1:2] - - # Extract all the information for the station - df_info = extract_info(computer_data_path, filedir, filename, verbose=FALSE) - # Get the code of the station - code = df_info$code - # Create a tibble with the date as Date class and the code of the station - df_data = tibble(Date=as.Date(as.character(df_data$Date), - format="%Y%m%d"), - df_data[-1], - code=code) - - return (df_data) + if (file.exists(file_path)) { + + # Extract the data as a data frame + df_data = read.table(file_path, + header=TRUE, + na.strings=c(' -99', ' -99.000'), + sep=';', + skip=41)[,1:2] + + # Extract all the information for the station + df_info = extract_info(computer_data_path, filedir, filename, verbose=FALSE) + # Get the code of the station + code = df_info$code + # Create a tibble with the date as Date class and the code of the station + df_data = tibble(Date=as.Date(as.character(df_data$Date), + format="%Y%m%d"), + df_data[-1], + code=code) + + return (df_data) + + } else { + print(paste('filename', file_path, 'do not exist')) + return (NULL) + } } # Example diff --git a/script_install.R b/script_install.R index aa217d6..c8cdf2a 100644 --- a/script_install.R +++ b/script_install.R @@ -4,6 +4,7 @@ install.packages("tools") install.packages("devtools") install.packages("dplyr") install.packages("ggplot2") +install.packages("officer") library(devtools) install_github("https://github.com/benRenard/BFunk") -- GitLab