-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add file parsing #2
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
^\.lintr$ | ||
^LICENSE\.md$ | ||
^\.github$ | ||
^\.github$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
YEAR: 2024 | ||
COPYRIGHT HOLDER: Hector Vernet, Marie Revol, Noemie Wawrzyniak, Amance Graindorge | ||
COPYRIGHT HOLDER: Hector Vernet, Marie Revol, Noemie Wawrzyniak, Amance Graindorge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
import(openxlsx) | ||
import(readODS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# This file is used to define the imports that will be updated by roxygen2 when running `devtools::document()`. | ||
|
||
#' @import openxlsx | ||
#' @import readODS | ||
NULL | ||
#> NULL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
library(openxlsx) | ||
library(readODS) | ||
|
||
# Prefixes of the head of the wishes columns | ||
Q2_PREFIX <- "Q02_Voeux->" | ||
Q3_PREFIX <- "Q03_VoeuxEMIR->" | ||
Q4_PREFIX <- "Q04_voeuxMICA->" | ||
|
||
#' Reads the file and selects the needed columns | ||
#' | ||
#' @param file_path The path to the file to parse | ||
#' @return The file as a data frame | ||
read_file <- function(file_path) { | ||
# Check if file extension is supported (xlsx or ods) | ||
if (grepl(".xlsx", file_path)) { | ||
file_data <- openxlsx::read.xlsx(file_path, sheet = 1, skipEmptyRows = FALSE, skipEmptyCols = FALSE, colNames = TRUE, cols = (8:25), sep.names = " ") | ||
file_data <- file_data[, c(8:25)] | ||
} else if (grepl(".ods", file_path)) { | ||
file_data <- readODS::read_ods(file_path, sheet = 1, col_names = TRUE, as_tibble = FALSE, na = "NULL") | ||
file_data <- file_data[, c(8:25)] | ||
} else { | ||
stop("File type not supported") | ||
} | ||
return(file_data) | ||
} | ||
|
||
#' Synthesizes a student's wishes from multiple columns per filiere to 7 columns for all filieres | ||
#' | ||
#' @param wishes The wishes columns | ||
#' @return A vector with the filiere and the 7 wishes (NA if no wish) | ||
synthesize_wishes <- function(wishes) { | ||
# Get the head of the columns | ||
colnames <- colnames(wishes) | ||
# Extract the filiere info | ||
if (length(grep("FC_FIRE", wishes[1])) > 0) { | ||
# Dataframe from rank number and speciality name | ||
df <- data.frame(rank = unlist(wishes[2:8]), spe = sub(Q2_PREFIX, "", colnames[2:8])) | ||
# Sort the dataframe by rank | ||
df <- df[order(df$rank), ] | ||
# Set result to the filiere and the specialities ordered by the student's preferences | ||
result <- c("FC_FIRE", df$spe) | ||
} else if (length(grep("EMIR", wishes[1])) > 0) { | ||
df <- data.frame(rank = unlist(wishes[9:12]), spe = sub(Q3_PREFIX, "", colnames[9:12])) | ||
df <- df[order(df$rank), ] | ||
result <- c("EMIR", df$spe) | ||
} else if (length(grep("MICA", wishes[1])) > 0) { | ||
df <- data.frame(rank = unlist(wishes[13:16]), spe = sub(Q4_PREFIX, "", colnames[13:16])) | ||
df <- df[order(df$rank), ] | ||
result <- c("MICA", df$spe) | ||
} else { | ||
stop("Unknown filiere") | ||
} | ||
return(result) | ||
} | ||
|
||
#' Parses the file to build a clean data frame | ||
#' | ||
#' @param file_path The path to the file to parse | ||
#' @return A data frame with the cleaned data | ||
parse_file <- function(file_path) { | ||
# Get file data as a data frame | ||
tryCatch({ | ||
file_data <- read_file(file_path) | ||
}, error = function(e) { | ||
stop("Error while reading the file : ", conditionMessage(e)) | ||
}) | ||
# Build the result data frame | ||
data <- data.frame( | ||
Nom = rep(NA_character_, nrow(file_data)), | ||
Prenom = rep(NA_character_, nrow(file_data)), | ||
Filiere = rep(NA_character_, nrow(file_data)), | ||
V1 = rep(NA_character_, nrow(file_data)), | ||
V2 = rep(NA_character_, nrow(file_data)), | ||
V3 = rep(NA_character_, nrow(file_data)), | ||
V4 = rep(NA_character_, nrow(file_data)), | ||
V5 = rep(NA_character_, nrow(file_data)), | ||
V6 = rep(NA_character_, nrow(file_data)), | ||
V7 = rep(NA_character_, nrow(file_data)), | ||
Aff_V1_S1 = rep(NA_character_, nrow(file_data)), | ||
Aff_V1_S2 = rep(NA_character_, nrow(file_data)), | ||
Aff_V1_S3 = rep(NA_character_, nrow(file_data)), | ||
Aff_V2_S1 = rep(NA_character_, nrow(file_data)), | ||
Aff_V2_S2 = rep(NA_character_, nrow(file_data)), | ||
Aff_V2_S3 = rep(NA_character_, nrow(file_data)), | ||
Aff_V3_S1 = rep(NA_character_, nrow(file_data)), | ||
Aff_V3_S2 = rep(NA_character_, nrow(file_data)), | ||
Aff_V3_S3 = rep(NA_character_, nrow(file_data)) | ||
) | ||
# Process each row of the file's data | ||
for (i in seq_len(nrow(file_data))) { | ||
# Name | ||
name_split <- strsplit(file_data[i, 1], split = " ")[[1]] # We assume that the first and last name are separated by a space | ||
data[i, 1] <- name_split[2] | ||
data[i, 2] <- name_split[1] | ||
# Wishes | ||
tryCatch({ | ||
synthesized_wishes <- synthesize_wishes(file_data[i, 3:18]) | ||
}, error = function(e) { | ||
stop("Error while synthesizing the wishes (l.", i, "): ", conditionMessage(e)) | ||
}) | ||
data[i, 3:(length(synthesized_wishes) + 2)] <- synthesized_wishes | ||
} | ||
return(data) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,8 @@ | |
|
||
library(testthat) | ||
library(ADSPlanner) | ||
library(mockr) | ||
library(openxlsx) | ||
library(readODS) | ||
|
||
test_check("ADSPlanner") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
read_file_output <- data.frame( | ||
"Nom complet" = c("Laurence Marchand", "Laurence ROZE", "Laurence Roze"), | ||
"Nom d’utilisateur" = c("[email protected]", "[email protected]", "[email protected]"), | ||
"Q01_Filiere" = c("1 : FC_FIRE (Filière classique ou filière internationale)", "2 : EMIR", "3 : MICA"), | ||
"Q02_Voeux->EII" = c(1, NA, NA), | ||
"Q02_Voeux->E&T" = c(2, NA, NA), | ||
"Q02_Voeux->INFO" = c(5, NA, NA), | ||
"Q02_Voeux->MA" = c(4, NA, NA), | ||
"Q02_Voeux->GCU" = c(3, NA, NA), | ||
"Q02_Voeux->GMA" = c(6, NA, NA), | ||
"Q02_Voeux->GPM" = c(7, NA, NA), | ||
"Q03_VoeuxEMIR->EII" = c(NA, 4, NA), | ||
"Q03_VoeuxEMIR->INFO" = c(NA, 3, NA), | ||
"Q03_VoeuxEMIR->GPM" = c(NA, 2, NA), | ||
"Q03_VoeuxEMIR->E&T" = c(NA, 1, NA), | ||
"Q04_voeuxMICA->GCU" = c(NA, NA, 4), | ||
"Q04_voeuxMICA->MA" = c(NA, NA, 1), | ||
"Q04_voeuxMICA->GMA" = c(NA, NA, 3), | ||
"Q04_voeuxMICA->INFO" = c(NA, NA, 2), | ||
check.names = FALSE | ||
) | ||
|
||
read_file_output_invalid <- data.frame( | ||
"Nom complet" = c("Laurence Marchand", "Laurence ROZE", "Laurence Roze"), | ||
"Nom d’utilisateur" = c("[email protected]", "[email protected]", "[email protected]"), | ||
"Q01_Filiere" = c("1 : FC_FIRE (Filière classique ou filière internationale)", "4 : Filière that doesn't exist", "3 : MICA"), | ||
"Q02_Voeux->EII" = c(1, NA, NA), | ||
"Q02_Voeux->E&T" = c(2, NA, NA), | ||
"Q02_Voeux->INFO" = c(5, NA, NA), | ||
"Q02_Voeux->MA" = c(4, NA, NA), | ||
"Q02_Voeux->GCU" = c(3, NA, NA), | ||
"Q02_Voeux->GMA" = c(6, NA, NA), | ||
"Q02_Voeux->GPM" = c(7, NA, NA), | ||
"Q03_VoeuxEMIR->EII" = c(NA, 4, NA), | ||
"Q03_VoeuxEMIR->INFO" = c(NA, 3, NA), | ||
"Q03_VoeuxEMIR->GPM" = c(NA, 2, NA), | ||
"Q03_VoeuxEMIR->E&T" = c(NA, 1, NA), | ||
"Q04_voeuxMICA->GCU" = c(NA, NA, 4), | ||
"Q04_voeuxMICA->MA" = c(NA, NA, 1), | ||
"Q04_voeuxMICA->GMA" = c(NA, NA, 3), | ||
"Q04_voeuxMICA->INFO" = c(NA, NA, 2), | ||
check.names = FALSE | ||
) | ||
|
||
synth_wishes_input_fc_fire <- data.frame( | ||
"Q01_Filiere" = "1 : FC_FIRE (Filière classique ou filière internationale)", | ||
"Q02_Voeux->EII" = 1, | ||
"Q02_Voeux->E&T" = 2, | ||
"Q02_Voeux->INFO" = 5, | ||
"Q02_Voeux->MA" = 4, | ||
"Q02_Voeux->GCU" = 3, | ||
"Q02_Voeux->GMA" = 6, | ||
"Q02_Voeux->GPM" = 7, | ||
"Q03_VoeuxEMIR->EII" = NA, | ||
"Q03_VoeuxEMIR->INFO" = NA, | ||
"Q03_VoeuxEMIR->GPM" = NA, | ||
"Q03_VoeuxEMIR->E&T" = NA, | ||
"Q04_voeuxMICA->GCU" = NA, | ||
"Q04_voeuxMICA->MA" = NA, | ||
"Q04_voeuxMICA->GMA" = NA, | ||
"Q04_voeuxMICA->INFO" = NA, | ||
check.names = FALSE | ||
) | ||
|
||
synth_wishes_output_fc_fire <- c( | ||
"FC_FIRE", | ||
"EII", | ||
"E&T", | ||
"GCU", | ||
"MA", | ||
"INFO", | ||
"GMA", | ||
"GPM" | ||
) | ||
|
||
synth_wishes_input_emir <- data.frame( | ||
"Q01_Filiere" = "2 : EMIR", | ||
"Q02_Voeux->EII" = NA, | ||
"Q02_Voeux->E&T" = NA, | ||
"Q02_Voeux->INFO" = NA, | ||
"Q02_Voeux->MA" = NA, | ||
"Q02_Voeux->GCU" = NA, | ||
"Q02_Voeux->GMA" = NA, | ||
"Q02_Voeux->GPM" = NA, | ||
"Q03_VoeuxEMIR->EII" = 4, | ||
"Q03_VoeuxEMIR->INFO" = 3, | ||
"Q03_VoeuxEMIR->GPM" = 2, | ||
"Q03_VoeuxEMIR->E&T" = 1, | ||
"Q04_voeuxMICA->GCU" = NA, | ||
"Q04_voeuxMICA->MA" = NA, | ||
"Q04_voeuxMICA->GMA" = NA, | ||
"Q04_voeuxMICA->INFO" = NA, | ||
check.names = FALSE | ||
) | ||
|
||
synth_wishes_output_emir <- c( | ||
"EMIR", | ||
"E&T", | ||
"GPM", | ||
"INFO", | ||
"EII" | ||
) | ||
|
||
synth_wishes_input_mica <- data.frame( | ||
"Q01_Filiere" = "3 : MICA", | ||
"Q02_Voeux->EII" = NA, | ||
"Q02_Voeux->E&T" = NA, | ||
"Q02_Voeux->INFO" = NA, | ||
"Q02_Voeux->MA" = NA, | ||
"Q02_Voeux->GCU" = NA, | ||
"Q02_Voeux->GMA" = NA, | ||
"Q02_Voeux->GPM" = NA, | ||
"Q03_VoeuxEMIR->EII" = NA, | ||
"Q03_VoeuxEMIR->INFO" = NA, | ||
"Q03_VoeuxEMIR->GPM" = NA, | ||
"Q03_VoeuxEMIR->E&T" = NA, | ||
"Q04_voeuxMICA->GCU" = 4, | ||
"Q04_voeuxMICA->MA" = 1, | ||
"Q04_voeuxMICA->GMA" = 3, | ||
"Q04_voeuxMICA->INFO" = 2, | ||
check.names = FALSE | ||
) | ||
|
||
synth_wishes_output_mica <- c( | ||
"MICA", | ||
"MA", | ||
"INFO", | ||
"GMA", | ||
"GCU" | ||
) | ||
|
||
synth_wishes_input_invalid <- data.frame( | ||
"Q01_Filiere" = "4 : Filière that doesn't exist", | ||
"Q02_Voeux->EII" = NA, | ||
"Q02_Voeux->E&T" = NA, | ||
"Q02_Voeux->INFO" = NA, | ||
"Q02_Voeux->MA" = NA, | ||
"Q02_Voeux->GCU" = NA, | ||
"Q02_Voeux->GMA" = NA, | ||
"Q02_Voeux->GPM" = NA, | ||
"Q03_VoeuxEMIR->EII" = NA, | ||
"Q03_VoeuxEMIR->INFO" = NA, | ||
"Q03_VoeuxEMIR->GPM" = NA, | ||
"Q03_VoeuxEMIR->E&T" = NA, | ||
"Q04_voeuxMICA->GCU" = 4, | ||
"Q04_voeuxMICA->MA" = 1, | ||
"Q04_voeuxMICA->GMA" = 3, | ||
"Q04_voeuxMICA->INFO" = 2, | ||
check.names = FALSE | ||
) | ||
|
||
parse_file_output <- data.frame( | ||
Nom = c("Marchand", "ROZE", "Roze"), | ||
Prenom = c("Laurence", "Laurence", "Laurence"), | ||
Filiere = c("FC_FIRE", "EMIR", "MICA"), | ||
V1 = c("EII", "E&T", "MA"), | ||
V2 = c("E&T", "GPM", "INFO"), | ||
V3 = c("GCU", "INFO", "GMA"), | ||
V4 = c("MA", "EII", "GCU"), | ||
V5 = c("INFO", NA_character_, NA_character_), | ||
V6 = c("GMA", NA_character_, NA_character_), | ||
V7 = c("GPM", NA_character_, NA_character_), | ||
Aff_V1_S1 = c(NA_character_, NA_character_, NA_character_), | ||
Aff_V1_S2 = c(NA_character_, NA_character_, NA_character_), | ||
Aff_V1_S3 = c(NA_character_, NA_character_, NA_character_), | ||
Aff_V2_S1 = c(NA_character_, NA_character_, NA_character_), | ||
Aff_V2_S2 = c(NA_character_, NA_character_, NA_character_), | ||
Aff_V2_S3 = c(NA_character_, NA_character_, NA_character_), | ||
Aff_V3_S1 = c(NA_character_, NA_character_, NA_character_), | ||
Aff_V3_S2 = c(NA_character_, NA_character_, NA_character_), | ||
Aff_V3_S3 = c(NA_character_, NA_character_, NA_character_) | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to document these imports inside
ADSPlanner-package.R
and then rundevtools::document()
, or else they won't work when the package is compiled. (Check out my PR)