Skip to content

Commit

Permalink
Load excel file as data frame (#199)
Browse files Browse the repository at this point in the history
* use readxl to load df from xls file

* tidy up, document

* update news
  • Loading branch information
hongooi73 authored Feb 15, 2024
1 parent ae8538e commit 569dab1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Suggests:
testthat,
blastula,
emayili,
readr
readr,
readxl
Roxygen: list(markdown=TRUE, r6=FALSE)
RoxygenNote: 7.2.1
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 14 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 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.
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 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.
Expand Down Expand Up @@ -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"
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.

0 comments on commit 569dab1

Please sign in to comment.