-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.R
90 lines (79 loc) · 2.82 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#' @title Import a dataset
#' @description The \code{import} function can import data from
#' delimited text files, Excel spreadsheets, and statistical
#' packages such as SAS, SPSS, and Stata.
#' @param file datafile to import. If missing, the user is prompted to select a
#' file interactively.
#' @param ... parameters passed to the import function. See details.
#' @param quietly logical. If \code{quietly=FALSE}, the data frame is described
#' after import.
#' @return a data frame (tibble)
#' @details
#' The \code{import} function is a wrapper for the
#' \href{https://haven.tidyverse.org/}{haven},
#' \href{https://readxl.tidyverse.org/}{readxl}, and
#' \href{https://github.com/r-lib/vroom}{vroom} packages. The \code{vroom}
#' package provides one of the fastest methods for importing delimited
#' text files currently available.
#'
#' Files are imported based on their extension (\code{.sas7bdat} for SAS,
#' \code{.sav} for SPSS, \code{.dta} for Stata, and \code{.xls}
#' or \code{.xlsx} for Excel). All other file extensions are assumed to
#' belong to delimited text files (e.g., comma or tab delimited files).
#'
#' If you import a delimited text file without options, the structure
#' and delimiters are determined from the first 100 rows.You can override
#' these guesses by specifying \link[vroom:vroom]{options}
#' (e.g., \code{sep=";", na="999"}).
#' @examples
#' \dontrun{
#'
#' # import a comma delimited file
#' mydataframe <- import("mydata.csv")
#'
#' # import a SAS binary datafile and describe the results
#' mydataframe <- import("mydata.sas7bdat", quietly=FALSE)
#'
#' # import the second worksheet of an Excel workbook
#' mydataframe <- import("mydata.xlsx", sheet=2)
#'
#' # prompt for a file to import
#' mydataframe <- import()
#' }
#' @seealso
#' \code{\link[haven]{read_sas}},
#' \code{\link[haven]{read_dta}},
#' \code{\link[haven]{read_spss}},
#' \code{\link[readxl]{read_excel}},
#' \code{\link[vroom]{vroom}}
#' @rdname import
#' @export
#' @importFrom tools file_ext
#' @importFrom haven read_sas read_stata read_spss
#' @importFrom readxl read_excel
#' @importFrom vroom vroom
#' @importFrom dplyr glimpse
import <- function(file, quietly=TRUE, ...){
# if no file specified, prompt user
if(missing(file))
file <- file.choose()
print(paste("Importing:", file))
# get file info
file <- tolower(file)
basename <- basename(file)
extension <- tools::file_ext(file)
# import dataset
df <- switch(extension,
"sas7bdat" = haven::read_sas(file, ...),
"dta" = haven::read_stata(file, ...),
"sav" = haven::read_spss(file, ...),
"xlsx" = readxl::read_excel(file, ...),
"xls" = readxl::read_excel(file, ...),
vroom::vroom(file, ...)
)
if (!quietly){
dplyr::glimpse(df)
}
# return data frame
return(df)
}