Added missing util function

This commit is contained in:
2022-06-16 13:18:30 +01:00
parent 038b037e90
commit abbd2f3094
2 changed files with 66 additions and 0 deletions

View File

@@ -8,6 +8,11 @@ check_pacman_dir <- function() {
return(invisible(FALSE)) return(invisible(FALSE))
} }
} }
if (!dir.exists("./.pacman/library")) {
if (!dir.create("./.pacman/library")) {
return(invisible(FALSE))
}
}
invisible(TRUE) invisible(TRUE)
} }
@@ -22,3 +27,34 @@ get_cran_repo <- function() {
} }
repo repo
} }
#' Displays a menu for the user to select elements from.
#'
#' @param choices A character vector of choices the user must choose from
#' @param title An optional string containing text for a title line above the
#' menu
#' @param msg An optional string containung the text directing the user what to
#' do, displayed below the menu
#' @param default An integer containing the index of the default choice to return
#'
#' @return string Selected menu choice
select_menu <- function(choices, title = NULL, msg = "Enter a number from the menu above, or an empty line to default: ", default = 1) {
if (!is.null(title)) {
cat(title, "\n", sep = "")
}
nc <- length(choices)
op <- paste0(format(seq_len(nc)), ": ", choices)
fop <- format(op)
cat("", fop, "", sep = "\n")
repeat {
answer <- readline(msg)
answer <- strsplit(answer, "[ ,]+")[[1]]
if (length(answer) == 0) {
return(choices[default])
}
if (all(answer %in% seq_along(choices))) {
return(choices[as.integer(answer)])
}
}
}

30
man/select_menu.Rd Normal file
View File

@@ -0,0 +1,30 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/utils.R
\name{select_menu}
\alias{select_menu}
\title{Displays a menu for the user to select elements from.}
\usage{
select_menu(
choices,
title = NULL,
msg = "Enter a number from the menu above, or an empty line to default: ",
default = 1
)
}
\arguments{
\item{choices}{A character vector of choices the user must choose from}
\item{title}{An optional string containing text for a title line above the
menu}
\item{msg}{An optional string containung the text directing the user what to
do, displayed below the menu}
\item{default}{An integer containing the index of the default choice to return}
}
\value{
string Selected menu choice
}
\description{
Displays a menu for the user to select elements from.
}