# Quant Camp Day 5: a good, scientific workflow --------------------------------
#
# Today is mostly demos, and most of the work happens in Quarto files (.qmd),
# not in this script. Use this file to try out the code that will go INSIDE
# your Quarto document. Save it in your camp project as code/05-code.R
#
# Today's moves:
#   prose + code in one file     -> Quarto (.qmd), chunks, inline code, Render
#   citations                    -> Zotero, .bib files, @key
#   managing agents              -> CLAUDE.md, work in the notebook, adversarial review
#   the pipeline                 -> raw data -> cleaning -> clean data -> analysis
#
# Files to download today (links are on the slides):
#   05_report.qmd and camp.bib -> top level of your project (next to the .Rproj)
#   05_check_me.R              -> code/
#   pipeline_example.zip       -> anywhere OUTSIDE your camp project; it is its own project


# Packages ---------------------------------------------------------------------

library(tidyverse)   # wrangling + plotting
library(juanr)       # coups, elections
library(here)        # file paths that work on any computer


# 1. Code for a Quarto chunk ---------------------------------------------------

# one row per coup attempt
coups

# a table: how many attempts succeeded (realized = 1) and failed (realized = 0)?
# knitr::kable() turns a tibble into a table that looks good in a document
coups |>
  count(realized) |>
  knitr::kable()


# 2. Numbers for inline code ---------------------------------------------------

# in a .qmd, these go inside a sentence, between backticks that start with r,
# so the number is never typed by hand

# how many coup attempts are in the data?
nrow(coups)

# what percent succeeded? (the mean of a 0/1 dummy is a share)
round(mean(coups$realized) * 100, 1)


# 3. Your turn: a report that updates itself -----------------------------------

# open 05_report.qmd (top level of your project) and click Render


# 4. Your turn: find the bugs --------------------------------------------------

# open code/05_check_me.R and work through it there


# 5. The pipeline --------------------------------------------------------------

# open pipeline_example.Rproj, then render code/clean_data.qmd and
# code/analysis.qmd, in that order
