Read and Write Excel from Azure Datalake Store using R and Rest API

  06 Sep 2019
   azure, r

The following code snippets are on creating a connection to Azure Data Lake Storage Gen1 using R with Service-to-Service authentication with client secret and client id using REST API and read and write an excel file.


Import Prerequisite

library(httr)
library(curl)  
library(stringr)
library(readxl) 

Authenticate

authentication_token <- function(tenant, client_id, client_secret){
  h <- new_handle()
  handle_setform(h,
                 "grant_type"="client_credentials",
                 "resource"="https://management.core.windows.net/",
                 "client_id" = client_id,
                 "client_secret" = client_secret
  )
  path = stringr::str_interp("https://login.windows.net/${tenant}/oauth2/token")
  req <- curl_fetch_memory(path, handle = h)
  res <- fromJSON(rawToChar(req$content))
  return(paste("Bearer",res$access_token))
}

// Generate token using above created function
token <- authentication_token(tenant = "TENANT",
                              client_id = "CLIENT ID",
                              client_secret = "CLIENT SECRET")

  Access Azure Active Directory Groups using R

  02 Sep 2019
   azure, r

The following code snippets are on creating a connection to Azure Active Directoy using R with Service-to-Service authentication with client secret and client id using REST API and fetch Active Directory`s groups and associated details with them like owners and memebers.


Import Prerequisite

library(httr)
library(jsonlite)
library(curl)

Authenticate

h <- new_handle()
handle_setform(h,
               "grant_type"="client_credentials",
               "scope" = "https://graph.microsoft.com/.default",
               "client_id"= "ENTER CLIENT ID HERE",
               "client_secret"="ENTER CLIENT SECRET HERE",
               "tenant_id"= "ENTER TENANT ID HERE"
)
authentication_url <- "https://login.microsoftonline.com/{ENTER-TENANT-ID-HERE}/oauth2/v2.0/token"
authentication_response <- fromJSON(rawToChar(curl_fetch_memory(authentication_url, handle = h)$content))
authentication_token <- paste(authentication_response$token_type, authentication_response$access_token)
// token_type is "Bearer" in this case and hence authentication token starts with "Bearer "

// Function to return response using above authentication token and a request link
get_response <- function(request_link, auth_token){
  request <- GET(request_link, add_headers(Authorization = auth_token))
  response <- fromJSON(content((request),"text"))
  return(response)
}


  Part 5 - Plotting Using Seaborn - Radar

  26 Aug 2019
   python, visualisation

Introduction and Data preparation

Please follow the folloing links regarding data preparation and previous posts to follow along -


Distribution of score (percentage) across participants in various categories

test_scores_Participant = test_scores.groupby(['Participant identifier','Track','Designation']).agg({'Test Name':'size', 'Score':'sum', 'maximum_score':'sum'}).reset_index()
test_scores_Participant['Percent'] =round(test_scores_Participant['Score']/test_scores_Participant['maximum_score'],2)*100

Participant_1 = test_scores_Participant[test_scores_Participant['Test Name']==1].sort_values(by=['Percent','Test Name'],
ascending=False)

Participant_2TO5 = test_scores_Participant[test_scores_Participant['Test Name'].isin([2,3,4,5])].sort_values(by=['Percent','Test Name'],ascending=False)

Participant_6TO10 =  test_scores_Participant[test_scores_Participant['Test Name'].isin([6,7,8,9,10])].sort_values(by=['Percent','Test Name'],ascending=False)

Participant_10To16 = test_scores_Participant[test_scores_Participant['Test Name'].isin([11,12,13,14,15,16])].sort_values(by=['Percent','Test Name'],ascending=False)

Participant_17 = test_scores_Participant[test_scores_Participant['Test Name']==17].sort_values(by=['Percent','Test Name'],
ascending=False)

fig, ax = plt.subplots(figsize=(15,8))
sns.distplot(Participant_1['Percent'], label="Only 1 test", hist =False, kde = True, color = "#1abc9c")
sns.distplot(Participant_2TO5['Percent'], label="2 to 5", hist = False, kde = True, color = "#3498db")
sns.distplot(Participant_6TO10['Percent'], label="6 to 7", hist = False, kde = True,  color = "#e74c3c")
sns.distplot(Participant_10To16['Percent'], label="10 to 16", hist = False, kde = True, color = "#f39c12")
ax = sns.distplot(Participant_17['Percent'], label="All 17 test", hist = False, kde = True, color = "#95a5a6")
plt.legend(fontsize = 20)
plt.ylabel(ylabel = "Probablity Density", fontsize=25)
plt.xticks(fontsize = 15)
plt.yticks(fontsize = 15)
plt.xlabel(xlabel=  "Score %", fontsize=25)
fig.suptitle('Kernel density estimate of distribution of score (percentage)', fontsize=20, x = 0.5, y = 0.95)
#plb.savefig('KDE Plot.png',dpi=100,bbox_inches='tight')