library(magrittr)
library(knitr)

code_tree <- read.csv(file.path("Output","codetree.csv"), stringsAsFactors = FALSE)


root <- code_tree[1,1]

PATH <- gsub("\\\\ROOT$", "", root,perl=TRUE) 

code_tree %<>% within({
  parent %<>% gsub("\\\\","/",., fixed=TRUE) %>% gsub(PATH,"",  . ,fixed=TRUE)
  child %<>% gsub("\\\\","/",., fixed=TRUE) %>% gsub(PATH,"",  . ,fixed=TRUE)
})

graphcode <- function( edges, node, rootnode=TRUE){
  #edges is a data.frame with two columns: parent, child
  if(length(node)){
    children <-  subset(edges, parent==node)$child
    if(!rootnode){ output <- " child{ "}
    if(rootnode){ output <- "" }
    output <- c(output,  "node{ ", node,"} ")
    #if(rootnode){ output <- c(output, "[grow=right] ")}
    for( child in children){ 
      output <- c(output, Recall(edges, child, rootnode=FALSE))
    }
    if(!rootnode){ output <- c( output, " }")}
  } else{ 
    output <- ""
  }
  output
}



graph <- graphcode(code_tree, code_tree[1,1])
graph <- gsub("\\\\","/", graph)
graph <- gsub(PATH,"", graph)
graph <- gsub("_"," ", graph)
graph <- paste(c('\\',graph, ";"), collapse = "")
graph

#sibling distance=20em,

\begin{tikzpicture}[edge from parent fork right, level distance=20em, grow'=right, every node/.style = {shape=rectangle, rounded corners,draw, align=center, fill=white}] r knitr::knit_child(text=graph) \end{tikzpicture}

Real Usage

This is generated a stand alone vignette, but really it woudl be called from a call in main.R like

render("Progs/code_tree_doc.Rmd", 
       output_file ="Vignette Code Tree.pdf",output_dir = "Output/Reports",
       params=list(my_author="Simon Bond", 
                   my_title= "Vignette Code Tree")

)

NB use the file /doc/Progs/code_tree_docs.Rmd and not the similarly named one within /doc . Building vignettes requires these two slightly different versions.



shug0131/cctu documentation built on Nov. 10, 2023, 12:03 p.m.