# devtools::load_all();devtools::document();pkgdown::build_site(lazy=F)

options(dplyr.summarise.inform = FALSE)
library(CausalMapFunctions)
library(knitr)
library(htmltools)
library(configr)
library(dbplyr)
library(DBI)
library(kableExtra)
library(scales)
library(tictoc)
knitr::opts_chunk$set(echo = T,warning=F,message=F)



pp=function(x,N=99){
  print(x,n=N)
}

replace_null <- function(x,replacement=0){
  if(is.null(x)) replacement else x
}


append_rows <- function(val,name){
  val <- val %>% mutate_all(clean_send)
  dbWriteTable(con=conn,name = local(name),value=local(val) ,append=TRUE,row.names=FALSE)
}
print_inner_filter <- function(xx){
  # browser()
xx$graf <- NULL
fil <- xx[[1]] %>% unlist %>% str_remove_all("^pipe_") %>% str_replace("_"," ")
xx <- xx[-1]
nams=names(xx)
tibble(names=nams,vals=unlist(xx)) %>% 
  unite(filter,sep="=") %>% 
  pull(filter) %>% 
  unlist %>% 
  paste0(.,collapse=" ") %>% 
  paste0(fil," ",.)
}
print_filter <- function(graf){

  if(!exists("xtitle"))xtitle <- ""
  if(!exists("xdescription"))xdescription <- ""

  if(is.null(attr(graf,"info")))return()
  # browser()
story <- attr(graf,"info")[[1]] %>% keep(~str_detect(.,"^load_mapfile")) %>% unlist %>% str_remove("load_mapfile *path=")
input_ace <- attr(graf,"info") %>% list_modify("load_mapfile" = NULL) %>% unlist %>% keep(.!=".") %>% map(~str_remove_all(.,"^pipe_") %>% str_replace("_"," ")) %>%  paste0(collapse = "\n")
filters <- paste0("&filters=",URLencode(input_ace %>% replace_null(""),reserved=T))

message("appending")
append_rows(
  tibble(likes=1,recommended=T,filter=input_ace %>% replace_null(""),hidden=F,title=xtitle %>%    replace_null(""),description=xdescription %>% replace_null(""),date=time_stamp(),project=story)
  ,"shortlinks")


attr(graf,"info") %>% 
  map(~print_inner_filter(.) %>% 
      str_trim %>% 
      div(style=";")
  ) %>%  tagList %>% div(style="padding:10px;background-color:aliceblue;") %>% div(
    if(xtitle!="")h5(xtitle),
    if(xdescription!="")p(xdescription),                                                                     
    .,
    a("Link",href=paste0("https://causalmap.shinyapps.io/CausalMap2?story=",story,filters)),style="padding:10px;background-color:aliceblue;")


}

# make_interactive_map <- function(graf){
#   tagList(print_filter(graf),CausalMapFunctions::make_interactive_map(graf))
# }
# make_print_map <- function(graf){
#   tagList(print_filter(graf),CausalMapFunctions::make_print_map(graf))
# }
clean_send <- function(vec){
  if(vec %>% unique %>% `%in%`(c(F,T)) %>% all) vec <- as.numeric(vec)
  if(F & is.character(vec)) {
    # Encoding(vec)="UTF-8"
    vec <- stri_enc_toascii(vec)
  }
  vec
}
recodes <- tibble(old="Damage to Property",new="changed")
recodes <- tibble(old="Damage to Property",new="Damage to Businesses")
pipe_recode_factors(example2,recodes=recodes) %>% make_interactive_map()
example2$settings <- list(recodes=recodes)
pipe_recode_factors(example2) %>% make_interactive_map()



oi <- load_mapfile("C:/Users/steve/Downloads/2oirefugeetestimonies-2023-01-16.xlsx")

sh %>% 
  pipe_find_links(field = "source_frequency",value = 7,operator="greater") %>% 
  pipe_find_factors(field = "label",value = "no change",operator="notcontains") %>% 
  pipe_bundle_links() %>% 
  make_interactive_map()

sh %>% 
  pipe_find_links(field = "source_frequency",value = 7,operator="greater") %>% 
  pipe_find_factors(field = "label",value = "no change",operator="notcontains") %>% 
  pipe_bundle_links() %>% 
  make_print_map()






message(1)
pipe_update_mapfile()  # creates an empty map
pipe_update_mapfile() %>% 
  make_interactive_map()
load_mapfile() %>% # creates an empty map
  make_interactive_map()

example2 
example2 %>% pipe_coerce_mapfile %>% pipe_update_mapfile() %>% make_interactive_map()
example2 %>% pipe_coerce_mapfile %>% pipe_update_mapfile(factors=example2$factors) %>% make_interactive_map()



ee <- example2 

path <- "C:/Users/steve/Downloads/kantar-swan-short-2022-09-06.xlsx"

example_file <- load_mapfile("example-file")
test <- load_mapfile("resignations2")
test %>% 
  make_interactive_map()


swan <- load_mapfile(path=path)
# example_file <- load_mapfile(path="data/example-file.xlsx")
example_file <- load_mapfile(path="C:/Users/steve/Dropbox/Projects/CausalMapFunctions/data/example-file.xlsx")

tic();swan %>% pipe_coerce_mapfile() %>% make_interactive_map();toc()
tic();swan %>% pipe_coerce_mapfile();toc()

example2 %>% pipe_coerce_mapfile() %>% make_interactive_map()

  ee %>%
    pipe_label_links("link_id",fun = "unique") %>%
    make_interactive_map

  ee %>%
    pipe_label_links("link_id",fun = "unique") %>%
    make_print_map()

  ee %>%
    pipe_set_print(grv_layout="circo") %>%
    make_print_map()



  example_file %>% 
    make_interactive_map()

  tic()
  example_file %>% 
    pipe_select_factors(10) %>% 
    pipe_bundle_links() %>% 
    pipe_scale_links() %>% 
    make_print_map()
  toc()


  example_file %>% 
    pipe_find_factors(value="Nutrition") %>% 
    pipe_bundle_links() %>% 
    pipe_scale_links() %>% 
    make_print_map()

  example_file %>% 
    pipe_select_factors() %>% 
    pipe_bundle_links() %>% 
    pipe_scale_links() %>% 
    pipe_color_factors() %>% 
    make_print_map()


