## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----eval=FALSE---------------------------------------------------------------
# devtools::install_github("microsoft/vivaglint")
# library(vivaglint)

## ----eval=FALSE---------------------------------------------------------------
# survey_path <- system.file("extdata", "survey_export.csv", package = "vivaglint")
# survey <- read_glint_survey(survey_path, emp_id_col = "EMP ID")

## ----eval=FALSE---------------------------------------------------------------
# glint_setup(
#   tenant_id = "your-tenant-id",
#   client_id = "your-client-id",
#   client_secret = "your-client-secret",
#   experience_name = "your-experience-name"
# )
# survey <- read_glint_survey_api(
#   survey_uuid = "your-survey-uuid",
#   cycle_id = "your-cycle-id",
#   emp_id_col = "EMP ID"
# )

## ----eval=FALSE---------------------------------------------------------------
# summary <- summarize_survey(survey, scale_points = 5)

## ----eval=FALSE---------------------------------------------------------------
# # Engagement-specific questions
# engagement_qs <- c(
#   "I would recommend my team as a great place to work",
#   "My work is meaningful"
# )
# engagement_summary <- summarize_survey(survey,
#                                        scale_points = 5,
#                                        questions = engagement_qs)

## ----eval=FALSE---------------------------------------------------------------
# distributions <- get_response_dist(survey, scale_points = 5)

## ----eval=FALSE---------------------------------------------------------------
# survey_path <- system.file("extdata", "survey_export.csv", package = "vivaglint")
# survey_q1 <- read_glint_survey(survey_path, emp_id_col = "EMP ID")
# survey_q2 <- read_glint_survey(survey_path, emp_id_col = "EMP ID")
# survey_q3 <- read_glint_survey(survey_path, emp_id_col = "EMP ID")
# 
# trends <- compare_cycles(
#   survey_q1, survey_q2, survey_q3,
#   scale_points = 5,
#   cycle_names = c("Q1 FY25", "Q2 FY25", "Q3 FY25")
# )

## ----eval=FALSE---------------------------------------------------------------
# # Direct reports only
# manager_summary <- aggregate_by_manager(survey, scale_points = 5)
# 
# # Full org tree (includes indirect reports)
# manager_full <- aggregate_by_manager(survey, scale_points = 5, full_tree = TRUE)

## ----eval=FALSE---------------------------------------------------------------
# library(dplyr)
# 
# # Managers where fewer than 50% of their team is favorable on a key item
# low_engagement_managers <- manager_summary %>%
#   filter(question == "I would recommend my team as a great place to work",
#          pct_favorable < 50) %>%
#   arrange(pct_favorable)

## ----eval=FALSE---------------------------------------------------------------
# attr_path <- system.file("extdata", "employee_attributes.csv", package = "vivaglint")
# demo_results <- analyze_by_attributes(
#   survey,
#   attribute_file = attr_path,
#   scale_points = 5,
#   attribute_cols = c("Department", "Gender", "Tenure Group"),
#   min_group_size = 10  # Suppress groups below this size for privacy
# )

## ----eval=FALSE---------------------------------------------------------------
# # Join once
# attr_path <- system.file("extdata", "employee_attributes.csv", package = "vivaglint")
# survey_enriched <- join_attributes(survey, attr_path)
# 
# # Reuse for multiple analyses — no need to re-read the file
# dept_results <- analyze_by_attributes(
#   survey_enriched,
#   scale_points = 5,
#   attribute_cols = "Department"
# )
# 
# gender_results <- analyze_by_attributes(
#   survey_enriched,
#   scale_points = 5,
#   attribute_cols = "Gender"
# )
# 
# # Filter to a subpopulation before analyzing
# na_only <- survey_enriched
# na_only$data <- filter(survey_enriched$data, Region == "North America")
# na_results <- analyze_by_attributes(na_only, scale_points = 5,
#                                     attribute_cols = "Department")

## ----eval=FALSE---------------------------------------------------------------
# # Basic attrition analysis (90, 180, and 365 days post-survey)
# attrition_path <- system.file("extdata", "attrition.csv", package = "vivaglint")
# attrition <- analyze_attrition(
#   survey,
#   attrition_file = attrition_path,
#   emp_id_col = "EMP ID",
#   term_date_col = "Termination Date",
#   scale_points = 5
# )

## ----eval=FALSE---------------------------------------------------------------
# attr_path <- system.file("extdata", "employee_attributes.csv", package = "vivaglint")
# attrition_path <- system.file("extdata", "attrition.csv", package = "vivaglint")
# survey_enriched <- join_attributes(survey, attr_path)
# 
# attrition_by_dept <- analyze_attrition(
#   survey_enriched,
#   attrition_file = attrition_path,
#   emp_id_col = "EMP ID",
#   term_date_col = "Termination Date",
#   scale_points = 5,
#   attribute_cols = c("Department", "Job Level"),
#   min_group_size = 10
# )

## ----eval=FALSE---------------------------------------------------------------
# # Pearson correlations in long format (default)
# correlations <- get_correlations(survey)
# 
# # Spearman correlations (more robust for ordinal scale data)
# correlations_spearman <- get_correlations(survey, method = "spearman")
# 
# # Correlation matrix
# cor_matrix <- get_correlations(survey, format = "matrix")

## ----eval=FALSE---------------------------------------------------------------
# # Requires the psych package
# factors <- extract_survey_factors(survey, n_factors = 3, rotation = "oblimin")
# 
# # Consolidated summary: item, factor assignment, loading, label, communality
# print(factors$factor_summary)
# 
# # Filter to items with strong factor loadings only
# strong_loaders <- dplyr::filter(factors$factor_summary, loading_label == "Strong")
# 
# # Access the raw psych object for advanced use
# factors$fa_object

## ----eval=FALSE---------------------------------------------------------------
# # Fuzzy search (default) — tolerates minor spelling differences
# flexibility_comments <- search_comments(survey, "flexibility")
# 
# # Exact, case-sensitive match
# exact_results <- search_comments(survey, "work from home", exact = TRUE)
# 
# # Broaden fuzzy tolerance to catch more spelling variation
# results <- search_comments(survey, "colaboration", max_distance = 0.3)

## ----eval=FALSE---------------------------------------------------------------
# # All responses in long format
# long_all <- pivot_long(survey, data_type = "all")
# 
# # Comments only
# long_comments <- pivot_long(survey, data_type = "comments")
# 
# # Both as separate tibbles
# both <- pivot_long(survey, data_type = "both")
# comments_df <- both$comments

## ----eval=FALSE---------------------------------------------------------------
# parts <- split_survey_data(survey)
# 
# # Numeric scores only — standard respondent columns + one score column per question
# quantitative <- parts$quantitative
# 
# # Comments only — EMP ID + all _COMMENT, _COMMENT_TOPICS, _SENSITIVE_COMMENT_FLAG columns
# qualitative <- parts$qualitative
# 
# # Pass numeric data directly to vivaglint functions
# summary <- summarize_survey(parts$quantitative, scale_points = 5,
#                             emp_id_col = "EMP ID")
# 
# # Rejoin at any time using EMP ID
# full_data <- dplyr::left_join(parts$quantitative, parts$qualitative, by = "EMP ID")

## ----eval=FALSE---------------------------------------------------------------
# # Default is 5; consider 10 or higher for sensitive analyses
# attr_path <- system.file("extdata", "employee_attributes.csv", package = "vivaglint")
# demo_results <- analyze_by_attributes(
#   survey,
#   attribute_file = attr_path,
#   scale_points = 5,
#   attribute_cols = c("Department", "Gender"),
#   min_group_size = 10
# )

