|
|
@@ -91,16 +91,7 @@ charger <- function(fichier,feuille=1,chemin="../../data/") |
|
|
|
} |
|
|
|
|
|
|
|
# Lecture des labels a partir du fichier SAS |
|
|
|
if (file.exists("labels.sas")) |
|
|
|
{ |
|
|
|
con=file("labels.sas","r",encoding="native.enc") |
|
|
|
labelsfile=readLines(con) |
|
|
|
close(con) |
|
|
|
labels=labelsfile[grepl('^ *\\w*?[[:space:]]*=[[:space:]]*\\".*?\\" *$',labelsfile)] |
|
|
|
labels=paste(labels,collapse=",") |
|
|
|
label_exe = paste("label(x)<-c(",labels,")") |
|
|
|
eval(parse(text=label_exe)) |
|
|
|
} |
|
|
|
x <- loadLabels(x,"labels.sas") |
|
|
|
|
|
|
|
# Remplacement des "" par NA dans les facteurs |
|
|
|
for (var in names(x)) |
|
|
@@ -110,4 +101,45 @@ charger <- function(fichier,feuille=1,chemin="../../data/") |
|
|
|
} |
|
|
|
|
|
|
|
return(x) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#' Load labels from a flat file |
|
|
|
#' |
|
|
|
#' Loads label values from a flat file and applies them to the provided dataframe |
|
|
|
#' |
|
|
|
#' This function takes a dataframe and adds label attributes to the variables. |
|
|
|
#' The file must be formatted like this : |
|
|
|
#' variablename = "Label for the variable" |
|
|
|
#' with one line per variable |
|
|
|
#' @encoding UTF-8 |
|
|
|
#' @param dataframe The dataframe to which you want to assign labels |
|
|
|
#' @param file The flat file from which to load the label data |
|
|
|
#' @return The dataframe with label attributes |
|
|
|
#' @examples |
|
|
|
#' \dontrun{df <- loadLabels(df,"labels.txt")} |
|
|
|
#' @export |
|
|
|
loadLabels <- function(dataframe,file) |
|
|
|
{ |
|
|
|
if (missing(dataframe) | missing(file)) |
|
|
|
{ |
|
|
|
warning("Dataframe or file missing") |
|
|
|
return -1 |
|
|
|
} |
|
|
|
|
|
|
|
if (!file.exists(file)) |
|
|
|
{ |
|
|
|
warning("File doesn't exists") |
|
|
|
return -1 |
|
|
|
} |
|
|
|
|
|
|
|
con=file(file,"r",encoding="native.enc") |
|
|
|
labelsfile=readLines(con) |
|
|
|
close(con) |
|
|
|
labels=labelsfile[grepl('^ *\\w*?[[:space:]]*=[[:space:]]*\\".*?\\" *$',labelsfile)] |
|
|
|
labels=paste(labels,collapse=",") |
|
|
|
label_exe = paste("label(dataframe)<-c(",labels,")") |
|
|
|
eval(parse(text=label_exe)) |
|
|
|
|
|
|
|
dataframe |
|
|
|
} |