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

Use gms::getLine() instead of re-defining it everywhere #930

Merged
merged 2 commits into from
Aug 3, 2022
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
27 changes: 7 additions & 20 deletions output.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,6 @@ if (file.exists("/iplex/01/landuse")) { # run is performed on the cluster
latexpath <- NA
}

get_line <- function() {
# gets characters (line) from the terminal of from a connection
# and stores it in the return object
if (interactive()) {
s <- readline()
} else {
con <- file("stdin")
s <- readLines(con, 1, warn = FALSE)
on.exit(close(con))
}
return(s)
}

0UmfHxcvx5J7JoaOhFSs5mncnisTJJ6q marked this conversation as resolved.
Show resolved Hide resolved
choose_folder <- function(folder, title = "Please choose a folder") {
dirs <- NULL

Expand All @@ -72,7 +59,7 @@ choose_folder <- function(folder, title = "Please choose a folder") {
cat(paste(seq_along(dirs), dirs, sep = ": "), sep = "\n")
cat(paste(length(dirs) + 1, "Search by the pattern.\n", sep = ": "))
cat("\nNumber: ")
identifier <- get_line()
identifier <- getLine()
identifier <- strsplit(identifier, ",")[[1]]
tmp <- NULL
for (i in seq_along(identifier)) {
Expand All @@ -86,13 +73,13 @@ choose_folder <- function(folder, title = "Please choose a folder") {
# PATTERN
if (length(identifier) == 1 && identifier == (length(dirs) + 1)) {
cat("\nInsert the search pattern or the regular expression: ")
pattern <- get_line()
pattern <- getLine()
id <- grep(pattern = pattern, dirs[-1])
# lists all chosen directories and ask for the confirmation of the made choice
cat("\n\nYou have chosen the following directories:\n")
cat(paste(seq_along(id), dirs[id + 1], sep = ": "), sep = "\n")
cat("\nAre you sure these are the right directories?(y/n): ")
answer <- get_line()
answer <- getLine()
if (answer == "y") {
return(dirs[id + 1])
} else {
Expand All @@ -112,7 +99,7 @@ choose_module <- function(Rfolder, title = "Please choose an outputmodule") {
cat("\n\n", title, ":\n\n")
cat(paste(seq_along(module), module, sep = ": "), sep = "\n")
cat("\nNumber: ")
identifier <- get_line()
identifier <- getLine()
identifier <- as.numeric(strsplit(identifier, ",")[[1]])
if (any(!(identifier %in% seq_along(module)))) {
stop("This choice (", identifier, ") is not possible. Please type in a number between 1 and ", length(module))
Expand All @@ -125,7 +112,7 @@ choose_mode <- function(title = "Please choose the output mode") {
cat("\n\n", title, ":\n\n")
cat(paste(seq_along(modes), modes, sep = ": "), sep = "\n")
cat("\nNumber: ")
identifier <- get_line()
identifier <- getLine()
identifier <- as.numeric(strsplit(identifier, ",")[[1]])
if (identifier == 1) {
comp <- FALSE
Expand Down Expand Up @@ -153,7 +140,7 @@ choose_slurmConfig_priority_standby <- function(title = "Please enter the slurm
cat("\n\n", title, ":\n\n")
cat(paste(seq_along(slurm_options), gsub("qos=", "", gsub("--", "", slurm_options)), sep = ": "), sep = "\n")
cat("\nNumber: ")
identifier <- get_line()
identifier <- getLine()
if (identifier == "") {
identifier <- 1
}
Expand All @@ -166,7 +153,7 @@ choose_slurmConfig_priority_standby <- function(title = "Please enter the slurm
choose_filename_prefix <- function(modules, title = "") {
cat(paste0("\n\n ", title, "Please choose a prefix for filenames of ", paste(modules, collapse=", "), ".\n"))
cat(" For example compareScenarios2 uses it for the filenames: compScen-yourprefix-2022-….pdf.\n Use only A-Za-z0-9_-, or leave empty:\n\n")
filename_prefix <- get_line()
filename_prefix <- getLine()
if(grepl("[^A-Za-z0-9_-]", filename_prefix)) {
filename_prefix <- choose_filename_prefix(modules, title = paste("No, this contained special characters, try again.\n",title))
}
Expand Down
17 changes: 1 addition & 16 deletions scripts/output/comparison/compareScenarios2.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,6 @@
# | Contact: [email protected]
# ---- Define set of runs that will be compared ----


# gets characters (line) from the terminal of from a connection
# and stores it in the return object
# same as get_line() in output.R, should eventually be extracted to package
getLine <- function() {
if (interactive()) {
s <- readline()
} else {
con <- file("stdin")
s <- readLines(con, 1, warn = FALSE)
on.exit(close(con))
}
return(s)
}

# Ask user to select an element form a sequence.
chooseFromSequence <- function(sequence, title, default) {
cat(
Expand All @@ -29,7 +14,7 @@ chooseFromSequence <- function(sequence, title, default) {
sep = "")
cat(paste(seq_along(sequence), sequence, sep = ": "), sep = "\n")
cat("\nNumbers, e.g., '1', '2,4', '3:5':\n")
input <- get_line()
input <- gms::getLine()
ids <- as.numeric(eval(parse(text = paste("c(", input, ")"))))
if (any(!ids %in% seq_along(sequence))) {
stop("Choose numbers between 1 and ", length(sequence))
Expand Down
2 changes: 1 addition & 1 deletion scripts/output/comparison/multiComparison.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ compareScenTable <- function(listofruns){

def_choice <- fls[length(fls)]
cat(sprintf("Select the correct output directory (%s): ", def_choice))
n <- as.integer(get_line())
n <- as.integer(gms::getLine())
if(is.na(n))
choice <- def_choice
else
Expand Down
14 changes: 2 additions & 12 deletions scripts/output/comparison/plotRuntime.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ library(tidyr)
library(ggplot2)
library(magclass)

getLine <- function() {
# gets and returns characters (line) from the terminal or from a connection
if (interactive()) {
s <- readline()
} else {
s <- readLines(withr::local_connection(file("stdin")), 1, warn = FALSE)
}
return(s)
}

if (!exists("source_include")) {
## Define arguments that can be read from command line
lucode2::readArgs("outputdirs")
Expand All @@ -31,15 +21,15 @@ print(outputdirs)
defaultFilenameKeywords <- "Base, NDC, PkBudg900"
cat("Which filename keywords (case-insensitive regex) do you want to compare? Separate with commas. (default: ",
defaultFilenameKeywords, ") ")
filenameKeywords <- getLine()
filenameKeywords <- gms::getLine()
if (identical(filenameKeywords, "")) {
filenameKeywords <- defaultFilenameKeywords
}
filenameKeywords <- trimws(strsplit(filenameKeywords, ",", fixed = TRUE)[[1]])

defaultComparisonProperty <- "config$gms$buildings"
cat("Which property in runstatistics.rda do you want to compare? (default: ", defaultComparisonProperty, ") ")
comparisonProperty <- getLine()
comparisonProperty <- gms::getLine()
if (identical(comparisonProperty, "")) {
comparisonProperty <- defaultComparisonProperty
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/output/comparison/policyCosts.R
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ while (!happy_with_input) {
message("Is that what you intended?")
message("Type '",crayon::green("y"),"' to continue, '",crayon::blue("r"),"' to reselect output directories, '",crayon::red("n"),"' to abort: ")

user_input <- get_line()
user_input <- gms::getLine()

if(user_input %in% c("y","Y","yes")) {
happy_with_input <- TRUE
Expand All @@ -282,7 +282,7 @@ while (!happy_with_input) {
message("3: Skip plot creation")
message("4: Plot until 2150 in pdf")
message("Type the number (or numbers seperated by a comma) to choose the special requests, or nothing to continue without any: ")
special_requests <- get_line() %>% str_split(",",simplify = TRUE) %>% as.vector()
special_requests <- gms::getLine() %>% str_split(",",simplify = TRUE) %>% as.vector()

} else if (user_input %in% c("r","R","reselect")) {
if (exists("choose_folder")) {
Expand Down
23 changes: 8 additions & 15 deletions scripts/output/single/plotIterations.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,11 @@ if (!exists("source_include")) {
outputdir <- normalizePath(outputdir)


getLine <- function() {
# gets characters (line) from the terminal of from a connection
# and stores it in the return object
if (interactive()) {
s <- readline()
} else {
con <- file("stdin")
on.exit(close(con))
s <- readLines(con, 1, warn = FALSE)
if (identical(length(s), 0L)) {
s <- ""
}
getLineCoerce <- function() {
# gets characters (line) from the user and always returns a string
s <- gms::getLine()
if (identical(length(s), 0L)) {
s <- ""
}
stopifnot(identical(length(s), 1L))
return(s)
Expand All @@ -41,7 +34,7 @@ rmdPath <- file.path(outputdir, paste0("plotIterations_", now, ".Rmd"))

# choose variables
cat("\n\nWhich variables/parameters do you want to plot? Separate with comma. (default: ", symbolNames, ") ")
answer <- getLine()
answer <- getLineCoerce()
if (!identical(trimws(answer), "")) {
symbolNames <- answer
}
Expand All @@ -52,7 +45,7 @@ symbolNames <- trimws(strsplit(symbolNames, ",")[[1]])
for (s in symbolNames) {
cat("\n\nHow do you want to map the dimensions of ", s, "in the plot?",
"Unused aesthetics need to be set to NULL. Combine dimensions with +.\n(default: ", plotMappingDefault, ")\n")
answer <- getLine()
answer <- getLineCoerce()
if (!identical(trimws(answer), "")) {
pm <- answer
if (grepl("\\+", pm)) {
Expand Down Expand Up @@ -156,7 +149,7 @@ writeLines(paste0(c(rmdHeader, vapply(symbolNames, rmdChunksForSymbol, character
), rmdPath)

cat("Render plots to html? (default: ", generateHtml, ") ")
answer <- getLine()
answer <- getLineCoerce()
if (!identical(trimws(answer), "")) {
generateHtml <- tolower(answer)
}
Expand Down
14 changes: 1 addition & 13 deletions scripts/start/choose_slurmConfig.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@
############### Select slurm partitiion ###############################
#######################################################################

get_line <- function(){
# gets characters (line) from the terminal or from a connection
# and returns it
if(interactive()){
s <- readline()
} else {
con <- file("stdin")
s <- readLines(con, 1, warn=FALSE)
on.exit(close(con))
}
return(s);
}

choose_slurmConfig <- function(identifier = FALSE) {

Expand Down Expand Up @@ -57,7 +45,7 @@ choose_slurmConfig <- function(identifier = FALSE) {
cat(modes,sep="\n")
cat("=======================================================================\n")
cat("Number: ")
identifier <- strsplit(get_line(), ",")[[1]]
identifier <- strsplit(gms::getLine(), ",")[[1]]
}
comp <- switch(as.integer(identifier),
"1" = "--qos=standby --nodes=1 --tasks-per-node=12" , # SLURM standby - task per node: 12 (nash H12) [recommended]
Expand Down
2 changes: 1 addition & 1 deletion scripts/start/submit.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ submit <- function(cfg, restart = FALSE, stopOnFolderCreateError = TRUE) {

# send prepare_and_run.R to cluster
cat(" Executing prepare_and_run.R for",cfg$results_folder,"\n")
if(cfg$slurmConfig=="direct") {
if (grepl("^direct", cfg$slurmConfig)) {
log <- format(Sys.time(), paste0(cfg$title,"-%Y-%H-%M-%S-%OS3.log"))
system("Rscript prepare_and_run.R")
} else {
Expand Down
27 changes: 6 additions & 21 deletions start.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,6 @@ helpText <- "
source("scripts/start/submit.R")
source("scripts/start/choose_slurmConfig.R")

############## Define function: get_line ##############################

get_line <- function(){
# gets characters (line) from the terminal or from a connection
# and stores it in the return object
if(interactive()){
s <- readline()
} else {
con <- file("stdin")
s <- readLines(con, 1, warn=FALSE)
on.exit(close(con))
}
return(s);
}

############## Define function: chooseFromList #########################
# thelist: list to be selected from
# group: list with same dimension as thelist with group names to allow to select whole groups
Expand Down Expand Up @@ -93,7 +78,7 @@ chooseFromList <- function(thelist, type = "runs", returnboolean = FALSE, multip
message(paste(paste(str_pad(1:length(thelist), nchar(length(thelist)), side = "left"), thelist, sep=": " ), collapse="\n"))
message("\nNumber", ifelse(multiple,"s entered as 2,4:6,9",""),
ifelse(allowempty, " or leave empty", ""), " (", type, "): ")
identifier <- strsplit(get_line(), ",")[[1]]
identifier <- strsplit(gms::getLine(), ",")[[1]]
if (allowempty & length(identifier) == 0) return(NA)
if (length(identifier) == 0 | ! all(grepl("^[0-9,:]*$", identifier))) {
message("Try again, you have to choose some numbers.")
Expand Down Expand Up @@ -124,13 +109,13 @@ chooseFromList <- function(thelist, type = "runs", returnboolean = FALSE, multip
# PATTERN
if(multiple && length(identifier == 1) && identifier == length(thelist) ){
message("\nInsert the search pattern or the regular expression: ")
pattern <- get_line()
pattern <- gms::getLine()
id <- grep(pattern=pattern, originallist)
# lists all chosen and ask for the confirmation of the made choice
message("\n\nYou have chosen the following ", type, ":")
if (length(id) > 0) message(paste(paste(1:length(id), originallist[id], sep=": "), collapse="\n"))
message("\nAre you sure these are the right ", type, "? (y/n): ")
if(get_line() == "y"){
if(gms::getLine() == "y"){
identifier <- id
booleanlist[id] <- 1
} else {
Expand All @@ -151,7 +136,7 @@ chooseFromList <- function(thelist, type = "runs", returnboolean = FALSE, multip
select_testOneRegi_region <- function() {
message("\nWhich region should testOneRegi use? Type it, or leave empty to keep settings:\n",
"Examples are CAZ, CHA, EUR, IND, JPN, LAM, MEA, NEU, OAS, REF, SSA, USA.")
return(get_line())
return(gms::getLine())
}

############## Define function: configure_cfg #########################
Expand Down Expand Up @@ -316,7 +301,7 @@ if ("--reset" %in% argv) {
if (any(c("--testOneRegi", "--debug", "--quick") %in% argv) & "--restart" %in% argv & ! "--reprepare" %in% argv) {
message("\nIt is impossible to combine --restart with --debug, --quick or --testOneRegi because full.gms has to be rewritten.\n",
"If this is what you want, use --reprepare instead, or answer with y:")
if (get_line() %in% c("Y", "y")) argv <- c(argv, "--reprepare")
if (gms::getLine() %in% c("Y", "y")) argv <- c(argv, "--reprepare")
}

ignorederrors <- 0 # counts ignored errors in --test mode
Expand All @@ -341,7 +326,7 @@ if (any(c("--reprepare", "--restart") %in% argv)) {
# possibledirs <- sub("./output/", "", lucode2::findIterations(runs, modelpath = "./output", latest = TRUE))
outputdirs <- chooseFromList(sort(unique(possibledirs)), "runs to be restarted", returnboolean = FALSE)
message("\nAlso restart subsequent runs? Enter y, else leave empty:")
restart_subsequent_runs <- get_line() %in% c("Y", "y")
restart_subsequent_runs <- gms::getLine() %in% c("Y", "y")
if ("--testOneRegi" %in% argv) testOneRegi_region <- select_testOneRegi_region()
if ("--reprepare" %in% argv) {
message("\nBecause of the flag --reprepare, move full.gms -> full_old.gms and fulldata.gdx -> fulldata_old.gdx such that runs are newly prepared.\n")
Expand Down