Skip to contents

Read or write data to/from a Sharepoint drive. Can be used with default site/drive set by sp_defaults() or with a specified site/drive.

Currently supported file types include: .csv, .csv2, .tsv, .xls, .xlsx, .rds

These functions will attempt to use the appropriate read/write function based on the file extension, however this can be overridden by specifying type.

The ... parameter is passed on to the appropriate reading or writing function. See the details section for more information on these functions by type.

If the folder in path does not yet exist, the user will be prompted if they would like to create it.

Usage

sp_read(path, site = NULL, drive = NULL, type = NULL, ...)

sp_write(x, path, site = NULL, drive = NULL, type = NULL, ...)

Arguments

path

The location in the Sharepoint drive

site

Site identifier. Can be the site name, id, URL, or an ms_site object. If no site identifier is provided, uses the stored default site if it exists.

drive

Drive identifier. Can be the drive name, id, or an ms_drive object. If site is provided but drive is not, uses the first drive of the provided site. If neither is provided, uses the stored default drive if it exists.

type

Optional. One of "dataframe" (for delimited files), "xlsx", or "rds". Uses the file extension to determine type if not provided.

...

Additional arguments passed on to the reading/writing function.

x

The object to be written

Value

sp_read() returns an R object as specified by type. sp_write() returns x, invisibly

Details

For more information on methods (shown as $__() below) see documentation on Microsoft365R::ms_drive.

Reading Functions

  • ".csv", ".csv2", ".tsv" are read using the $load_dataframe() method, which uses readr::read_delim().

  • ".rds" is read using the $load_rds() method which accepts no additional arguments.

  • ".xls" and ".xlsx" are read using readxl::read_excel() (if installed). The function will download the excel file temporarily, then import it and delete the temporary copy

Writing Functions

  • ".csv", ".csv2", ".tsv" are written using the $save_dataframe() method and uses readr::write_delim(). Delimiter will be assumed by the extension unless provided in a delim argument

  • ".rds" is written using the $save_rds() method, which accepts no additional arguments

  • ".xlsx" is written using writexl::write_xlsx() (if installed) and then uploaded using the $upload_file() method.

See also

sp_upload(), sp_download(); $upload_file(), $download_file(), $save_rdata(), $load_rdata() from Microsoft365R::ms_drive

Examples

if (FALSE) { # interactive()

# Set site defaults
sp_defaults(site = "Data Analytics")

# Write a file
sp_write(mtcars, "mtcars.csv")

# Write a file, specifying type and adding additional parameters
sp_write(mtcars, "mtcars.txt", type = "dataframe", delim = "|")

# Read a file
x <- sp_read("mtcars.csv")
y <- sp_read("mtcars.txt", type = "dataframe", delim = "|")

# Save / load an .rdata file using ms_drive methods
dr <- sp_drive() # Get stored default ms_drive object
dr$save_rdata(x, y, file = "data.rdata")
dr$load_rdata("data.rdata")

}