report_format <- 'html (page)'
## if we are knitting outside of shiny, we cannot rely on reactives.
## Thus, we run the functions manually
## At the end, we will output the cleaned dataframe for visualization

if (params$rendered_by_shiny == FALSE){
  
  source(here::here("global.R"))
  
  ## artificial input since we're not in shiny context
  input <- list()
  
  source(here::here("helper_scripts/server_functions.R")) 

  input$data_to_use <- "Use preloaded data"
  preloaded_data_options <-  preloaded_data_options ## defined in global file
  input$preloaded_data_choice <- "Sample tracing data"
  cols_to_exclude_from_filters <- c("placeholder_non_column")
  input$select_date_of_review <- as.Date("2021-04-13")
  
      
  report_format <- "html (page)"
  todays_date <- input$select_date_of_review

  url <- "https://godata-r13.who.int/"
  username <- "godata_api@who.int"
  password <- "godata_api@who"
  outbreak_id <- "3b5554d7-2c19-41d0-b9af-475ad25a382b"

  # ~~~~ read_file_raw ----
  
   tracing_data_raw <-
    read_file_raw()
  
  # ~~~~ read_file_transformed ----
  
  contacts_df_long_transformed <- 
    read_file_transformed(tracing_data_raw = tracing_data_raw)
  
  
# ~~~~ read_file_filtered ----
  
  contacts_df_long <- 
    read_file_filtered(contacts_df_long_transformed,
                       input$select_date_of_review)

  PARAMS$documentation <-  TRUE

}
## if knitting within shiny
if (params$rendered_by_shiny == TRUE){
  
# ~~~~ Source functions  ----

  source(here::here("global.R"))
  source(here::here("helper_scripts/server_functions.R")) 


  ## parameters
  contacts_df_long <- params$contacts_df_long
  todays_date <- params$todays_date
  report_format <- params$report_format
}


update_rmd_progress()

VALUE BOX SUMMARIES

new_contacts_per_day_value_box(contacts_df_long,
                           todays_date,
                           report_format)

cumulative_contacts_value_box(contacts_df_long,
                              todays_date,
                              report_format)

update_rmd_progress()
contacts_under_surveillance_value_box(contacts_df_long,
                                      todays_date,
                                      report_format)

pct_contacts_followed_value_box(contacts_df_long,
                                todays_date,
                                report_format)


update_rmd_progress()
new_contacts_today_row_title(todays_date, report_format)

new_contacts_today_text(contacts_df_long,
                        todays_date,
                        report_format)

update_rmd_progress()

New contacts bar chart

new_contacts_today_bar_chart(contacts_df_long,
                        todays_date,
                        report_format)
update_rmd_progress()

New contacts sunburst plot

new_contacts_today_sunburst_plot(contacts_df_long,
                        todays_date,
                        report_format)

update_rmd_progress()

New contacts table

new_contacts_today_table(contacts_df_long,
                        todays_date,
                        report_format)

update_rmd_progress()
new_contacts_historical_row_title(report_format)

new_contacts_historical_text(contacts_df_long,
                                 todays_date,
                                 report_format)
update_rmd_progress()

Absolute numbers

new_contacts_historical_bar_chart(contacts_df_long,
                                      todays_date,
                                      report_format)
update_rmd_progress()

Relative proportions

new_contacts_historical_bar_chart_relative(contacts_df_long,
                                               todays_date,
                                               report_format)
update_rmd_progress()
cumul_contacts_today_row_title(todays_date, report_format)

cumul_contacts_today_text(contacts_df_long,
                        report_format)

update_rmd_progress()

Cumulative contacts bar chart

cumul_contacts_today_bar_chart(contacts_df_long,
                        report_format)
update_rmd_progress()

Cumulative contacts sunburst plot

cumul_contacts_today_sunburst_plot(contacts_df_long,
                        report_format)

update_rmd_progress()

Cumulative contacts table

cumul_contacts_today_table(contacts_df_long,
                        report_format)

update_rmd_progress()
cumul_contacts_historical_row_title(report_format)

cumul_contacts_historical_text(contacts_df_long,
                                 report_format)
update_rmd_progress()

Absolute numbers

cumul_contacts_historical_bar_chart(contacts_df_long,
                                      todays_date,
                                      report_format)
update_rmd_progress()

Relative proportions

cumul_contacts_historical_bar_chart_relative(contacts_df_long,
                                               todays_date,
                                               report_format)
update_rmd_progress()
active_contacts_today_row_title(todays_date, report_format)

active_contacts_today_text(contacts_df_long,
                        todays_date,
                        report_format)

update_rmd_progress()

Contacts under surveillance bar chart

active_contacts_today_bar_chart(contacts_df_long,
                        todays_date,
                        report_format)
update_rmd_progress()

Contacts under surveillance sunburst plot

active_contacts_today_sunburst_plot(contacts_df_long,
                        todays_date,
                        report_format)

update_rmd_progress()

Contacts under surveillance table

active_contacts_today_table(contacts_df_long,
                        todays_date,
                        report_format)

update_rmd_progress()
active_contacts_historical_row_title(report_format)

active_contacts_historical_text(contacts_df_long,
                                 todays_date,
                                 report_format)
update_rmd_progress()

Absolute numbers

active_contacts_historical_bar_chart(contacts_df_long,
                                      todays_date,
                                      report_format)
update_rmd_progress()

Relative proportions

new_contacts_historical_bar_chart_relative(contacts_df_long,
                                               todays_date,
                                               report_format)
update_rmd_progress()

Contacts per case

total_contacts_per_case_text(contacts_df_long,
                             report_format)

update_rmd_progress()

Contacts per case, table

total_contacts_per_case_table(contacts_df_long,
                             report_format)

update_rmd_progress()

Contacts per case, donut plot

total_contacts_per_case_donut_plot(contacts_df_long,
                             report_format)

update_rmd_progress()

Contacts per case, bar chart

total_contacts_per_case_bar_chart(contacts_df_long,
                             report_format)

update_rmd_progress()

Case-contact relationships

total_contacts_per_link_type_text(contacts_df_long,
                                        report_format)

update_rmd_progress()

Case-contact relationships, donut plot

total_contacts_per_link_type_donut_plot(contacts_df_long,
                             report_format)

update_rmd_progress()

Case-contact relationships, bar chart

total_contacts_per_link_type_bar_chart(contacts_df_long,
                             report_format)

update_rmd_progress()

Follow-up timelines

active_contacts_timeline_text(contacts_df_long,
                             todays_date,
                             report_format)

Follow-up summary bar chart

active_contacts_breakdown_bar_chart(contacts_df_long,
                           todays_date,
                           report_format,
                           legend_df)

update_rmd_progress()

Follow-up timeline snake plot

active_contacts_timeline_snake_plot(contacts_df_long,
                           todays_date,
                           report_format,
                           legend_df)

update_rmd_progress()

Contacts not seen recently

lost_contacts_linelist_text(contacts_df_long,
                             todays_date,
                             report_format)
update_rmd_progress()

Loss to follow-up, past 3 days

contacts_lost_24_to_72_hours_table(contacts_df_long,
                             todays_date,
                             report_format)

List of contacts not seen

lost_contacts_linelist_table(contacts_df_long,
                             todays_date,
                             report_format = report_format) %>%
  .$output_table