tf <- load_mapfile("tearfund-all")  

tf %>% 
  parse_commands("zoom factors level=1
find links field=source_frequency value=7 operator=greater
trace paths length=3 remove_links=FALSE threads_direction=down from=CCM to=Good family/ personal relationships/ support
find links field=hashtags value=#plain_coding operator=notcontains
bundle links field=simple_bundle
label factors field=thread_count add_field_name=FALSE clear_previous=FALSE
label links value=count: downstream_threads add_field_name=FALSE clear_previous=TRUE
scale links value=count: downstream_threads
color factors field=found_type
color links field=threads fun=count") %>% 
  make_print_map()


tic()
tf %>% pipe_zoom_factors() # 6 times as long
toc()

tic()
example_file %>% pipe_zoom_factors()
toc()

tic();example_file %>% pipe_trace_paths(from="method/practice",to="Income",length = 3,remove_links = F);toc()

# increases from .3 to 3 seconds
tic();example_file %>% pipe_trace_paths(from="method/practice",to="Income",length = 3,threads_direction = "down",remove_links = F);toc()

# length doesnt make a difference
tic();example_file %>% pipe_trace_paths(from="method/practice",to="Income",length = 6,remove_links = F);toc()

# increases from .3 to 3 seconds
tic();example_file %>% pipe_trace_paths(from="method/practice",to="Income",length = 6,threads_direction = "down",remove_links = F);toc()

tic();tf %>% pipe_trace_paths(from="Christian faith",to="Increased material",length = 3,remove_links = F);toc()


tic();tf %>% pipe_trace_paths(from="Christian faith",to="Increased material",length = 3,remove_links = F) %>% make_interactive_map();toc()

tic();tf %>% pipe_zoom_factors() %>% pipe_trace_paths(from="Christian faith",to="Increased material",length = 3,remove_links = F) %>% pipe_cluster_factors("e") %>% pipe_remove_brackets() %>% pipe_color_factors()%>% pipe_color_links();toc()





# oh my gosh, this is the one
tic();tf %>% pipe_trace_paths(from="Christian faith",to="Increased material",length = 3,remove_links = F,threads_direction = "down");toc()





tf$links %>% nrow
example_file$links %>% nrow

tf
example_file %>%
    pipe_find_links("simple_frequency",value = 15,"greater") %>%
    make_interactive_map
example_file %>%
    pipe_find_links("simple_frequency",value = 1500,"greater") %>%
    make_interactive_map

example_file %>%
    pipe_find_links("simple_frequency",value = 1500,"greater") %>%
    make_print_map
example_file %>%
    pipe_select_links(5) %>%
    make_interactive_map
example_file %>%
    pipe_find_statements(field="statement_id",value=5,operator="equals") %>%
    make_interactive_map
example_file %>%
    pipe_find_factors(value="health") %>%
    make_interactive_map

Zero up and down

# shows nothing because isolated are removed
ee %>%
    pipe_find_factors(value="rainfall",up=0,down=0,remove_isolated = T) %>%
    make_interactive_map

ee %>%
    pipe_find_factors(value="rainfall",up=0,down=0,remove_isolated = F) %>%
    make_interactive_map

# shows nothing because isolated are removed
ee %>%
    pipe_find_factors(value="Damage",up=0,down=0,remove_isolated = T) %>%
    make_interactive_map

ee %>%
    pipe_find_factors(value="Damage",up=1,down=1,remove_isolated = F) %>%
    make_interactive_map

ee %>%
    pipe_find_factors(value="Damage",up=1,down=1,remove_isolated = F) %>%
    make_interactive_map

Selecting and finding

If you filter the factors of a mapfile, e.g. show only factors with labels beginning xyz,

If you filter the links of a mapfile, e.g. show only links with hashtags containing xyz,

If you filter the statements of a mapfile, e.g. show only statements with texts containing xyz,

graf <- ee

tic();graf <- pipe_update_mapfile(graf,factors=graf$factors);graf;toc()

ee %>%
    pipe_select_factors(3) %>%
    make_interactive_map

Simple frequency

ee %>%
    pipe_find_factors("frequency",value = 1,"greater") %>%
    make_interactive_map
ee %>%
    pipe_find_factors("frequency",value = 1,"greater") %>%
    make_print_map
ee %>%
    pipe_find_factors("frequency",value = 1,"greater",up=0,down=0) %>%
    make_interactive_map

Showing interlinks or not

If you don't want to show interlinks between downstream factors, use find links instead.

example_file %>%
  pipe_find_factors(value="health",up=0,down=1,remove_isolated = T) %>%
    pipe_bundle_links() %>% 
    make_print_map

example_file %>%
  pipe_find_links(value="health",field="from_label",operator="contains") %>%
    pipe_bundle_links() %>% 
    make_print_map

Case insensitive

ee %>%
    pipe_find_factors(value="business") %>%
    make_interactive_map

notcontains

ee %>%
    pipe_find_factors(value="sea",operator="notcontains") %>%
    make_interactive_map
ee %>% pipe_find_statements(value="1",operator="notcontains",field="statement_id")%>%
    make_interactive_map
ee %>% pipe_find_statements(value="1",operator="notcontains",field="statement_id")%>%
    make_interactive_map

notequals

ee %>% pipe_find_statements(value="1",operator="notequals",field="statement_id")%>%
    make_interactive_map
ee %>%
    pipe_find_factors(value="Coastal erosion",operator="notequals") %>%
    make_interactive_map

OR

Does not work in functions, but works when parsed:

ee %>%
    pipe_find_factors(value="business OR property") %>%
    make_interactive_map
ee %>%
    parse_commands("find factors field=label operator=contains value=Business OR Property") %>%
    make_interactive_map

Order matters

example_file %>%
    pipe_find_factors(value="health") %>%
    pipe_select_factors(5) %>%
    make_interactive_map

No result

example_file %>%
    pipe_find_factors(value="asdfasdfasdf") %>%
    make_interactive_map
example_file %>%
    pipe_find_links(field="from_label",value="health",operator="contains") %>%
    make_interactive_map

Numerical comparison

example_file %>%
    pipe_find_statements(field="statement_id",value=20,operator="less") %>%
    make_interactive_map

No result

example_file %>%
    pipe_find_statements(field="statement_id",value=20000000,operator="greater") %>%
    make_interactive_map

Highlight only

ee  %>% 
  pipe_find_factors(value="Damage",highlight_only=T) %>% pipe_color_factors(field="found") %>% 
  make_interactive_map()
ee  %>% 
  pipe_find_links(field="to_label",value="Damage",highlight_only=T) %>%     
  pipe_color_links(field="found",fun="literal") %>% 
  make_interactive_map()

Clustering sources

pipe_cluster_sources(example_file) %>% 
  .$sources %>% 
  select(contains("cluster"))

example_file %>%
  parse_commands("cluster sources n_clusters=all") %>% 
  .$sources %>% 
  select(contains("cluster"))

example_file %>%
  parse_commands("cluster sources n_clusters=2") %>% 
  .$sources %>% 
  select(contains("cluster"))


example_file %>%
  parse_commands("cluster sources") %>% 
  .$sources %>% 
  select(contains("cluster"))

Calculated fields

example_file %>% 
  pipe_find_factors(value="health",up = 1,down=1) %>% 
  make_mentions_tabl()
example_file %>% 
  pipe_find_factors(value="health",up = 1,down=1) %>% 
.$factors %>% select(label,source_count)

Conditional formatting

example_file %>%
    pipe_select_factors(5) %>%
    pipe_scale_factors(field="frequency") %>%
    pipe_bundle_links %>% 
  make_interactive_map

example_file %>%
    pipe_select_factors(5) %>%
    pipe_color_text(field="frequency") %>%
    pipe_bundle_links %>% 
  make_interactive_map

example_file %>%
    pipe_select_factors(5) %>%
    pipe_scale_factors(field="frequency") %>%
    make_print_map
example_file %>%
    pipe_select_factors(5) %>%
    pipe_color_factors(field="frequency") %>%
    pipe_color_borders(field="betweenness") %>%
    pipe_wrap_factors(5) %>%
    pipe_bundle_links %>% 
  make_interactive_map

Wrap factors

example_file %>%
    pipe_select_factors(5) %>%
    pipe_color_links(value="count: link_id") %>%
    pipe_wrap_factors(5) %>%
    pipe_bundle_links %>% 
  make_interactive_map

example_file %>%
    pipe_select_factors(5) %>%
    pipe_color_links(value="count: link_id") %>%
    pipe_wrap_factors(5) %>%
    pipe_bundle_links %>% 
    make_print_map

Wrap links

ee %>%
    pipe_label_links("from_label",fun = "unique") %>%
    pipe_wrap_links(6) %>%
    pipe_bundle_links %>% 
  make_interactive_map

ee %>%
    pipe_label_links("from_label",fun = "unique") %>%
    pipe_wrap_links(6) %>%
    make_print_map
ee %>%
    pipe_label_links(value="unique: from_label") %>%
    pipe_wrap_links(6) %>%
    make_interactive_map

Remove brackets

example_file$factors$label[32] <- "Income [E]"

example_file %>%
    pipe_select_factors(5) %>%
    make_interactive_map

example_file %>%
    pipe_select_factors(5) %>%
    pipe_remove_brackets() %>%
    make_interactive_map

example_file$factors$label[32] <- "Income"

Bundle factors

example_file %>%
    pipe_bundle_factors(value = "health") %>%
    pipe_select_factors(5) %>%
  pipe_bundle_links() %>% 
    make_interactive_map

Self loops

example_file %>%
    pipe_bundle_factors(value = "health") %>%
    pipe_select_factors(5) %>%
    pipe_bundle_links() %>% 
    pipe_remove_selfloops() %>% 
    make_interactive_map

Format links without bundling

ee %>%
  pipe_color_links("link_id",fun = "mean") %>%
  make_interactive_map
ee %>%
  pipe_scale_links("link_id",fun = "mean") %>%
  make_interactive_map

Label factors with and without field names

example_file %>%
  pipe_find_factors(value="Improved health") %>% 
    # pipe_zoom_factors(1) %>%
  pipe_select_factors(top=10) %>% 
  pipe_bundle_links(field="simple_bundle") %>% 
  pipe_label_links(field="source_id",fun = "count") %>% 
  pipe_label_factors(field="source_count",add_field_name = F) %>% 
  pipe_wrap_factors() %>% 
    make_print_map 



example_file %>%
  pipe_find_factors(value="Improved health") %>% 
    # pipe_zoom_factors(1) %>%
  pipe_select_factors(top=10) %>% 
  pipe_bundle_links(field="simple_bundle") %>% 
  pipe_label_links(field="source_id",fun = "count") %>% 
  pipe_label_factors(field="source_count",add_field_name = T) %>% 
  pipe_wrap_factors() %>% 
    make_print_map 

Label links

example_file %>%
  pipe_select_links(10) %>% 
  pipe_label_links("link_id",fun = "literal") %>%
  pipe_label_links("from_label",fun = "literal",add_field_name = T,clear_previous = F) %>%
  make_interactive_map

example_file %>%
  pipe_select_links(10) %>% 
  pipe_bundle_links() %>% 
  pipe_label_links("link_id",fun = "literal",add_field_name = T) %>%
  make_interactive_map

example_file %>%
  pipe_select_links(10) %>% 
  pipe_bundle_links() %>% 
  pipe_label_links("link_id",fun = "literal") %>%
  pipe_label_links("from_label",fun = "literal",clear_previous = F) %>%
  make_interactive_map

example_file %>%
  pipe_select_links(10) %>% 
  pipe_bundle_links() %>% 
  parse_commands("label links field=statement_id fun=unique clear_previous=F") %>%
  make_interactive_map

Bundling links

Note the defaults for bundle_links and label_links:

oo %>%
    pipe_select_factors(5) %>%
    pipe_bundle_links(("#Name of province")   ) %>%
    pipe_label_links(("#Name of province")   ) %>%
    make_interactive_map

oo %>%
    parse_commands("
    select links top=15
select factors top=10
bundle links field=#2
label factors field=frequency add_field_name=FALSE clear_previous=FALSE
scale links value=count: link_id
label links field=#2Name of village fun=unique add_field_name=FALSE clear_previous=TRUE
color text field=is_opposable lo=black hi=#f26d04") %>%
    make_interactive_map
oo %>%
    pipe_select_factors(5) %>%
    pipe_bundle_links("village"   ) %>%
    pipe_label_links("village" ,fun = "unique"  ) %>%
    make_interactive_map
ll %>%
    pipe_find_factors(value="economic") %>%
    pipe_select_factors(5) %>%
    pipe_bundle_links() %>%
    pipe_label_links() %>%
    make_interactive_map
ll %>%
    pipe_find_factors(value="economic") %>%
    pipe_select_factors(5) %>%
    pipe_bundle_links() %>%
    pipe_label_links() %>%
    make_interactive_map

Note the default for bundle_links is equivalent to simple_bundle:

ll %>%
    pipe_find_factors(value="economic") %>%
    pipe_select_factors(5) %>%
    pipe_bundle_links(group="simple_bundle") %>%
    pipe_label_links() %>%
    make_interactive_map

Group and label by sex and scale by count:

ll %>%
    pipe_select_factors(5) %>%
    pipe_bundle_links(field="1. Sex") %>%
    pipe_scale_links("link_id",fun = "count") %>%
    pipe_label_links("1. Sex",fun = "unique") %>%
    pipe_color_links("1. Sex",fun = "unique") %>%
    make_interactive_map
ll %>%
    pipe_select_links(16) %>%
    pipe_bundle_links(field="1. Sex") %>%
    pipe_label_links("link_id",fun = "count") %>%
    make_interactive_map

Group by sex and scale and colour by count:

example_file %>%
    pipe_select_factors(5) %>%
    pipe_bundle_links(field="Sex of the head of the household") %>%
    pipe_color_links("link_id",fun = "count") %>%
    pipe_scale_links("link_id",fun = "count") %>%
    pipe_label_links("link_id",fun = "count") %>%
    make_interactive_map

Proportion

ll %>%    
  pipe_find_factors(value="sed yield") %>%
  pipe_bundle_links(field="District") %>% 
  pipe_color_links(field="District",fun="unique") %>% 
  pipe_label_links(value="percent: link_id") %>%  
  pipe_scale_links(field="link_id",fun="count") %>%  
  make_print_map
ll %>%    
  pipe_find_factors(value="sed yield") %>%
  pipe_bundle_links(field="1. Sex") %>% 
  pipe_color_links(field="1. Sex",fun="unique") %>% 
  pipe_label_links(field="link_id",fun="percent") %>%  
  pipe_scale_links(field="link_id",fun="count") %>%  
  make_print_map
ll %>%    
  pipe_find_factors(value="sed yield") %>%
  pipe_bundle_links(field="1. Sex") %>% 
  pipe_color_links(field="1. Sex",fun="unique") %>% 
  pipe_label_links(field="source_id",fun="percent") %>%  
  pipe_scale_links(field="source_id",fun="percent") %>%  
  make_print_map
ll %>%    
  pipe_find_factors(value="sed yield",down=0) %>%
  pipe_bundle_links(field="1. Sex") %>% 
  pipe_color_links(field="source_id",fun="percent") %>% 
  pipe_label_links(field="source_id",fun="percent") %>%  
  pipe_scale_links(field="source_id",fun="count") %>%  
  make_print_map

Surprise

ll %>%    
  pipe_find_factors(value="sed yield",down=0) %>%
  pipe_bundle_links(field="1. Sex") %>% 
  pipe_color_links(field="1. Sex",fun="unique") %>% 
  pipe_label_links(field="source_id",fun="surprise") %>%  
  pipe_scale_links(field="source_id",fun="count") %>%  
  make_print_map
ll %>%    
  pipe_find_factors(value="sed yield",down=0) %>%
  pipe_bundle_links(field="1. Sex") %>% 
  pipe_label_links(field="1. Sex",fun="unique") %>% 
  pipe_color_links(field="source_id",fun="surprise") %>%  
  pipe_scale_links(field="source_id",fun="count") %>%  
  make_print_map

Using initials to shorten the labels

oo %>%    
  pipe_find_factors(value="crops",down=0) %>%
  pipe_bundle_links(field="#4Sex of the respondent") %>% 
  pipe_label_links(field="#4Sex of the respondent",fun="initials") %>% 
  pipe_color_links(field="source_id",fun="surprise") %>%  
  pipe_scale_links(field="source_id",fun="count") %>%  
  make_print_map
oh %>%    
  pipe_select_factors(10) %>%
  pipe_bundle_links(field="simple_bundle") %>% 
  pipe_label_links(field="from_label",fun="initials") %>% 
  make_print_map

Recalculating

Shouldn't make any difference as factor and link fields are now recalculated after every transform; but beforeids and afterids are added only on load_mapfile.

ll %>% 
  pipe_find_factors(value="WASH") %>% 
  pipe_color_factors(field="betweenness") %>% 
  make_interactive_map

ll %>% 
  pipe_find_factors(value="WASH") %>% 
  pipe_recalculate_factors() %>% 
  pipe_color_factors(field="betweenness") %>% 
  make_interactive_map

Mark links

so %>% 
  pipe_label_links(field = "source_id",fun="literal") %>% 
  pipe_mark_links(field="source_id") %>% 
  make_print_map

e3 %>% 
  pipe_label_links(field = "source_id",fun="literal") %>% 
  pipe_mark_links(field="source_id") %>% 
  make_print_map

ee %>% 
  pipe_label_links(field = "source_id",fun="literal") %>% 
  pipe_mark_links(field="source_id") %>% 
  make_print_map


so %>% 
  pipe_bundle_links(field = "source_id") %>% 
  pipe_label_links(field = "source_id",fun="literal") %>% 
  pipe_mark_links(field="source_id") %>% 
  make_print_map

so %>% 
  pipe_combine_opposites() %>% 
  pipe_bundle_links(field = "flipped_bundle") %>% 
  pipe_label_links(field = "source_id",fun="literal") %>% 
  pipe_mark_links(field="source_id") %>% 
  make_print_map

ll %>% 
  pipe_select_factors(5) %>% 
  pipe_bundle_links(field = "simple_bundle") %>% 
  pipe_mark_links(field="source_id") %>% 
  make_print_map

Show continuity

#original:

so %>% 
  pipe_label_links(field = "source_id",fun="unique") %>% 
  make_print_map

so %>% 
  pipe_label_links(field = "source_id",fun="unique") %>% 
  pipe_show_continuity(field="source_id",type="label") %>% 
  make_print_map
so %>% 
  pipe_label_links(field = "source_id",fun="unique") %>% 
  pipe_show_continuity(field="source_id") %>% 
  make_print_map


ee %>% 
  pipe_label_links(field = "source_id",fun="unique") %>% 
  pipe_show_continuity(field="source_id") %>% 
  make_print_map

example_file %>% 
  pipe_label_links(field = "source_id",fun="unique") %>% 
  pipe_show_continuity(field="source_id") %>% 
  make_print_map

example2 %>% 
  pipe_zoom_factors() %>% 
  pipe_select_factors() %>% 
  pipe_show_continuity(field="source_id") %>% 
  pipe_label_links(field = "source_id",fun="unique") %>% 
  make_print_map

example2 %>% 
  pipe_bundle_factors("Flooding") %>% 
  pipe_show_continuity(field="source_id") %>% 
  pipe_label_links(field = "source_id",fun="unique") %>% 
  make_print_map

example2 %>% 
  pipe_bundle_factors("Flooding") %>% 
  pipe_bundle_links(field = "source_id") %>% 
  pipe_label_links(field = "source_id",fun="literal") %>% 
  pipe_show_continuity(field="source_id") %>% 
  make_print_map



so %>% 
  pipe_bundle_links() %>% 
  pipe_label_links(field = "source_id",fun="unique") %>% 
  pipe_show_continuity(field="source_id") %>% 
  make_print_map



so %>% 
  pipe_combine_opposites() %>% 
  pipe_label_links("link_id","literal") %>% 
  pipe_show_continuity(field="source_id") %>% 
  make_print_map

so %>% 
  pipe_combine_opposites() %>% 
  pipe_show_continuity() %>% 
  make_print_map

so %>% 
  pipe_combine_opposites() %>% 
  pipe_bundle_links(field = "flipped_bundle") %>% 
  pipe_show_continuity(field="source_id") %>% 
  pipe_label_links(field = "source_id",fun="unique") %>% 
  make_print_map




# Order should not matter, but you can't put bundle links later


so %>% 
  pipe_combine_opposites() %>% 
  pipe_show_continuity(field="source_id") %>% 
  pipe_bundle_links(field = "flipped_bundle") %>% 
  pipe_label_links(field = "source_id",fun="unique") %>% 
  make_print_map
ll %>% 
  pipe_select_factors(5) %>% 
  pipe_bundle_links(field = "simple_bundle") %>% 
  pipe_scale_links(field = "link_id",fun="count") %>% 
  pipe_show_continuity(field="source_id") %>% 
  make_print_map


hz %>%
  pipe_show_continuity(field="source_id") %>% 
    make_print_map 

hz %>%
  pipe_zoom_factors(1) %>%
  pipe_show_continuity(field="source_id") %>% 
    make_print_map 

Group and label by sex and scale by count:

ll %>%
    pipe_select_factors(5) %>%
    pipe_bundle_links(field="1. Sex") %>%
    pipe_scale_links("link_id",fun = "count") %>%
    pipe_label_links("1. Sex",fun = "unique") %>%
    pipe_color_links("1. Sex",fun = "unique") %>%
    pipe_show_continuity(field="source_id") %>% 
    make_print_map

ll %>%
    pipe_select_factors(5) %>%
    pipe_find_links(field = "statement_id",value=90,operator="greater") %>%
    pipe_find_links(field = "statement_id",value=290,operator="less") %>%
    pipe_bundle_links(field="1. Sex") %>%
    pipe_scale_links("link_id",fun = "count") %>%
    pipe_label_links("source_id",fun = "unique") %>%
    pipe_color_links("1. Sex",fun = "unique") %>%
    pipe_show_continuity(field="source_id") %>% 
    make_print_map

Nested maps

tf %>%
    pipe_zoom_factors(1) %>%
    pipe_select_factors(5) %>%
    make_interactive_map
tf %>%
    pipe_zoom_factors(1) %>%
    pipe_bundle_links() %>%
    pipe_label_links() %>%
    pipe_scale_links() %>%
    make_print_map

Zooming out and showing source count / frequency

example_file %>%
    pipe_zoom_factors(level = 1) %>%
    pipe_select_factors(10) %>%
  pipe_bundle_links(field="simple_bundle") %>% 
  pipe_label_links(field="source_id",fun = "count") %>% 
  pipe_label_factors(field="frequency",add_field_name = F) %>% 
  pipe_wrap_factors() %>% 
    make_print_map

example_file %>%
    pipe_zoom_factors(level = 2) %>%
    pipe_select_factors(10) %>%
  pipe_bundle_links(field="simple_bundle") %>% 
  pipe_label_links(field="source_id",fun = "count") %>% 
  pipe_label_factors(field="frequency",add_field_name = F) %>% 
  pipe_wrap_factors() %>% 
    make_print_map 

example_file %>%
  pipe_zoom_factors(1,preserve_frequency = 4,frequency_field = "frequency",frequency_other = "(other)") %>%
    pipe_select_factors(10) %>%
  pipe_bundle_links(field="simple_bundle") %>% 
  pipe_label_factors(field="frequency",add_field_name = F) %>% 
  pipe_wrap_factors() %>% 
    make_print_map 


oig %>%
    pipe_zoom_factors(1,preserve_frequency = 20,frequency_field = "source_count") %>%
  pipe_bundle_links(field="simple_bundle") %>% 
  pipe_label_links(field="source_id",fun = "count") %>% 
  pipe_label_factors(field="source_count",add_field_name = F) %>% 
  pipe_color_borders(field="frequency_preserved",lo="white") %>% 
  pipe_wrap_factors() %>% 
    make_print_map 
tt %>%
    pipe_zoom_factors(1,preserve_frequency = 80) %>%
  pipe_bundle_links(field="simple_bundle") %>% 
  pipe_label_links(field="link_id",fun = "count") %>% 
  pipe_label_factors(field="frequency",add_field_name = F) %>% 
  pipe_color_borders(field="frequency_preserved",lo="white") %>% 
  pipe_wrap_factors() %>% 
    make_print_map 

cc %>%
  pipe_find_factors(value="Improved health") %>% 
    pipe_zoom_factors(1) %>%
  pipe_select_factors(top=10) %>% 
  pipe_bundle_links(field="simple_bundle") %>% 
  pipe_label_links(field="source_id",fun = "count") %>% 
  pipe_label_factors(field="source_count",add_field_name = F) %>% 
  pipe_label_factors(field="frequency",add_field_name = F) %>% 
  pipe_wrap_factors() %>% 
    make_print_map 

cc %>%
  pipe_find_factors(value="Improved health") %>% 
  pipe_zoom_factors(1) %>%
  pipe_select_factors(top=10) %>% 
  pipe_bundle_links(field="simple_bundle") %>% 
  pipe_label_links(field="source_id",fun = "count") %>% 
  pipe_label_factors(field="source_count",add_field_name = F) %>% 
  pipe_label_factors(field="frequency",add_field_name = F) %>% 
  pipe_wrap_factors() %>% 
    make_print_map 

oi %>% 
  pipe_find_factors(value="Take a loan") %>%
  pipe_zoom_factors(1,preserve_frequency = 2,frequency_other = "(other)") %>%
  # pipe_label_factors("source_count") %>% 
  # pipe_label_links(value="unique:source_id") %>% 
  make_print_map

Combining opposites

example_file %>% 
  pipe_combine_opposites() %>% 
  pipe_zoom_factors() %>% 
  pipe_select_factors() %>% 
  make_print_map()

example_file %>% 
  pipe_combine_opposites() %>% 
  pipe_zoom_factors() %>% 
  pipe_select_factors() %>% 
  pipe_color_borders(fixed="white") %>% 
  make_print_map()

example_file %>% 
  pipe_combine_opposites() %>% 
  pipe_zoom_factors() %>% 
  pipe_select_factors() %>% 
  pipe_color_borders(field = "frequency") %>% 
  make_print_map()

hh %>% 
  make_print_map()
hh %>% 
  pipe_combine_opposites %>% 
  pipe_label_links(value="count:link_id") %>% 
  make_print_map()

# artificial arrowlabels
hh %>% 
    pipe_combine_opposites %>% pipe_update_mapfile(.,links=.$links %>% mutate(color="limegreen",taillabel=c("","","\u274C","\u274C","","\u274C"),headlabel=c("","","\u274C","\u274C","\u274C","\u274C")))  %>% make_print_map()

hh %>% 
    pipe_combine_opposites %>% pipe_update_mapfile(.,links=.$links %>% mutate(color="limegreen",taillabel=c("+","+","~","~","+","~"),headlabel=c("+","+","~","~","~","~")))  %>% make_print_map()

hh %>% 
    pipe_combine_opposites %>% pipe_update_mapfile(.,links=.$links %>% mutate(color="limegreen",arrowtail=c("","","nonetee","nonetee","","nonetee"),arrowhead=c("nonetee","","veenonenonetee","veenonenonetee","veenonenonetee","veenonenonetee")))  %>% make_print_map()

Nested maps with opposites

Note colours in Interactive view

tt %>%
    pipe_zoom_factors(1) %>%
    pipe_combine_opposites() %>%
    pipe_select_links(3) %>%
    make_print_map


tt %>%
    pipe_zoom_factors(1) %>%
    pipe_select_links(15) %>%
    pipe_combine_opposites() %>%
  pipe_bundle_links("flipped_bundle") %>% 
  pipe_label_links(value="count:link_id") %>% 
    make_print_map


tt %>%
    pipe_zoom_factors(1) %>%
    pipe_combine_opposites() %>%
    pipe_select_links(3) %>%
    make_interactive_map

Factor quick fields

# factors <- de$factors

de %>% 
  pipe_color_text(field="time") %>% 
  make_print_map

Hiding quickfields

de %>% 
  pipe_hide_quickfields %>% 
  make_print_map


de %>% 
  pipe_color_borders(field="happiness") %>% 
  pipe_label_factors(field = "time") %>% 
  pipe_hide_quickfields %>% 
  make_print_map

equivalent:

de %>% 
  pipe_find_factors(field="time",value="after",up=0,down=0) %>% 
  make_print_map

de %>% 
  pipe_find_factors(field="label",value="time:after",up=0,down=0) %>% 
  make_print_map

zoom

de %>% 
  pipe_zoom_factors(1) %>% 
  pipe_color_factors(field="time") %>% 
  make_print_map

Link quick fields

# factors <- de$factors

de %>% 
  pipe_recalculate_all() %>% 
  pipe_color_links(field="plausibility") %>% 
  make_print_map


de %>% 
  pipe_color_text(field="happiness") %>% 
  pipe_hide_quickfields %>% 
  make_print_map


de %>% 
  pipe_label_factors(field = "time") %>% 
  pipe_hide_quickfields %>% 
  make_print_map

Path tracing

cat("### Single\n")  



example_file %>% 
  pipe_trace_paths(from = "Organisation 1",to="Ability to buy food",length = 3) %>% 
  pipe_select_factors(15) %>% 
  make_interactive_map

ma %>%    
  pipe_trace_paths(from = "Shared roles",to="balanced",length = 1) %>% 
  pipe_select_factors(15) %>% 
  make_interactive_map
ma %>%    
  pipe_trace_paths(from = "Covid-19",to="Business",length = 2) %>% 
  pipe_select_factors(15) %>% 
  make_interactive_map

oo %>%    
  pipe_select_factors(15) %>% 
  pipe_trace_paths(from = "Learnt about farming",to="",length = 5) %>% 
  make_interactive_map

ee %>%    
  pipe_trace_paths(from = "Funds",to="area",length = 5) %>% 
  make_interactive_map

cat("### Case insensitive\n")  

ee %>%    
  pipe_trace_paths(from = "funds",to="aREa",length = 5) %>% 
  make_interactive_map

cat("### Failing; no paths at all\n")  

ee %>%    
  pipe_trace_paths(from = "xx",to="yy",length = 5) %>% 
  make_interactive_map

cat("### Failing; no paths\n")  
ee %>%    
  pipe_trace_paths(from = "Funds",to="yy",length = 5) %>% 
  make_interactive_map

ee %>%    
  pipe_trace_paths(from = "xx",to="Property",length = 5) %>% 
  make_interactive_map

cat("### Implicit multiple\n")  

ee %>%    
  pipe_trace_paths(from = "High",to="Damage",length = 5) %>% 
  make_interactive_map

cat("### Explicit multiple\n")  

ee %>%    
  pipe_trace_paths(from = "High",to="Property | Business",length = 5) %>% 
  make_interactive_map

ee %>%    
  pipe_trace_paths(from = "High",to="Property OR Business",length = 5) %>% 
  make_interactive_map

cat("Should this be possible?")

ee %>%    
  pipe_trace_paths(from = "High",to="Property OR Business",length = 5) %>% 
  make_interactive_map

tt %>%
  pipe_trace_paths(from = "Capabilities",to="[OP3]",length = 2) %>% 
  make_interactive_map

ee %>%    
  pipe_trace_paths(from = "Funds",to="area",length = 5) %>% 
  make_print_map()

Threads

example_file %>% 
  pipe_zoom_factors(1) %>% 
  pipe_select_factors(10) %>% 
  pipe_trace_paths(length=4,from="knowledge",threads_direction="down") %>% 
  pipe_bundle_links() %>% 
  pipe_scale_links() %>% 
  pipe_color_links(field="threads") %>% 
  pipe_label_links(field="threads") %>% 
  pipe_color_factors(field="thread_count") %>% 
  pipe_label_factors(field="thread_count") %>% 
  make_print_map()

example_file %>% 
  pipe_zoom_factors(1) %>% 
  pipe_select_factors(6) %>% 
  pipe_trace_paths(length=4,from="knowledge",threads_direction="down") %>% 
  pipe_bundle_links() %>% 
  pipe_scale_links() %>% 
  # pipe_color_links(field="threads") %>% 
  pipe_label_links(field="threads",fun = "unique") %>% 
  pipe_wrap_links(12) %>% 
  pipe_color_factors(field="thread_count") %>% 
  pipe_label_factors(field="thread_count") %>% 
  make_print_map()

Robustness

## request is large but ok
pa %>%    
  pipe_trace_paths(from = "",to="~Impact on wellbeing",length = 1) %>% 
  make_print_map()
pa %>%    
  pipe_trace_paths(from = "Covid",to="",length = 3) %>% 
  make_print_map()

## request is large

if(F){e3 %>%    
  pipe_trace_robustness(from = "High",to="Damage",length = 5) %>% 
  make_print_map()

ee %>%    
  pipe_trace_robustness(from = "High",to="Damage",length = 5) %>% 
  get_robustness()

tt %>%
  pipe_trace_robustness(from = "Capabilities",to="[OP3]",length = 2) %>% 
  pipe_wrap_factors() %>% 
  make_print_map()

tt %>%
  pipe_trace_robustness(from = "Capabilities",to="[OP3",length = 2) %>% 
  pipe_wrap_factors() %>% 
  make_print_map()

## checking the robustness gets transferred
tt %>%
  pipe_trace_robustness(from = "Capabilities",to="[OP3]",length = 2) %>% 
  pipe_wrap_factors() %>%
  pipe_wrap_links() %>%
  pipe_select_links(10) %>%
  get_robustness()

tt %>%
  pipe_trace_robustness(from = "Capabilities",to="[OP3",length = 2) %>% 
  get_robustness()

tt %>%
  pipe_trace_robustness(from = "Capabilities; [P13",to="[OP3]",length = 2) %>% 
  get_robustness()

e3  %>%
  pipe_trace_robustness(from = "High",to="People moving",length = 5) %>% 
  make_print_map()

e3  %>%
  pipe_trace_robustness(from = "High",to="",length = 5) %>% 
  make_print_map()

e3  %>%
  pipe_trace_robustness(from = "High",to="People moving",length = 5) %>% 
  get_robustness() %>% 
  kable
e3  %>%
  pipe_trace_robustness(from = "High",to="Flooding",length = 5) %>% 
  get_robustness() %>% 
  kable
e3  %>%
  pipe_trace_robustness(from = "High",to="Damage",length = 5) %>% 
  get_robustness() %>% 
  kable
e3  %>%
  pipe_trace_robustness(from = "External",to="Damage",length = 5) %>% 
  get_robustness() %>% 
  kable
e3  %>%
  pipe_trace_robustness(from = "External",to="Outcome",length = 5) %>% 
  get_robustness()%>% 
  kable

}

Robustness by field

Just one source:

ee  %>%
  pipe_trace_robustness(from = "Funds",to="Increased",length = 5,field = "source_id") %>% get_robustness

ll  %>%
  pipe_trace_robustness(from = "Cash",to="Income",length = 5,field="source_id") %>% 
  get_robustness()

Check that opposites colouring is always preserved?

if(F){
hh  %>%
  pipe_trace_robustness(from = "Revision",to="happy",length = 5) %>% 
  pipe_combine_opposites %>% 
  make_print_map()

hh  %>%
  pipe_combine_opposites %>% 
  pipe_find_factors(value="exam") %>% 
  make_print_map()

hh  %>%
  pipe_combine_opposites %>% 
  pipe_zoom_factors() %>% 
  pipe_find_factors(value="exam") %>% 
  pipe_select_factors(2) %>% 
  pipe_select_links(3) %>% 
  make_print_map()
}

Colours in interactive map

hh  %>%
  pipe_combine_opposites %>% 
  pipe_zoom_factors() %>% 
  make_interactive_map()

upstream

e3  %>%
  pipe_trace_paths(from = "",to="Outcome",length = 4) %>% 
  pipe_trace_threads(direction="up") %>% 
  pipe_bundle_links() %>% 
  pipe_label_links(field = "source_id",fun = "literal") %>% 
  pipe_label_links(field = "has_upstream_threads",fun = "sum",clear_previous = F) %>% 
  pipe_color_links(field = "has_upstream_threads",fun = "sum") %>% 
  pipe_color_borders(field="n_upstream_threads_surviving") %>% 
  pipe_label_factors(field="n_upstream_threads_surviving") %>% 
  make_print_map() 

oi %>%    
  pipe_trace_paths(from = "Flee",to="",length = 3) %>% 
  pipe_trace_threads(direction="down") %>% 
  pipe_bundle_links() %>% 
  pipe_label_links(field = "has_downstream_threads",fun = "sum") %>% 
  pipe_label_links(field = "source_id",fun = "literal",clear_previous = F) %>% 
  pipe_color_links(field = "has_downstream_threads",fun = "sum") %>% 
  pipe_color_factors(field="found_type") %>% 
  make_print_map

oi %>%    
  pipe_trace_paths(from = "",to="Financial diary",length = 2) %>% 
  pipe_trace_threads(direction="up") %>% 
  pipe_bundle_links() %>% 
  pipe_label_links(field = "has_upstream_threads",fun = "sum") %>% 
  pipe_label_links(field = "source_id",fun = "literal",clear_previous = F) %>% 
  pipe_color_links(field = "has_upstream_threads",fun = "sum") %>% 
  pipe_color_factors(field="found_type") %>% 
  make_print_map
if(F){
graf <- tt1
graf$factors %>% filter(factor_id %notin% graf$links$from & factor_id %notin% graf$links$to)  %>% nrow

graf$links %>% filter(from %notin% graf$factors$factor_id & to %notin% graf$factors$factor_id)  %>% nrow

graf$links %>% filter(statement_id %notin% graf$statements$statement_id) %>% nrow
  }

Cluster factors

ee %>% 
  pipe_coerce_mapfile %>% 
  pipe_cluster_factors("Damage OR Flood") %>% 
  make_print_map

Pipe-able:

ee %>% 
  pipe_cluster_factors("Damage OR Flood") %>% 
  pipe_cluster_factors("Rising") %>% 
  make_print_map

Large map

cm <- 
  load_mapfile("cmi-gender")
library("tictoc")
tic()
make_interactive_map(cm)
toc()

Data manipulation, file management etc

Accessing the data

One column in one table

ll %>%
    pipe_find_factors(value="economic") %>%
    .$factors %>%
    .$label %>% 
  knitr::kable()

Merging two maps

if(T)merge_mapfile(ee,tt %>% pipe_select_factors(top=8)) %>% 
  pipe_color_factors(field="map_id") %>% 
  pipe_color_links(field="map_id",fun="unique") %>% 
  make_interactive_map

Note warning if factor labels are shared

if(T)load_mapfile("example2") %>%
  pipe_coerce_mapfile %>% 
  pipe_merge_mapfile("example2") %>% 
  pipe_color_factors(field="map_id") %>% 
  pipe_color_links(field="map_id",fun="unique") %>% 
  make_interactive_map

Editing maps directly

There is no guarantee that the resulting map is still a standard mapfile.

ee$factors$label[1] <- "Label changed"
ee %>% make_interactive_map

Editing maps with pipe_update_mapfile

There is no guarantee that the resulting map is still a standard mapfile.

ee %>% 
  pipe_update_mapfile(factors = ee$factors %>% mutate(label="one")) %>% 
  make_interactive_map

Printing the filters

# example2 %>% 
#   pipe_coerce_mapfile %>% 
#   pipe_find_links(field = "link_id",value=1)  %>% 
#   pipe_select_factors(20) %>%  
#   pipe_remove_isolated() %>% 
#   pipe_select_links(20) %>%  
#   pipe_zoom_factors() %>% 
#   pipe_find_statements(field = "text",value="Flo") %>% 
#   pipe_find_factors(value="Flo") %>%
#   pipe_remove_brackets("[") %>% 
#   pipe_trace_robustness(from = "Flood",to="Damage") %>% 
#   pipe_trace_paths(from = "Flood",to="Damage") %>% 
#   pipe_combine_opposites() %>% 
#   pipe_bundle_links() %>% 
#   pipe_label_links() %>% 
#   attr("info") %>% 
#   str
# 
# example2 %>%
#   pipe_coerce_mapfile() %>%
#   pipe_bundle_links() %>%
#   pipe_scale_links() %>%
#   pipe_label_links() %>%
#   pipe_color_links() %>%
#   pipe_scale_factors(field = "frequency") %>%
#   pipe_label_factors() %>%
#   pipe_color_factors() %>%
#   pipe_color_borders() %>%
#   pipe_mark_links() %>%
#   pipe_show_continuity() %>%
#   pipe_cluster_factors() %>%
#   pipe_wrap_factors() %>%
#   pipe_wrap_links() %>%
#   print_filter()

You can also load up an Excel file:

# system.file("extdata", "quip-lorem", package = "CausalMapFunctions") %>% 
#   get_mapfile_from_excel()


stevepowell99/CausalMapFunctions documentation built on Oct. 12, 2023, 11:13 a.m.