---
title: "Report"
author: "Scary Scarecrow"
date: '`r format(Sys.Date())`'
output:
  html_document:
    includes:
      before_body: "logo-option.html"
    theme: flatly
    # toc: true
    # toc_float: true
    code_download: true
    code_folding: "show"
    css: style.css
    self_contained: true

---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
	echo = FALSE,
	message = FALSE,
	warning = FALSE
)

library(limer)
library(highcharter)
library(dplyr)
library(tidyr)
options(lime_api = 'https://survey.tools.lanubia.com/index.php/admin/remotecontrol')
options(lime_username = '') #enter id
options(lime_password = '') #enter password

get_session_key()  # Log in
survey_df<-call_limer(method='list_surveys')
responses <- get_responses(iSurveyID=89257,  sResponseType='short')

release_session_key()

responses[responses=="AG1"] <- "1"
responses[responses=="AG2"] <- "2"
responses[responses=="AG3"] <- "3"
responses[responses=="AG4"] <- "4"
responses[responses=="AG5"] <- "5"


responses[] <- lapply(responses, as.character)

responses<-
  responses |> 
  select(-datasciencepro) |> 
  pivot_longer(cols = c(17:146)) |> 
  separate(name, c("group","sub_group","name"))

df<-
  responses |> 
  mutate(pillar= case_when(
    group %in% c("datasource","datatype","dataquality","datastoragequality","safety","privacy") ~ "data",
    group %in% c("involvement","open","competency","innovation","selfreliance","teamreliance","culture","knowledge","social") ~ "people",
    group %in% c("strategy","operations","test", "qualityassurance") ~ "process",
    group %in% c("infra","tools") ~ "technology",
    group %in% c("datausage","perception") ~ "perception"
  ) ) |> 
  select(c(id, submitdate, seed, gender, socio, education, education.other., department, department.other., management, directemployees, timespentwithdata, pillar, group, sub_group, name, value)) |> 
  mutate(value=as.numeric(value))

overall<-df |> 
  mutate(value=as.numeric(value)) |> 
  group_by(pillar) |> 
  summarise(value=round(mean(value, na.rm=T))) |> 
  filter(pillar != "perception") |> 
  arrange(value)

perception<-df |> 
  filter(group == "perception") |> 
  mutate(value=as.numeric(value)) |> 
  mutate(sub_group=factor(sub_group, levels = c("beginner","Collector","Descriptor","Predictor","Expert"), labels = c("beginner","Collector","Descriptor","Predictor","Expert"))) |> 
  mutate(score=case_when(
    sub_group=="beginner" ~ 1,
    sub_group=="Collector" ~ 2,
    sub_group=="Descriptor" ~ 3,
    sub_group=="Predictor" ~ 4,
    sub_group=="Expert" ~ 5
  )) |> 
  mutate(score=value*score) |> pull(value) |> mean() |> round() #Scoring unclear. Temporarily using mean

overall.levels <- overall |> 
  mutate(value=factor(value, levels = c("Oblivious","Explorer","Enlightened","Vanguard","Empowered"),
                      labels = c("Oblivious","Explorer","Enlightened","Vanguard","Empowered"))) |> 
  pull(value) |> levels()
```


## Overall picture


```{r}


  # hchart(overall,"column", hcaes(x=pillar, y=value)) |> 
  #   hc_yAxis(categories=overall.levels, title="") |> 
  #   hc_xAxis(title="") |> 
  #   hc_title(text="Overall Picture") |> 
  #   hc_credits(
  #   enabled = TRUE,
  #   text = "LaNubia Data Science",
  #   href = "https://www.lanubia.com/") |> 
  #   hc_exporting(
  #   enabled = TRUE, # always enabled
  #   filename = "Overall"
  # )

```





```{r}

pdf<-data.frame(pillar=c("process","technology","people","data"), value=perception)

Lvl1dfStatus <- tibble(name = overall$pillar, y = overall$value, drilldown = tolower(name))

Level_2_Drilldowns <- lapply(unique(overall$pillar), function(x_level) {
      overall.group <- df[df$pillar == x_level,]
      overall.group <- overall.group |>
        group_by(group) |>
        summarize(value = round(mean(value, na.rm=T))
        ) |> arrange(value) 
      
      Lvl2dfStatus <- tibble(name = overall.group$group, y = overall.group$value, drilldown = tolower(paste(x_level, name, sep = "_")))
      list(id = tolower(x_level), type = "column", data = list_parse(Lvl2dfStatus), name="Score by Group")
    })


Level_3_Drilldowns <- lapply(unique(overall$pillar), function(x_level) {
      overall.group <- df[df$pillar == x_level,]
      lapply(unique(overall.group$group), function(y_level) {
        overall.sub.group <- df[df$group == y_level,]
        overall.sub.group <- overall.sub.group |>
          group_by(sub_group) |>
          summarize(value = round(mean(value, na.rm=T))
        ) |> arrange(value) 
        
        Lvl3dfStatus <- tibble(name = overall.sub.group$sub_group,y = overall.sub.group$value)
        list(id = tolower(paste(x_level, y_level, sep = "_")), type = "column", data = list_parse2(Lvl3dfStatus), name="Score by Sub Group")
      })
    }) |> unlist(recursive = FALSE)

highchart() |>
      hc_xAxis(type = "category") |>
      hc_add_series(Lvl1dfStatus, "column", hcaes(x = name, y = y),name = "Overall Score", showInLegend = F) |>
      hc_add_series(pdf,"line",hcaes(x=pillar, y=value),enableMouseTracking=F, name="Perceived Mean Score") |> 
      hc_plotOptions(column = list(stacking = "normal"), lang=list(drillUpText ="Back")) |>
      hc_drilldown(
        allowPointDrilldown = TRUE,
        series = c(Level_2_Drilldowns,Level_3_Drilldowns)
      ) |> 
  hc_yAxis(categories=overall.levels, title="") |> 
    hc_xAxis(title="") |> 
  hc_title(text="'Data Driven' Status") |> 
  hc_subtitle(text="Click on bar for deep dive") |> 
  hc_credits(
    enabled = TRUE,
    text = "LaNubia Data Science",
    href = "https://www.lanubia.com/"
  ) |> 
  hc_tooltip() |> 
        hc_exporting(
    enabled = TRUE, # always enabled
    filename = "Error_Contact"
  )

```



## By Pillars

The highest performing pillars is/are `r overall[overall$value==max(overall$value),]$pillar` and lowest is/are `r overall[overall$value==min(overall$value),]$pillar`.

Compared to the perceived mean maturity score (`r perception`), scores in the following are lower.

```{r}
overall |> 
  filter(round(value)<round(perception))
```


Following match the perception


```{r}
overall |> 
  filter(round(value)==round(perception))
```


and Following are higher than perception

```{r}
overall |> 
  filter(round(value)<round(perception))
```



### People

```{r}

```



### Data



### Process



### Technology





