ourMELONS/R/fgetl-fopen.R

27 lines
1.1 KiB
R
Raw Normal View History

2020-07-31 09:20:44 +02:00
#' @title Read line from file, removing newline characters
#' @description Equivalent function to its homonymous Matlab equivalent.
2020-07-31 09:26:57 +02:00
#' @param file character vector to be read, usually an output of `fopen()`
2020-07-31 09:20:44 +02:00
#' @return If the file is nonempty, then fgetl returns tline as a character vector. If the file is empty and contains only the end-of-file marker, then fgetl returns tline as a numeric value -1.
#' @author Waldir Leoncio
2020-07-31 09:26:57 +02:00
#' @seealso fopen
2020-07-31 09:20:44 +02:00
#' @export
fgetl <- function(file) {
# ==========================================================================
# Validation
# ==========================================================================
2020-07-31 09:26:57 +02:00
if (length(file) <= 1) return(-1)
2020-07-31 09:20:44 +02:00
# ==========================================================================
2020-07-31 09:26:57 +02:00
# Returning file minus the first line
2020-07-31 09:20:44 +02:00
# ==========================================================================
2020-07-31 09:26:57 +02:00
out <- file[-1]
2020-07-31 09:20:44 +02:00
return(out)
}
#' @title Open file
#' @description Open a text file
#' @param filename Path and name of file to be open
#' @return The same as `readLines(filename)`
#' @author Waldir Leoncio
2020-07-31 09:26:57 +02:00
#' @seealso fgetl
2020-07-31 09:20:44 +02:00
#' @export
fopen <- function(filename) readLines(filename)