#' @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
#' @param ... parameters passed to the import function. See details.
#' @return a data frame
#' @details The \code{import} function is a wrapper for the haven, readxl, and
#' vroom packages.
#' @examples
#' \dontrun{
#' if(interactive()){
#' mydataframe <- import("mydata.csv")
#' }
#' }
#' @seealso
#' \code{\link[tools]{fileutils}}
#' \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
import <- function(file, ...){
# if no file specified, prompt user
if(missing(file))
file <- file.choose()
# 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, ...)
)
# return data frame
return(df)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.