The connector.sharepoint package provides a convenient interface for accessing and interacting with Microsoft SharePoint sites directly from R.
Introduction
This vignette will guide you through the process of connecting to a SharePoint site, retrieving data, and performing various operations using this package.
Connecting to a SharePoint Site
Without connector package
To get started, you need to establish a connection to your SharePoint
site. Use the ConnectorSharepoint()
function to
authenticate and connect to your SharePoint site. Here’s an example of
how to do this:
library(connector.sharepoint)
# Connect to SharePoint
con <- connector_sharepoint(site_url = "sharepoint_url")
With connector package (recommended)
If you are using the connector package, you can connect to a
SharePoint site using the connect()
function. This function
based on a configuration file or a list creates a
connectors()
object with a connector
for each
of the specified datasources (for detailed explanation have a look at
connector package).
Configuration file for connecting to SharePoint should look like this:
metadata:
trial:"trial_name"
project:"project_name"
sharepoint:"https://my_organisation.sharepoint.com"
datasources:
-name:"adam"
backend:
type:"connector.sharepoint::connector_sharepoint"
site_url:"{metadata.sharepoint}/sites/{metadata.project}_{metadata.trial}_adam"
-name:"output"
backend:
type:"connector.sharepoint::connector_sharepoint"
site_url:"{metadata.sharepoint}/sites/{metadata.project}_{metadata.trial}_output"
Save this to _connector.yml
file and use the
connect()
function to connect to SharePoint:
Now you can access the SharePoint site using the db
object and adam
field inside of it.
# Connection to SharePoint site. This will print object details
db$adam
After the setup is done we can use this connection to manipulate Sharepoint data.
Reading and writing data
The connector
packages provide a set of functions to
read and write data from the datasources. They all have similar
interface, so it’s easy to switch between them.
Now, we will show how to read and write different types data from/to
Sharepoint. In these examples we will be using iris
and
mtcars
datasets.
Here is an example of writing data to the ADaM table:
library(dplyr)
# Manipulate data
## Iris data
setosa <- iris |>
filter(Species == "setosa")
mean_for_all_iris <- iris |>
group_by(Species) |>
summarise_all(list(mean, median, sd, min, max))
## Mtcars data
cars <- mtcars |>
filter(mpg > 22)
mean_for_all_mtcars <- mtcars |>
group_by(gear) |>
summarise(
across(
everything(),
list("mean" = mean, "median" = median, "sd" = sd, "min" = min, "max" = max),
.names = "{.col}_{.fn}"
)
) |>
tidyr::pivot_longer(
cols = -gear,
names_to = c(".value", "stat"),
names_sep = "_"
)
## Store data
db$adam |>
write_cnt(setosa, "setosa", overwrite = TRUE)
db$adam |>
write_cnt(mean_for_all_iris, "mean_iris", overwrite = TRUE)
db$adam |>
write_cnt(cars, "cars_mpg", overwrite = TRUE)
db$adam |>
write_cnt(mean_for_all_mtcars, "mean_mtcars", overwrite = TRUE)
Now, let’s read the data back manipulate it a bit and write it to the SharePoint. This way we can save different types of data in different formats.
library(gt)
library(tidyr)
library(ggplot2)
# List and load data
db$adam |>
list_content_cnt()
table <- db$adam |>
read_cnt("mean_mtcars")
gttable <- table |>
gt(groupname_col = "gear")
# Save nontabular data to sharepoint
tmp_file <- tempfile(fileext = ".docx")
gtsave(gttable, tmp_file)
db$output |>
upload_cnt(tmp_file, "tmeanallmtcars.docx")
# Manipulate data
setosa_fsetosa <- db$adam |>
read_cnt("setosa") |>
filter(Sepal.Length > 5)
fsetosa <- ggplot(setosa) +
aes(x = Sepal.Length, y = Sepal.Width) +
geom_point()
## Store data into output location
db$output |>
write_cnt(fsetosa$data, "fsetosa.csv")
db$output |>
write_cnt(fsetosa, "fsetosa.rds")
tmp_file <- tempfile(fileext = ".png")
ggsave(tmp_file, fsetosa)
db$output |>
upload(tmp_file, "fsetosa.png")