I am trying to create a text file based on two tables. The text file should look like this for each interstage:
$INTERSTAGE
ISt name : St1->St12
ISt description : Source: Stage sequence plan
ISt Description:
INTERSTAGE_number : 1
Length [s] : 0
From stage : 1
To stage : 12
$
SG3 -127 "insert value later manually".
If a SG goes from an active phase to an inactive phase it should be written down as f.i.
SG3 -127 "insert value later manually"
If a SG goes from an inactive phase to an active phase it should be written down as f.i.
SG3 "insert value later manually" 127
I tried to program this in R. But it does not work when i have for instance a phase12 instead of a phase3. How can i solve this issue?
tabel_signal_groups <- data.frame(
Signal_Group = c("SG1", "SG2", "SG3", "SG4"),
phase1 = c("x", "", "x","x"),
phase2 = c("", "x", "","x"),
phase12 = c("", "", "","x")
)
tabel <- data.frame(
Interstage = c("ISt112", "ISt23", "ISt31"),
from_stage = c(1, 2, 3),
to_stage = c(12, 3, 1),
length = c(0, 5, 7)
)
# Aangenomen voorbeeldkleuren voor rood en groen
red_color <- "Red"
green_color <- "Green"
# Functie om de tekststring te genereren voor elke record in de tabel
genereer_text_string <- function(row, signal_groups_df) {
text_string <- paste0(
"$", row["Interstage"], "\n",
"ISt name : St", row["from_stage"], "->St", row["to_stage"], "\n",
"ISt description : Source: Stage sequence plan\n",
"ISt Description: \n",
"INTERSTAGE_number : ", row["Interstage"], "\n",
"Length [s] : ", row["length"], "\n",
"From stage : ", row["from_stage"], "\n",
"To stage : ", row["to_stage"], "\n"
)
# Check welke signal groups actief zijn in elke fase en voeg rood/groen informatie toe
for (i in seq_along(signal_groups_df$Signal_Group)) {
signal_group <- signal_groups_df$Signal_Group[i]
active_phases <- colnames(signal_groups_df)[signal_groups_df[i, ] == "x"]
if (length(active_phases) > 0) {
if (all(grepl("phase1|phase2", active_phases))) {
# Add information for red signal group
text_string <- paste0(text_string, signal_group, " -127 Handmatig Invullen\n")
} else {
# Add information for green signal group
text_string <- paste0(text_string, signal_group, " Handmatig Invullen 127\n")
}
}
}
text_string <- paste0(text_string, "$\n")
return(text_string)
}
# Loop over de rijen van de tabel en creëer de tekststring voor elke rij
alle_text_strings <- apply(tabel, 1, genereer_text_string, signal_groups_df = tabel_signal_groups)
# Resultaten afdrukken
cat(alle_text_strings)