-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add vignette on reading and writing items
- Loading branch information
1 parent
8c87551
commit 000743f
Showing
3 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
--- | ||
title: "Reading and writing items from SharePoint" | ||
--- | ||
|
||
```{r, include = FALSE} | ||
knitr::opts_chunk$set( | ||
collapse = TRUE, | ||
comment = "#>" | ||
) | ||
``` | ||
|
||
Note: this vignette is pre-computed to utilize the Microsoft Graph login that is only available locally to me as a logged in SharePoint user. | ||
|
||
```{r setup} | ||
library(sharepointr) | ||
``` | ||
|
||
## Downloading and reading files from SharePoint | ||
|
||
You can use `download_sp_item()` to download files or folders from SharePoint: | ||
|
||
```{r} | ||
docx_url <- "https://bmore.sharepoint.com/:w:/r/sites/MayorsOffice-DataGovernance/Policy%20Documents/Data%20Classification%20Standard.docx?d=w54a9ae7eaa894e94b6d6d14516f3aaa4&csf=1&web=1&e=ee7ZSX" | ||
|
||
download_sp_item( | ||
path = docx_url, | ||
new_path = tempdir() | ||
) | ||
``` | ||
|
||
For files on SharePoint, `read_sharepoint()` extends `download_sp_item()` by downloading the selected item to a temporary folder by default and, depending on the file extension, tries to read the file using `{readr}`, `{readxl}`, `{officer}`, or `{sf}`. | ||
|
||
```{r} | ||
docx <- read_sharepoint(docx_url) | ||
|
||
docx | ||
``` | ||
|
||
## Writing and uploading files to SharePoint | ||
|
||
You can use `upload_sp_item()` to upload a local file to a SharePoint folder or document library. | ||
|
||
```{r} | ||
folder_url <- "https://bmore.sharepoint.com/:f:/r/sites/MayorsOffice-DataGovernance/Shared%20Documents/RStats?csf=1&web=1&e=S1XxVU" | ||
|
||
upload_sp_item( | ||
file = system.file("gpkg/nc.gpkg", package = "sf"), | ||
dest = folder_url | ||
) | ||
``` | ||
|
||
Using `read_sharepoint()`, we can confirm that the file has been uploaded: | ||
|
||
```{r} | ||
sp_drive <- get_sp_drive(folder_url) | ||
|
||
nc <- read_sharepoint( | ||
"RStats/nc.gpkg", | ||
drive = sp_drive | ||
) | ||
|
||
plot(nc["AREA"]) | ||
``` | ||
|
||
`write_sharepoint()` extends `upload_sp_item()` by allowing you to pass an R object instead of a file path. Like `read_sharepoint()` tries to guess the appropriate input function, `write_sharepoint()` tries to guess the appropriate output function based on the object class. | ||
|
||
```{r} | ||
write_sharepoint( | ||
mtcars, | ||
file = "mtcars.csv", | ||
dest = folder_url | ||
) | ||
``` | ||
|
||
To wrap up this example, we need to remove the uploaded files from SharePoint to keep a tidy shared file system. | ||
|
||
`delete_sp_item()` supports the option to use a shared item URL to select which file to remove but, in this case, it is easier to set the `drive` argument along with a relative filepath: | ||
|
||
```{r} | ||
# Remove the file | ||
delete_sp_item( | ||
file.path("RStats", "nc.gpkg"), | ||
drive = sp_drive, | ||
confirm = FALSE | ||
) | ||
|
||
delete_sp_item( | ||
file.path("RStats", "mtcars.csv"), | ||
drive = sp_drive, | ||
confirm = FALSE | ||
) | ||
``` | ||
|