Skip to content
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 6 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
^\.lintr$
^LICENSE\.md$
^\.github$
^\.github$
1 change: 0 additions & 1 deletion .lintr
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ linters: linters_with_defaults(
object_name_linter = object_name_linter(styles = c("snake_case", "symbols", "SNAKE_CASE"))
) # see vignette("lintr")
encoding: "UTF-8"

6 changes: 5 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Imports:
openxlsx,
readODS
Suggests:
testthat (>= 3.0.0)
testthat (>= 3.0.0),
mockr
Config/testthat/edition: 3
2 changes: 1 addition & 1 deletion LICENSE
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
2 changes: 2 additions & 0 deletions NAMESPACE
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)
6 changes: 6 additions & 0 deletions R/ADSPlanner-package.R
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
104 changes: 104 additions & 0 deletions R/input.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
library(openxlsx)
library(readODS)
Copy link
Collaborator

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 run devtools::document(), or else they won't work when the package is compiled. (Check out my PR)


# 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)
}
17 changes: 17 additions & 0 deletions man/parse_file.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions man/read_file.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions man/synthesize_wishes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added tests/data/test_file.ods
Binary file not shown.
Binary file added tests/data/test_file.xlsx
Binary file not shown.
3 changes: 3 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@

library(testthat)
library(ADSPlanner)
library(mockr)
library(openxlsx)
library(readODS)

test_check("ADSPlanner")
172 changes: 172 additions & 0 deletions tests/testthat/setup.R
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_)
)
Loading
Loading