## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----eval=FALSE---------------------------------------------------------------
# library(connector.sharepoint)
# 
# # Connect to SharePoint
# con <- connector_sharepoint(site_url = "sharepoint_url")

## ----eval=FALSE---------------------------------------------------------------
# # An example of configuration file
# metadata:
#   trial: "0001"
#   extra_class: "adam_connector"
#   url: !!expr Sys.getenv("SHAREPOINT_SITE_URL")
# 
# datasources:
#   - name: "adam"
#     backend:
#       type: "connector.sharepoint::connector_sharepoint"
#       site_url: "{metadata.url}"
#       folder: "{metadata.trial}/adam"
#       extra_class: "{metadata.extra_class}"
#   - name: "output"
#     backend:
#       type: "connector.sharepoint::connector_sharepoint"
#       site_url: "{metadata.url}"
#       folder: "{metadata.trial}/output"
# 

## ----eval=FALSE---------------------------------------------------------------
# library(connector)
# 
# # Create connector object
# db <- connect()

## ----eval=FALSE---------------------------------------------------------------
# # Connection to SharePoint site. This will print object details
# db$adam

## ----eval=FALSE---------------------------------------------------------------
# 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(x = setosa, name = "setosa.csv", overwrite = TRUE)
# 
# db$adam |>
#   write_cnt(mean_for_all_iris, "mean_iris.csv", overwrite = TRUE)
# 
# db$adam |>
#   write_cnt(cars, "cars_mpg.csv", overwrite = TRUE)
# 
# db$adam |>
#   write_cnt(mean_for_all_mtcars, "mean_mtcars.csv", overwrite = TRUE)

## ----eval=FALSE---------------------------------------------------------------
# library(gt)
# library(tidyr)
# library(ggplot2)
# 
# # List and load data
# db$adam |>
#   list_content_cnt()
# 
# table <- db$adam |>
#   read_cnt("mean_mtcars.csv")
# 
# 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.csv") |>
#   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_cnt(tmp_file, "fsetosa.png")

