diff --git a/DESCRIPTION b/DESCRIPTION index 171e768..13c71d5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -34,6 +34,7 @@ Suggests: testthat, blastula, emayili, - readr + readr, + readxl Roxygen: list(markdown=TRUE, r6=FALSE) RoxygenNote: 7.2.1 diff --git a/NEWS.md b/NEWS.md index cd64587..1c18522 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,7 @@ ## OneDrive/SharePoint - In the `ms_drive_item$load_dataframe()` method, pass the `...` argument to `read_delim`. +- Add the ability to load Excel files (with extension .xls or .xlsx) to the `ms_drive_item$load_dataframe()` method. This requires the readxl package to be installed. ## Planner diff --git a/R/ms_drive_item.R b/R/ms_drive_item.R index bd363eb..518e958 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 a delimited file and return its contents as a data frame. See 'Saving and loading data' below. +#' - `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_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 a delimited file and returns its contents as a data frame. The delimiter can be specified 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 the file, otherwise `utils::read.delim` is used. You can supply other arguments to the parsing function 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. +#' - `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_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. @@ -421,6 +421,17 @@ public=list( { private$assert_is_file() ext <- tolower(tools::file_ext(self$properties$name)) + + if(ext %in% c("xls", "xlsx")) + { + if(!requireNamespace("readxl")) + stop("The readxl package must be installed to load an Excel spreadsheet") + infile <- paste0(tempfile(), ".", ext) + on.exit(unlink(infile)) + self$download(dest=infile) + return(readxl::read_excel(infile, ...)) + } + if(is.null(delim)) { delim <- if(ext == "csv") "," else if(ext == "csv2") ";" else "\t" diff --git a/man/ms_drive_item.Rd b/man/ms_drive_item.Rd index 1bb8af5..18764db 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 a delimited file and return its contents as a data frame. See 'Saving and loading data' below. +\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_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 a delimited file and returns its contents as a data frame. The delimiter can be specified 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 the file, otherwise \code{utils::read.delim} is used. You can supply other arguments to the parsing function 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. +\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_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.