# Load packages library (conflicted)library (xfun):: pkg_attach2 (c ("extrafont" ,"knitr" ,"fMultivar" ,"IDPmisc" ,"mvtnorm" ,"Matrix" ,"psych" ,"tidyverse" ,"scales" ,"gganimate" ,"ggforce" ,"sjmisc" ,"WJSmisc" ,"tikzDevice" ,"patchwork" ,"qualvar" ,"modeest" ,"tinter" ,"ggfx" ,"ggtext" ,"lemon" ,"signs" ,"psycheval" ,"bezier" ,"DescTools" ,"ggh4x" ,"ggthemes" ,"rsvg" ,"ggarrow" ,"arrowheadr" ,"rgl" ,"ggdiagram" ,"ggbeeswarm" ,"tmvtnorm" ,"rmarkdown" ,"downlit" ,"xml2" )conflicts_prefer (dplyr:: select, :: filter, :: alpha, :: lag,:: add_case,:: ` + ` ,:: discard,:: col_factor,:: alpha,:: rescale,:: is_empty,:: replace_na,:: expand,:: pack,:: unpack,:: add_case,:: multivariate_ci,:: ` %nin% ` ,:: AUC,:: ICC,:: SD,:: geom_pointpath,:: GeomPointPath,:: signs_centered,:: distanceloadfonts ("win" , quiet =  TRUE ):: knit_hooks$ set (webgl =  hook_webgl)# Set options options (knitr.kable.digits =  2 , knitr.kable.na =  '' ):: opts_template$ set (marginfigure =  list (fig.column =  "margin" , fig.cap.location =  "top" , out.width =  "100%" , fig.align =  "left" ),bodyfigure =  list (fig.column =  "body" , fig.cap.location =  "margin" ))# Default fonts and colors =  "Equity Text A Tab" =  16 # myfills <- class_color(c("royalblue4", "firebrick4", "#51315E"))@color # myfills <- viridis::viridis(3, begin = .4, end = .65) # myfills <- pal_brewer(type = "div", 3)(4) # myfills <-  hsv(h = degree(c(150,210, 250))@turn,  #     s = .7,  #     v = c(.45,.45,.45))[c(2,1,3)] <-  class_color (c ("#27408B" , "#22734B" , "#51315E" ))@ color# myfills %>% show_col() <-  "gray20" =  ggtext_size (bsize)<-  arrow_head_deltoid (2.3 )# Default geoms and themes :: update_geom_defaults ("text" ,list (family =  bfont, size =  btxt_size,color =  txt_color)):: update_geom_defaults ("label" ,list (family =  bfont,size =  btxt_size,label.padding =  unit (0 , "lines" ),label.size =  0 ,color =  txt_color)):: update_geom_defaults ("richtext" ,list (family =  bfont, size =  btxt_size,color =  txt_color)):: update_geom_defaults ("density" , list (fill =  myfills[1 ]))theme_set (theme_minimal (base_size =  bsize, base_family =  bfont))# font family <-  function (x, style =  "font-family:serif;" paste0 ('<span style= \" ' ,' \" >' ,"</span>" )# Set table column width # https://github.com/rstudio/bookdown/issues/122#issuecomment-221101375 <-  function (kable_output, width, tag =  "</caption>" ){<-  paste0 (paste0 ('<col width="' ,'">' ),collapse =  " \n " )sub (tag,paste0 (tag," \n " ,# Make a matrix with braces <-  function (M, brace =  "bmatrix" , includenames= TRUE ) {if  (includenames) {<-  cbind (rownames (M),M)<-  rbind (colnames (M), M)<-   paste (apply (M,MARGIN =  1 ,FUN =  paste0,collapse =  " & " ),collapse =  " \\\\\n " )if  (! is.null (brace)) {<-  paste0 (" \\ begin{" ,brace,"} \n " , M, " \n\\ end{" , brace , "}" )# Function to make dice <-  function (i, id, add_blank =  TRUE ) {=  switch (` 1 `  =  0 ,` 2 `  =  c (- 1 , 1 ),` 3 `  =  c (- 1 , 1 , 0 ),` 4 `  =  c (- 1 , 1 , - 1 , 1 ),` 5 `  =  c (- 1 , 1 , - 1 , 1 , 0 ),` 6 `  =  c (- 1 , 1 , - 1 , 1 , - 1 , 1 )=  switch (` 1 `  =  0 ,` 2 `  =  c (1 ,- 1 ),` 3 `  =  c (1 ,- 1 , 0 ),` 4 `  =  c (1 ,- 1 ,- 1 , 1 ),` 5 `  =  c (1 ,- 1 ,- 1 , 1 , 0 ),` 6 `  =  c (1 ,- 1 ,- 1 , 1 , 0 , 0 ))<-  tibble (id =  id *  1 ,i =  i,x =  x,y =  y)if  (add_blank) {<-  d %>% add_case (id =  id +  0.5 ,i =  0 ,x =  NA ,y =  NA )<-  function (x) {<-  sign (x)paste0 (ifelse (signs <  0 ,"$" ,"" ), x, ifelse (signs <  0 ," \\ phantom{-}$" ,"" ))<-  function (side =  1 , at, labels =  at) {axis (side, labels =  rep ("" ,length (at)), at =  at)for  (i in  1 : length (at)) {axis (side, at =  at[i], labels =  labels[i],tick =  F)<-  function (size =  10 , text =  "." , color =  "white" ) {paste0 ("<span style='color:" ,"; font-size:" ,"pt;'>" ,"</span>" )<-  function (limits =  c (0 , 5 )) {<-  seq (limits[1 ], limits[2 ])<-  breaks[breaks !=  0 ]ggplot () + theme_classic (base_family =  bfont,base_size =  18 ,base_line_size =  .5 + theme (axis.text =  element_text (color =  "gray40" ),axis.line =  element_blank (),axis.ticks =  element_line (color =  "gray" ),axis.title.x =  element_text (angle =  0 ,vjust =  .5 ,face =  "italic" ,color =  "gray40" axis.title.y =  element_text (angle =  0 ,vjust =  .5 ,face =  "italic" ,color =  "gray40" + scale_x_continuous (name =  "y" , breaks =  breaks, labels =  WJSmisc:: signs_centered) + scale_y_continuous (name =  "x" , breaks =  breaks, labels =  signs) + :: coord_axes_inside (xlim =  limits,ylim =  limits,labels_inside =  T,ratio =  1 + ob_segment (x =  c (0 , limits[1 ] -  abs (max (limits) -  min (limits)) /  20 ), xend =  c (0 , limits[2 ] +  abs (max (limits) -  min (limits)) /  20 ), y =  c (limits[1 ] -  abs (max (limits) -  min (limits)) /  20 , 0 ), yend =  c (limits[2 ] +  abs (max (limits) -  min (limits)) /  20 , 0 ), linewidth =  .75 , arrow_head =  my_arrowhead, arrow_fins =  my_arrowhead, color =  "gray" )# Hooks ------- # # Enclose collapsible r chunk in  button # knitr::opts_hooks$set(button_r = function(options) { #   if (isTRUE(options$button_r)) { #     options$button_before_r <- TRUE #     options$button_after <- TRUE #     options$echo = TRUE #     options$eval = FALSE #   } #  #   options # }) #  # # Enclose collapsible latex chunk in  button # knitr::opts_hooks$set(button_latex = function(options) { #   if (isTRUE(options$button_latex)) { #     options$button_before_latex <- TRUE #     options$button_after <- TRUE #     options$echo = TRUE #     options$eval = FALSE #   } #  #   options # }) # before button for collapsible r chunk $ set (button_before =  function (before, options, envir) {if  (before) {if  (is.null (options$ figlabel)) {<-  options$ label %>%  str_remove ("^coder \\ -" ) %>%  str_remove ("^codelatex \\ -" ) %>%  str_remove ("^codeojs \\ -" ) if  (str_detect (l, "^fig \\ -" ) |  str_detect (l, "^tbl \\ -" )) {$ figlabel <-  l<-  options$ codelabelif  (! is.null (options$ figlabel)) {<-  paste0 (codetype, " for @" , options$ figlabel)return (paste0 (# '<div class="wrap-collapsible" style="margin-top: 1em">', # "\n", # '<input id="collapsible-', # options$label, # '" class="toggle" type="checkbox">', # "\n", # '<label for="collapsible-', # options$label, # '" class="lbl-toggle">', codetype,'</label>', # '<div class="collapsible-content">', # "\n", # '<div class="content-inner">' ':::{.callout-note collapse="true" appearance="minimal"} \n ## ' ,codetype# After button for collapsible chunks $ set (button_after =  function (before, options, envir) {# if (!before) return('</div></div></div>') if  (! before) return (' \n ::: \n ' )# Solution chunk # knitr::opts_hooks$set(solution = function(options) { #   options$echo <- TRUE #   options$solutionsetter <- TRUE #   return(options) # }) :: knit_hooks$ set (solutionsetter =  function (before,options, envir) {if  (before) {" \n\n <details><summary>Suggested Solution</summary> \n\n " else  {" \n\n </details> \n\n " # Make all chunks with demo-prefix echo = TRUE :: opts_hooks$ set (label =  function (options) {if  (startsWith (options$ label, "demo-" )) {$ echo <-  TRUE if  (startsWith (options$ label, "ex-" )) {$ echo <-  TRUE if  (startsWith (options$ label, "solution-" )) {$ echo <-  TRUE $ solutionsetter <-  TRUE if  (str_starts (options$ label, "code" )) {$ button_before <-  TRUE $ button_after <-  TRUE $ echo =  TRUE $ eval =  FALSE <-  c (r =  "R Code" , latex =  "$ \\ rm \\ LaTeX$ Code" ,ojs =  "Observable Code" )<-  str_match (options$ label, "^code(.*?) \\ -" )if  (length (mycode) ==  2 ) {$ codelabel =  codelanguages[mycode[2 ]]return (options)