-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.R
34 lines (31 loc) · 1.23 KB
/
import.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import_delim <- function(file, sep, dec){
tab <- NULL
enc <- readr::guess_encoding(file, threshold = .9)$encoding[1]
if (is.na(enc) | enc == "ASCII") enc <- ""
if (tolower(tools::file_ext(file)) == "txt" | sep != "\t" | dec != "."){
tab <- read.delim(file, strip.white = TRUE, fileEncoding = enc, na.strings=c("NA", "", " ", "."),
stringsAsFactors = FALSE, check.names = FALSE, sep = sep, dec = dec)
} else if (tolower(tools::file_ext(file)) == "csv"){
tab <- import_csv(file, enc = enc)
}
tab
}
import_csv <- function(file, enc = "") {
tab <- read.csv2(file, na.strings=c("NA", "", " ", "."), fileEncoding = enc,
strip.white = TRUE, stringsAsFactors = FALSE, check.names = FALSE)
if(length(tab) == 1){
tab <- read.csv(file, na.strings=c("NA", ""," ", "."), strip.white = TRUE, fileEncoding = enc,
stringsAsFactors = FALSE, check.names = FALSE)
}
tab
}
import_file <- function(x, sep = "\t", dec = "."){
ext <- tools::file_ext(x)
if(ext %in% c("csv", "txt")){
import_delim(x, sep = sep, dec = dec)
} else if (ext %in% c("xls", "xlsx", "xlsm")){
return(read_excel(x))
} else {
toast_showNotif("Extension de fichier non prise en charge")
}
}