Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 20 additions & 3 deletions R/ms_drive_item.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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"))
Expand Down
6 changes: 3 additions & 3 deletions man/ms_drive_item.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.