Using Microsoft365R, if you want to work with a file hosted on OneDrive or SharePoint, you need to follow these steps:
While this isn’t necessarily difficult, it gives the user a bit more responsibility than the typical workflow of working with files locally. For example, given that the file was downloaded locally you may want to clean that up instead of leaving the local artifact on your system. Furthermore, using this process flow you may have to redundantly read a file down from the cloud even thought it wasn’t updated.
m365filer aims to take much of this responsibility out of the hands of the user by managing the uploading and downloading of files from OneDrive or SharePoint for you. In doing so, it also attempts to make this process feel as natural and close to using local files as possible.
Let’s consider the following example.
library(m365filer)
library(dplyr)
od <- Microsoft365R::get_personal_onedrive()
# Read a file down from OneDrive
test_file <- read.csv(onedrive_file('Documents/test.csv', drive=od))
test_file %>%
head()
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#> Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
Note that I provided the function onedrive_file()
to the file
parameter of read.csv()
. By doing this, the following sequence of events happened:
onedrive_file()
returned a file connection objectread.csv()
, which returned the opened data.frame.Another interesting tidbit about m365filer is that it makes its best attempt to avoid repeatedly downloading files from the cloud. It does this by checking the timestamp from the cloud version of the file against the local copy of the file to determine if the file has been updated, and only when the file has been updated with it re-download the file.
The same function can additionally be used to write files to the cloud as well:
library(dplyr)
test_file <- test_file %>%
mutate(
new = vs + am
)
options(m365filer.onedrive = od)
test_file %>%
write.csv(onedrive_file('Documents/test.csv'))
updated_file <- read.csv(onedrive_file('Documents/test.csv'))
updated_file %>%
head()
#> mpg cyl disp hp drat wt qsec vs am gear carb new
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 1
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 1
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 2
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 1
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 0
#> Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 1
Notice that in this example, I set the option m365filer.onedrive
. Instead of repeatedly referring to the drive object, you can tuck this away in an option. A complimentary option m365filer.spdrive
can be used when working with SharePoint files as well.