From 5fe5f10115e39ce30f85fbb7fae9529f0f1addbe Mon Sep 17 00:00:00 2001 From: Hong Ooi Date: Thu, 15 Feb 2024 00:17:33 +1100 Subject: [PATCH] use readxl to load df from xls file --- DESCRIPTION | 3 ++- R/ms_drive_item.R | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) 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/R/ms_drive_item.R b/R/ms_drive_item.R index bd363eb..36aa845 100644 --- a/R/ms_drive_item.R +++ b/R/ms_drive_item.R @@ -421,6 +421,19 @@ public=list( { private$assert_is_file() ext <- tolower(tools::file_ext(self$properties$name)) + if(!(ext %in% c("csv", "csv2", "xls", "xlsx"))) + stop("Data frame can only be loaded from a CSV or Excel file") + + 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) + self$download(dest=infile) + on.exit(unlink(infile, force=TRUE)) + return(readxl::read_excel(infile, ...)) + } + if(is.null(delim)) { delim <- if(ext == "csv") "," else if(ext == "csv2") ";" else "\t"