I wrote a script that is supposed to reformat a csv file's columns into a specific format. It includes various "subfunctions" (not actual defined functions) that the user can select based on what in the csv file needs to be reformatted. The subfunctions are to: rename columns, delete columns, merge 2 columns into 1, split 1 column into 2, and reorder the columns. The subfunctions, when run on their own, all work perfectly.
When I originally wrote the script, it required the user to go through all of the subfunctions before being able to save the reformatted data as a new csv file, and it didn't allow the user to repeat subfunctions (say, to delete more than one column). I then rewrote it using a while loop so that the user was presented a numbered list of the subfunctions and could select which one to use. The subfunctions could be used in any order and reused any number of times.
In a different script, I used a while loop for the same purpose and it worked perfectly. But, for some reason, the while loop doesn't loop properly when I run this script.
Here's a condensed version of my script, as it would be too long to post the full thing:
col_opt <- c("Options:", "1. rename columns", "2. delete columns", "3. merge 2 columns", "4. split a column", "5. finish")cat(col_opt, sep = "\n")cond_col_fun <- readline("Enter option number: ")while(cond_col_fun == "1" | cond_col_fun == "one"){# code to rename columnscat(col_opt, sep = "\n")cond_col_fun <- readline("Enter option number: ")}while(cond_col_fun == "2" | cond_col_fun == "two"){# code to delete columnscat(col_opt, sep = "\n")cond_col_fun <- readline("Enter option number: ")}# same while loop repeats for options 3 & 4while(cond_col_fun == "5" | cond_col_fun == "five"){# code to reorder columns & write as new csv filebreak}
When I run the script, I am able to select option 1 (rename columns) first and then select whatever option afterwards, repeat any other option, and finish to write a new csv file. However, if I select any other option first and try to select option 1 (rename columns) second, it just exits the script. It looks like this in the console:
Options:1. rename columns2. delete columns3. merge 2 columns4. split a column5. finishEnter option number: 2# code runs to delete a column, based on user inputsOptions:1. rename columns2. delete columns3. merge 2 columns4. split a column5. finishEnter option number: 1>
There's nothing in the loop "while(cond_col_fun == "1" | cond_col_fun == "one")" that would make it exit the script, since the script works perfectly if this is the first subfunction run. I have read through my script dozens of times and revised it over and over again, but for some reason the while loop doesn't want to work properly.
If anyone has any idea why this is happening or how to fix it, I would be very grateful. I have a way more complex script with a multi-nested loop that works fine, so it's driving me crazy that this somewhat simple script has me stumped.
EDIT: Issue is resolved. Adding an initial while loop while(cond_col_fun < 5)
and changing the while loops to if statements fixed it.