Project: s41467-024-45113-5 | Function: autowrite_SEM_full (r)

system: You are an expert R programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow R best practices and maintain consistent code style.
Return **only** the complete R function code.

user: Complete this R function:

**DESCRIPTION:** Automatically generates Structural Equation Model (SEM) syntax with direct, indirect, and total effects for trophic cascade analysis. Implements the core statistical framework for testing hypothesis 2 about direct vs indirect pathways.

**KEY TERMS:** {
  "model_raw": "SEM model specification",
  "trophic_levels": "Trophic level assignments",
  "Direct__": "Direct effect parameters",
  "Indirect__": "Indirect effect parameters",
  "Total__": "Total effect parameters"
}

**CONTEXT CODE:**   keep_criteria[, partial := More_than_random >2 & Prop.traits.25 >= 0.60 & Cor_unsignificant <= 1]
  keep_criteria[, LUI.Axis1 := round(cor.test(data$LUI, pca_stand$ind$coord[,1])$p.value, 3)]
  keep_criteria[, LUI.Axis2 := round(cor.test(data$LUI, pca_stand$ind$coord[,1])$p.value, 3)]
  names(Labels)  = traits
  
  if (is.na(annot)){
    Criteria_label = ifelse(keep_criteria$enough_support, 'Criteria met', 'Criteria not met!')
    if (keep_criteria$partial == TRUE & keep_criteria$enough_support != TRUE){
      Criteria_label = paste('Criteria partially met', sep = ' ')
    }
    
    Criteria_label = paste(Criteria_label, '\n Axis1 x LUI: p =', keep_criteria$LUI.Axis1,
                           '\n Axis2 x LUI: p =', keep_criteria$LUI.Axis2, sep = '')
  }else{
    Criteria_label =  annot
  }
  
  rownames(pca_to_plot$var$coord) = rownames(pca_to_plot$var$cor) = rownames(pca_to_plot$var$cos2) = Labels
  
  if (env_corr == TRUE){
    plot_pca_12 = fviz_pca(pca_to_plot, title = '', geom = 'point', geom.var = c("arrow", 'text'), col.quanti.sup = 'brown3', col.var = 'grey30', 
                           labelsize = 4, repel = T)
  }
  if (env_corr == FALSE){
    plot_pca_12 = fviz_pca(pca_to_plot, title = '', geom = 'point', geom.var = c("arrow", 'text'), col.quanti.sup = 'brown3', col.var = 'grey30', habillage = factor(substr(rownames(pca_to_plot$ind$coord), 1, 1)), 
                           labelsize = 5, repel = T) + 
      scale_shape_discrete( breaks = c('A', 'H', 'S'), labels = c('South', 'Central', 'North'), name = 'Region')+
      scale_color_viridis(discrete = TRUE, begin = 0.3, breaks = c('A', 'H', 'S'), labels = c('South', 'Central', 'North'), name = 'Region')+
      theme(legend.position = 'none') 
    
  }
  res = list(PCA = pca_stand, plot12 = plot_pca_12, criteria = keep_criteria, direction = direction_axis)
  return(res)
}

# Function to format thetable testing all the hypotheses
return_hypo_result_color = function(Signif, Direction){
  res = NA
  if (Signif == 'no'){
    res = 'Inconclusive' } 
  if (Signif == 'yes'){
    if (Direction %in% c('a', 'b')) {res = Direction}
    if (Direction %in% c('yes', 'no')) {res = ifelse(Direction == "yes", "@@@10004;", "X")}  }
  if (Signif == 'YES'){
    if (Direction %in% c('a', 'b')) {res = toupper(Direction)}
    if (Direction %in% c('yes', 'no')) {res = ifelse(Direction == "yes", "@@@9989;", "@@@10060;")} }
  
  return(res)}

## Function to convert a SEM into a text variable to facilitate SEM handling
autowrite_SEM_full <- function(model_raw, trophic_levels = Trophic_levels, LUI = T, Data = dd){
  stop("Function implementation has been masked")
}






Complete the function implementation replacing "stop("Not implemented")".