diff --git a/DESCRIPTION b/DESCRIPTION index 9dca2bf..85a778d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -35,6 +35,7 @@ Suggests: blastula, emayili, readr, - readxl + readxl, + haven Roxygen: list(markdown=TRUE, r6=FALSE) -RoxygenNote: 7.2.1 +RoxygenNote: 7.3.3 diff --git a/R/ms_drive_item.R b/R/ms_drive_item.R index f82a52b..337291a 100644 --- a/R/ms_drive_item.R +++ b/R/ms_drive_item.R @@ -26,7 +26,7 @@ #' - `get_parent_folder()`: Get the parent folder for this item, as a drive item object. Returns the root folder for the root. Not supported for remote items. #' - `get_path()`: Get the absolute path for this item, as a character string. Not supported for remote items. #' - `is_folder()`: Information function, returns TRUE if this item is a folder. -#' - `load_dataframe(delim=NULL, ...)`: Download an Excel or text file and return its contents as a data frame. See 'Saving and loading data' below. +#' - `load_dataframe(delim=NULL, ...)`: Download an Excel, SAS, Stata, SPSS, or text file and return its contents as a data frame. See 'Saving and loading data' below. #' - `load_rds()`: Download a .rds file and return the saved object. #' - `load_rdata(envir)`: Load a .RData or .Rda file into the specified environment. #' - `save_dataframe(df, file, delim=",", ...)` Save a dataframe to a delimited file. @@ -81,8 +81,8 @@ #' #' @section Saving and loading data: #' The following methods are provided to simplify the task of loading and saving datasets and R objects. -#' - `load_dataframe` downloads an Excel (.xlsx or .xls extension) or text file and returns its contents as a data frame. Loading Excel files uses the `readxl::read_excel` function, and so requires that the readxl package is installed. For a text file, you can specify the delimiter with the `delim` argument; if omitted, this is "," if the file extension is .csv, ";" if the file extension is .csv2, and a tab otherwise. If the readr package is installed, the `readr::read_delim` function is used to parse text files, otherwise `utils::read.delim` is used. You can supply other arguments to these functions via the `...` argument. -#' - `save_dataframe` is the inverse of `load_dataframe`: it uploads the given data frame to a folder item. Specify the delimiter with the `delim` argument. The `readr::write_delim` function is used to serialise the data if that package is installed, and `utils::write.table` otherwise. Note that unlike loading, saving to an Excel file is not supported. +#' - `load_dataframe` downloads an Excel (.xlsx or .xls extension), SAS (.sas7bdat, .sas7bcat, or .xpt), Stata (.dta), SPSS (.sav or .por), or text file and returns its contents as a data frame. Loading Excel files uses the `readxl::read_excel` function, and so requires that the readxl package is installed. Loading SAS, Stata, or SPSS files uses the corresponding `haven::read_` function for each extension, and so requires that the haven package be installed. For a text file, you can specify the delimiter with the `delim` argument; if omitted, this is "," if the file extension is .csv, ";" if the file extension is .csv2, and a tab otherwise. If the readr package is installed, the `readr::read_delim` function is used to parse text files, otherwise `utils::read.delim` is used. You can supply other arguments to these functions via the `...` argument. +#' - `save_dataframe` is the inverse of `load_dataframe`: it uploads the given data frame to a folder item. Specify the delimiter with the `delim` argument. The `readr::write_delim` function is used to serialise the data if that package is installed, and `utils::write.table` otherwise. Note that unlike loading, saving to an Excel, SAS, Stata, or SPSS file is not supported. #' - `load_rds` downloads a .rds file and returns its contents as an R object. It is analogous to the base `readRDS` function but for OneDrive/SharePoint drive items. #' - `save_rds` uploads a given R object as a .rds file, analogously to `saveRDS`. #' - `load_rdata` downloads a .RData or .Rda file and loads its contents into the given environment. It is analogous to the base `load` function but for OneDrive/SharePoint drive items. @@ -422,6 +422,23 @@ public=list( private$assert_is_file() ext <- tolower(tools::file_ext(self$properties$name)) + if(ext %in% c("sav","zsav","por","sas7bdat","sas7bcat","dta","xpt")) + { + if(!requireNamespace("haven")) + stop("The haven package must be installed to load a file in a SAS, Stata, or SPSS format") + infile <- paste0(tempfile(),".",ext) + on.exit(unlink(infile)) + self$download(dest=infile) + if(ext %in% c("sav","zsav","por")) + return(haven::read_spss(infile,...)) + if(ext %in% c("sas7bdat","sas7bcat")) + return(haven::read_sas(infile,...)) + if(ext == "dta") + return(haven::read_dta(infile,...)) + if(ext == "xpt") + return(haven::read_xpt(infile,...)) + } + if(ext %in% c("xls", "xlsx")) { if(!requireNamespace("readxl")) diff --git a/man/ms_drive_item.Rd b/man/ms_drive_item.Rd index 4cb99ea..d575b18 100644 --- a/man/ms_drive_item.Rd +++ b/man/ms_drive_item.Rd @@ -40,7 +40,7 @@ Class representing an item (file or folder) in a OneDrive or SharePoint document \item \code{get_parent_folder()}: Get the parent folder for this item, as a drive item object. Returns the root folder for the root. Not supported for remote items. \item \code{get_path()}: Get the absolute path for this item, as a character string. Not supported for remote items. \item \code{is_folder()}: Information function, returns TRUE if this item is a folder. -\item \code{load_dataframe(delim=NULL, ...)}: Download an Excel or text file and return its contents as a data frame. See 'Saving and loading data' below. +\item \code{load_dataframe(delim=NULL, ...)}: Download an Excel, SAS, Stata, SPSS, or text file and return its contents as a data frame. See 'Saving and loading data' below. \item \code{load_rds()}: Download a .rds file and return the saved object. \item \code{load_rdata(envir)}: Load a .RData or .Rda file into the specified environment. \item \code{save_dataframe(df, file, delim=",", ...)} Save a dataframe to a delimited file. @@ -110,8 +110,8 @@ This method returns a URL to access the item, for \code{type="view"} or "\verb{t The following methods are provided to simplify the task of loading and saving datasets and R objects. \itemize{ -\item \code{load_dataframe} downloads an Excel (.xlsx or .xls extension) or text file and returns its contents as a data frame. Loading Excel files uses the \code{readxl::read_excel} function, and so requires that the readxl package is installed. For a text file, you can specify the delimiter with the \code{delim} argument; if omitted, this is "," if the file extension is .csv, ";" if the file extension is .csv2, and a tab otherwise. If the readr package is installed, the \code{readr::read_delim} function is used to parse text files, otherwise \code{utils::read.delim} is used. You can supply other arguments to these functions via the \code{...} argument. -\item \code{save_dataframe} is the inverse of \code{load_dataframe}: it uploads the given data frame to a folder item. Specify the delimiter with the \code{delim} argument. The \code{readr::write_delim} function is used to serialise the data if that package is installed, and \code{utils::write.table} otherwise. Note that unlike loading, saving to an Excel file is not supported. +\item \code{load_dataframe} downloads an Excel (.xlsx or .xls extension), SAS (.sas7bdat, .sas7bcat, or .xpt), Stata (.dta), SPSS (.sav or .por), or text file and returns its contents as a data frame. Loading Excel files uses the \code{readxl::read_excel} function, and so requires that the readxl package is installed. Loading SAS, Stata, or SPSS files uses the corresponding \code{haven::read_} function for each extension, and so requires that the haven package be installed. For a text file, you can specify the delimiter with the \code{delim} argument; if omitted, this is "," if the file extension is .csv, ";" if the file extension is .csv2, and a tab otherwise. If the readr package is installed, the \code{readr::read_delim} function is used to parse text files, otherwise \code{utils::read.delim} is used. You can supply other arguments to these functions via the \code{...} argument. +\item \code{save_dataframe} is the inverse of \code{load_dataframe}: it uploads the given data frame to a folder item. Specify the delimiter with the \code{delim} argument. The \code{readr::write_delim} function is used to serialise the data if that package is installed, and \code{utils::write.table} otherwise. Note that unlike loading, saving to an Excel, SAS, Stata, or SPSS file is not supported. \item \code{load_rds} downloads a .rds file and returns its contents as an R object. It is analogous to the base \code{readRDS} function but for OneDrive/SharePoint drive items. \item \code{save_rds} uploads a given R object as a .rds file, analogously to \code{saveRDS}. \item \code{load_rdata} downloads a .RData or .Rda file and loads its contents into the given environment. It is analogous to the base \code{load} function but for OneDrive/SharePoint drive items.