Saving a file to Sharepoint with R
Someone from IT department helped us to solve the issue. As I said we were able to import data from share point using the link to the folder. However the same link did not work when trying to save on SharePoint. To be able to do this the IT team gave us a slightly changed link. See below the type of link we used to upload and the one that was recommended and that works.
https://siteadress/foldername/subfolder/docname.rda
the one that works is
\\\\siteadress@SSL\\foldername\\subfolder\\docname.rda
Saving files to SharePoint folder from R
I tried this and it worked:
system("curl --ntlm --user username:password --upload-file someFolder/FileToCopy.ext https://sites.somecompany.com/sites/sitename/Documents/UserDocumentFolder/FileToCopy.ext")
Uploading file to sharepoint through R
I have never heard of using R to load files into SharePoint. If you are already using Excel, just run a small VBA script to load the files into SharePoint.
Dim SharepointAddress As String
Dim LocalAddress As String
Dim objNet As Object
Dim FS As Object
' Where you will enter Sharepoint location path
SharepointAddress = "\\sharepoint path to document library" & "\"
' Where you will enter the file path, ex: Excel file
LocalAddress = "your file path"
Set objNet = CreateObject("WScript.Network")
Set FS = CreateObject("Scripting.FileSystemObject")
If FS.FileExists(LocalAddress) Then
FS.CopyFile LocalAddress, SharepointAddress
End If
Set objNet = Nothing
Set FS = Nothing
Accessing Excel file from Sharepoint with R
I use
library(readxl)
read_excel('//companySharepointSite/project/.../ExcelFilename.xlsx', 'Sheet1', skip=1)
Note, no https:
, and sometimes I have to open the file first (i.e., cut and paste //companySharepointSite/project/.../ExcelFilename.xlsx
into my browser's address bar)
how to download files from a sharepoint folder with R
Solution
I made a simple R Package called sharepointr to upload and download files from SharePoint.
The way I made the whole download/upload to sharepoint work from R was to:
- Make a SharePoint App Registration
- Add permissions to App Registration
- Getting "tenant_id" and "resource_id" using cURL
- Make get/post calls to API
It is all described in the package Readme.md
Example
# Installing the package
install.packages("devtools")
devtools::install_github("esbeneickhardt/sharepointr")
# Setting parameters
client_id <- "insert_from_first_step"
client_secret <- "insert_from_first_step"
tenant_id <- "insert_from_fourth_step"
resource_id <- "insert_from_fourth_step"
site_domain <- "yourorganisation.sharepoint.com"
sharepoint_url <- "https://yourorganisation.sharepoint.com/sites/MyTestSite"
# Getting a SharePoint Token
sharepoint_token <- get_sharepoint_token(client_id, client_secret, tenant_id, resource_id, site_domain)
# Getting sharepoint digest value
sharepoint_digest_value <- get_sharepoint_digest_value(sharepoint_token, sharepoint_url)
# Downloading a file
sharepoint_path <- "Shared Documents/test"
sharepoint_file_name <- "Mappe.xlsx"
out_path <- "C:/Users/User/Desktop/"
download_sharepoint_file(sharepoint_token, sharepoint_url, sharepoint_digest_value, sharepoint_path, sharepoint_file_name, out_path)
Related Topics
Specifying the Colour Scale for Maps in Ggplot
R Calculate the Average of One Column Corresponding to Each Bin of Another Column
Converting Utc Time to Local Standard Time in R
R Dataframe with Varied Column Lengths
Efficiently Counting Non-Na Elements in Data.Table
Unpacking and Merging Lists in a Column in Data.Frame
Alpha Aesthetic Shows Arrow's Skeleton Instead of Plain Shape - How to Prevent It
R: Ggplot2: Adding Count Labels to Histogram with Density Overlay
Adding All Elements of Two Lists
How to Count Sequences of Ones in a Logical Vector
Calculating Prediction Accuracy of a Tree Using Rpart's Predict Method
Pass String as Name of Attached Data Column Name
Highlight Areas Within Certain X Range in Ggplot2
R - Min, Max and Mean of Off-Diagonal Elements in a Matrix
How to Show a Loading Screen When the Output Is Being Calculated in a Background Process
How to Load Xlsx File Using Fread Function
Rscript Detect If R Script Is Being Called/Sourced from Another Script