#' loadFile
#'
#' @param file name of the file you want to load. If empty it will list the files in `folder`
#' @param folder folder where the file is found within the proyect (default = "DATOS/")
#' @param sheetIndex if file is an Excel/Calc file, number of the sheet to be loaded (default = 1)
#'
#'
#' @return data.frame loaded data
#'
#' @examples
#'
#'
#' @importFrom tools file_ext
#' @importFrom crayon bold green red
#' @importFrom haven read_sav
#' @importFrom readxl read_xlsx read_xls
#' @export
loadFile <- function(file=NULL, folder = "DATOS/", sheetIndex=1){
require(crayon)
if(is.null(file) | is.numeric(file)) {
archivos <- list.files(path=folder)
if(is.null(file)) {
message("Files in ", folder,":")
mapply(function(x,y) message(paste0(y," - ",x)), archivos, seq_along(archivos))
selection <- readline(prompt="Choose file to load (0) to quit: ")
if(selection == 0) return(NULL)
else return(loadFile(as.numeric(selection)))
} else if (is.numeric(file)) {
return(loadFile(archivos[file], folder = folder, sheetIndex = sheetIndex))
}
}
message("Loading: ",file)
file_path <- paste0(folder,file)
file_name <- file
# if(is.null(LOG)) LOG <- list(text="")
if(tools::file_ext(file_path) == "xlsx"){
datos <- readxl::read_xlsx(file_path, sheet = sheetIndex)
} else if(tools::file_ext(file_name) == "xls") {
datos <- readxl::read_xls(file_path, sheet = sheetIndex)
} else if(tools::file_ext(file_name) == "sav") {
datos <- haven::read_spss(file_path)
#---- hagamos que los labelled funcionen
datos <- haven::as_factor(datos)
}
if(exists("datos")) {
crayon.x <- green $ bold
message(crayon.x("✔ "),"File loaded: ",file_name)
return(as.data.frame(datos))
} else {
crayon.x <- red $ bold
message(crayon.x("✗ "),"File ",crayon::bold("not")," loaded: ",file_name," EXT -> ",tools::file_ext(file_name))
}
return(NA)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.