ourMELONS/R/uigetfile.R

31 lines
1.4 KiB
R
Raw Normal View History

2020-05-20 10:27:37 +02:00
#' @title Select a file for loading
#' @description Loosely mimics the functionality of the `uigetfile` function on
#' Matlab.
#' @references https://se.mathworks.com/help/matlab/ref/uigetfile.html
2020-05-20 13:09:11 +02:00
#' @param filter Filter listed files
2020-05-20 10:27:37 +02:00
#' @param title Pre-prompt message
#' @export
2020-05-20 13:09:11 +02:00
uigetfile <- function(filter = "", title = "") {
2020-05-20 10:27:37 +02:00
# ==========================================================================
# Pre-prompt message
# ==========================================================================
2020-05-20 11:22:31 +02:00
message(title)
2020-05-20 10:27:37 +02:00
# ==========================================================================
# Reading file path and name
# ==========================================================================
filepath <- readline(
paste0("Enter file path (leave empty for ", getwd(), "): ")
)
if (filepath == "") filepath <- getwd()
2020-05-20 13:09:11 +02:00
# ==========================================================================
# Presenting possible files
# ==========================================================================
message("Files present on that directory:")
print(list.files(path = filepath, pattern = filter, ignore.case = TRUE))
2020-05-20 10:27:37 +02:00
filename <- file.choose()
# ==========================================================================
# Organizing output
# ==========================================================================
out <- list(name = filename, path = filepath)
return(out)
}