#' Data loading
#'
#' This function helps with the data loading by wraping different functions accessible
#' in the feR package and that together make a complete and seamless data loading procedure
#'
#' @param file String; file name or empty if you want the `folder` listed for you to choose file
#' @param folder String; folder inside the project filder where data files can be found
#' @param sheetIndex numeric (default=1); if data file is an excel/calc file what sheet should be loaded
#' @param var.to.upper boolean (default=FALSE); return all caps
#' @param var.forbiden.chars string vector (see details); characters to ve removed from the string
#' @param var.forbiden.last.char string vector (see details); characters to remove if they are the last character in the string
#' @param var.empty.starts.char strings vector (see details); characters that if found at the very beggining of the string could mean that the variable is empty
#' @param guess.factor boolean (default=TRUE); try to guess variables that are indeed factors
#' @param max.factor.cat num (default=10); max number of unique values after which a variable is not considered a factor anymore
#' @param verbose boolean (default=TRUE); if you want the function to be verbose
#' @param DEBUG boolean (default=FALSE); even more verbose
#'
#' @return data.frame; data frame with variable names corrected and nameless vars removed
#'
#' @seealso
#'
#' This function wrap [strClean][feR::strClean()] for string cleaning (used to clean var names), [feR::loadFile()], wrapped by the function
#'
#' @seealso [roxygen2::roxygenize()], `browseVignettes("roxygen2")`
#'
#' @examples
#'
#' @importFrom crayon bold green red
#' @export
loadData <- function(file = NULL, sheetIndex=1, folder = "DATOS/",
var.to.upper = TRUE,
var.forbiden.chars = c(" ",".",":",";","-","__","(",")","[","]","{","}","$","%","&","/","\\","!"),
var.forbiden.last.char = c("_"," "),
var.empty.starts.char = c("_"),
guess.factor = TRUE,
max.factor.cat = 10,
verbose=TRUE,
DEBUG=FALSE) {
crayon <- "crayon" %in% installed.packages()
if (crayon) {
crayon.x <- crayon::red(crayon::bold("✗ "))
crayon.v <- crayon::green(crayon::bold("✔ "))
} else {
crayon.x <- "✗ "
crayon.v <- "✔ "
}
data <- tryCatch(feR::loadFile(file, folder = folder, sheetIndex = sheetIndex),
error = function(e) {
message(crayon.x,"[loadData] File not loaded")
}
)
# data <- feR::loadFile(file, folder = folder, sheetIndex = sheetIndex)
if(!is.data.frame(data)) stop("No data could be loaded")
if(verbose) message("[loadData] loading data")
data <- feR::cleanVarNames(data)
data <- feR::removeNamelessVars(data)
if(guess.factor){
if(verbose) message("[loadData] guessing factors")
for (v in names(data)) {
value <- dplyr::pull(data,v)
feR:::.guess.factor(value, max.factor.cat = max.factor.cat)
data[,v] <- value
}
}
return(data)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.