Reading OneDrive and SharePoint files with read365 R package

R
read365
Author

Artur Quirino

Published

February 8, 2023

Modified

February 20, 2023

In this post, I would like to introduce four functions available in the read365 package that allow for the reading of OneDrive and SharePoint files: OneDrive_file, SharePoint_file, OneDrive_shared_file and SharePoint_shared_file. The former two functions are intended for private content, while the last two are designed for public files. It is worth noting that read365 is heavily reliant on the Microsoft365R package.

The goal of the read365 package is to allow users to read files from office365 directly in R, without the need to sync files between their computer and the cloud.

Reading private content from OneDrive

The OneDrive_file function executes the login process onto OneDrive by utilizing Azure Active Directory authentication. Upon successful authentication, OneDrive_file requires users to set three parameters: the location of the file (file), the desired function for reading the file (.function), and optionally, arguments for this function (...). See the example below.

#remotes::install_github("arturhgq/read365")
read365::OneDrive_file(
  "CienciaPolitica/data/read365_examples/OneDrive_file.xlsx",
  readxl::read_excel, 
  sheet = 1
)
Loading Microsoft Graph login for default tenant
Access token has expired or is no longer valid; refreshing
# A tibble: 3 × 3
   col1  col2 `private content`
  <dbl> <dbl> <chr>            
1     1     2 TRUE             
2     3     4 TRUE             
3     5     6 TRUE             

Reading private content from SharePoint

Shortly, the usage of the SharePoint_file is similiar to OneDrive_file. However, there are two additional parameters that must be set: SharePoint and drive. In the SharePoint parameter, the user is required to specify the SharePoint URL where the target file is located. As SharePoint may contain multiple drives to store files, users must indicate the specific drive1 to be accessed too. In most cases, “Documents” is the primary drive for storing files in SharePoint.

read365::SharePoint_file(
  SharePoint = "https://arturhgq.sharepoint.com",
  drive = "Documentos",
  file = "Sharepoint_file.xlsx",
  .function = readxl::read_excel
)
Loading Microsoft Graph login for default tenant
Access token has expired or is no longer valid; refreshing
# A tibble: 3 × 4
   col1  col2 `private content` SharePoint
  <dbl> <dbl> <chr>             <chr>     
1     1     2 TRUE              TRUE      
2     3     4 TRUE              TRUE      
3     5     6 TRUE              TRUE      

Reading public content from OneDrive or SharePoint

To use OneDrive_shared_file or SharePoint_shared_file2 no authentication is required. Therefore, all that is needed is to provide the file’s URL along with the corresponding function for reading the file and, optionally, its respective parameters.

 url_OneDrive = 'https://arturhgq-my.sharepoint.com/:x:/p/contact/EZ_KJ3cqtIVEh4PdXhTGy7IBpWT5_Zlp2VjYwlgVCPK4oQ?e=UZGQZt'
 url_SharePoint = 'https://arturhgq.sharepoint.com/:x:/g/EZOMb55WRsZFhXuo5K8uGXYBaRdGPNhX7lFNDjLpphj-mw?e=nCm0L4'
 
 read365::OneDrive_shared_file(
    url_OneDrive,
    .function = readxl::read_excel,
    sheet = 1
  )
# A tibble: 3 × 2
   col1  col2
  <dbl> <dbl>
1     1     2
2     3     4
3     5     6
  read365::SharePoint_shared_file(
    url_SharePoint,
    .function = readxl::read_excel,
    sheet = 1
  )
# A tibble: 3 × 4
   col1  col2 `private content` SharePoint
  <dbl> <dbl> <chr>             <chr>     
1     1     2 TRUE              TRUE      
2     3     4 TRUE              TRUE      
3     5     6 TRUE              TRUE      

Footnotes

  1. In case the appropriate drive is unknown, a list of drives can be obtained by utilizing the read365::get_drives function.↩︎

  2. Internally, both functions are identical.↩︎