The series of functions in
qiverse.sharepoint are wrappers of
Microsoft365R functions to perform some key tasks such as
downloading and uploading files to SharePoint, and downloading data from
a SharePointlist. The Microsoft365R package provides a lot
of functionality that leverages off the Microsoft Graph API, including
the ability to create, modify and delete lists, manage SharePoint sites
and many more.
This package provides three SharePointfunctions:
download_sharepoint_file
upload_sharepoint_file
download_sharepoint_list
Each of these will be covered in more detail below.
Download SharePoint File
This function uses the Microsoft365R package to download
any file in SharePoint, which can either be loaded directly in-memory to
R or to a specific location on your local computer. The
download_sharepoint_file function looks as follows:
download_sharepoint_file(
site_url,
file_url,
token,
download,
download_dest
)
With descriptions for the arguments as follows:
site_url - This is the URL when first accessing the
SharePoint team via your web browser (Home page)
file_url - This is a direct link to the file to be
downloaded. It can be obtained by navigating to the file you want to
download, right-clicking and selecting “Copy Link”
token - The token generated with the correct
SharePoint permissions. Use get_az_tk(’sp”) to create this
token.
download - Set this to FALSE to download the file
into a temporary file. This temporary file can then be read in using any
standard data reading function. Set this to TRUE to download the file to
your computer.
download_dest - This option is only active when
download is set to TRUE. When download_location is set to NULL, the file
is just downloaded into the current working directory. Otherwise,
specify the destination path to save the file.
The three use cases for this function is as below:
Download SharePoint File to Working Directory
# Create SharePoint azure token
library(qiverse.azure)
tk <- get_az_tk('sp')
# Download file to working directory from SharePoint site
library(qiverse.sharepoint)
download_sharepoint_file(
site_url = "https://myexample.sharepoint.com/sites/Example-Files/"),
file_url = paste0("https://myexample.sharepoint.com/:x:/r/sites/Example-Files/Shared%20Documents/Example_datasets/example_data.csv?d=XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
token = tk,
download = TRUE
)
Download SharePoint File to Selected Directory
# Create SharePoint azure token
library(qiverse.azure)
tk <- get_az_tk('sp')
# Download file to C:/ drive from SharePoint
library(qiverse.sharepoint)
download_sharepoint_file(
site_url = "https://myexample.sharepoint.com/sites/Example-Files/"),
file_url = paste0("https://myexample.sharepoint.com/:x:/r/sites/Example-Files/Shared%20Documents/Example_datasets/example_data.csv?d=XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
token = tk,
download = TRUE,
download_dest = 'C:/test.csv'
)
Download SharePoint File and Load In-Memory to R
# Create SharePoint azure token
library(qiverse.azure)
tk <- get_az_tk('sp')
# Example to download to tempfile, to load straight into R memory
library(qiverse.sharepoint)
data <- download_sharepoint_file(
site_url = "https://myexample.sharepoint.com/sites/Example-Files/"),
file_url = paste0("https://myexample.sharepoint.com/:x:/r/sites/Example-Files/Shared%20Documents/Example_datasets/example_data.csv?d=XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
token = tk,
download = FALSE
) |>
# Pipe in the approprate function to read in the data
read.csv()
Upload SharePoint File
This function uses the Microsoft365R package to upload
any file into SharePoint. The upload_sharepoint_file
function looks as follows:
upload_sharepoint_file(
src,
site_url,
dest_fldr_url,
token
)
With descriptions for the arguments as follows:
src -The file path to the local file that is being
uploaded to SharePoint
site_url - This is the url when first accessing the
SharePoint team via your web browser (Home page)
file_url - This is a direct link to the SharePoint
folder where the file will be uploaded to. It can be obtained by
navigating to the folder you want to upload to, right-clicking and
selecting “Copy Link”
token - The token generated with the correct
SharePoint permissions. Use get_az_tk(’sp”) to create this
token.
Here is an example code block of this function:
# Create SharePoint azure token
library(qiverse.azure)
tk <- get_az_tk('sp')
# Example to upload local file to SharePoint folder
library(qiverse.sharepoint)
upload_sharepoint_file(
src = 'TestFiles/test.txt',
site_url = "https://myexample.sharepoint.com/sites/Example-Files/"),
file_url = paste0("https://myexample.sharepoint.com/:x:/r/sites/Example-Files/Shared%20Documents/Example_datasets/example_data_amended.csv?d=XXXXXXXXXXXXXXXXXXXXXXXXXXXX",
token = tk
)
Download SharePoint List
The download_sharepoint_list function is able to
download SharePoint lists into a data.frame object. The function looks
as follows:
download_sharepoint_list(
list_url,
token
)
With the arguments defined as:
Here is an example code block of this function:
# Get the token for SharePoint
library(qiverse.azure)
tk <- get_az_tk('sp')
# Download a particular view for the SharePoint list
library(qiverse.sharepoint)
list_data <- download_sharepoint_list(
paste0("https://myexample.sharepoint.com/sites/",
"Field%20Metadata/Lists/Fields/",
"Latest.aspx"),
token = tk
)
# Download the default view for the SharePoint list
list_data <- download_sharepoint_list(
paste0("https://myexample.sharepoint.com/sites/",
"Field%20Metadata/Lists/Fields"),
token = tk
)
Other SharePoint Functionality
The functions available in qiverse.sharepoint serve as
easy-to-use wrappers of the Microsoft365R package, to
conduct key functions such as downloading and uploading SharePoint
files, and downloading SharePoint lists. More functionality is available
directly in the package such as create, modify and delete lists, and
managing SharePoint sites.