## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>",
  eval     = FALSE
)
# library(submitr)

## -----------------------------------------------------------------------------
# library(readr)
# library(dplyr)
# 
# input <- read_csv("data.csv")
# 
# summary <- input |>
#   group_by(group) |>
#   summarise(
#     mean_value = mean(value, na.rm = TRUE),
#     n          = dplyr::n(),
#     .groups    = "drop"
#   )
# 
# if (!dir.exists("results")) dir.create("results")
# 
# write_csv(summary, "results/summary.csv")

## -----------------------------------------------------------------------------
# library(submitr)
# 
# cfg <- htc_config()

## -----------------------------------------------------------------------------
# cfg <- htc_config()
# #> Reading HTC config from ./htc.cfg
# #> ✔ Connected to "ap2002.chtc.wisc.edu" as "your.netid".

## -----------------------------------------------------------------------------
# htc_gen_submit(
#   output_file     = "analysis.sub",
#   container_image = "docker://registry.doit.wisc.edu/your.netid/my-analysis:1.0.0",
#   executable      = "analysis.sh",
#   input_files     = c("analysis.R", "data.csv"),
#   output_files    = "results.tar.gz",
#   resources       = "small",
#   comments        = TRUE,
#   output          = "."
# )

## -----------------------------------------------------------------------------
# htc_gen_executable(
#   r_script       = "analysis.R",
#   output_file    = "analysis.sh",
#   results_folder = "results",
#   comments       = TRUE
# )

## -----------------------------------------------------------------------------
# htc_upload(
#   files   = c("analysis.sub", "analysis.sh", "analysis.R", "data.csv"),
#   config  = cfg,
#   dry_run = TRUE
# )
# #> ✔ Dry run -- command that would be executed:
# #>   `scp analysis.sub analysis.sh analysis.R data.csv your.netid@ap2002.chtc.wisc.edu:~/`

## -----------------------------------------------------------------------------
# htc_upload(
#   files  = c("analysis.sub", "analysis.sh", "analysis.R", "data.csv"),
#   config = cfg
# )

## -----------------------------------------------------------------------------
# cluster_id <- htc_submit(
#   submit_file = "analysis.sub",
#   config      = cfg,
#   verbose     = TRUE
# )
# #> Submitting "analysis.sub" on "ap2002.chtc.wisc.edu"...
# #> Submitting job(s)...
# #> 1 job(s) submitted to cluster 6302860.
# #> ✔ Job submitted from "~/analysis.sub" on "ap2002.chtc.wisc.edu".

## -----------------------------------------------------------------------------
# # One-shot status check
# htc_status(cluster_id = cluster_id, config = cfg)
# 
# # Watch until the job completes
# htc_status(cluster_id = cluster_id, config = cfg, watch = TRUE)

## -----------------------------------------------------------------------------
# # Results
# htc_download(
#   files      = "*.tar.gz",
#   config     = cfg,
#   local_path = "results/"
# )
# 
# # Logs
# htc_download(
#   files      = c("job.log", "job.err"),
#   config     = cfg,
#   local_path = "logs/"
# )

## -----------------------------------------------------------------------------
# htc_gen_submit(
#   output_file     = "analysis.sub",
#   container_image = "docker://registry.doit.wisc.edu/your.netid/my-analysis:1.0.0",
#   executable      = "analysis.sh",
#   input_files     = "analysis.R",
#   mode            = "multiple",
#   queue_from      = "data/manifest.csv",
#   resources       = "medium",
#   comments        = TRUE
# )
# 
# htc_gen_executable(
#   r_script       = "analysis.R",
#   output_file    = "analysis.sh",
#   results_folder = "results",
#   mode           = "multiple",
#   comments       = TRUE
# )

## -----------------------------------------------------------------------------
# args       <- commandArgs(trailingOnly = TRUE)
# input_file <- args[[1]]
# 
# data <- readr::read_csv(input_file)

## -----------------------------------------------------------------------------
# containr::generate_dockerfile(r_version = "4.4.0", output = ".")
# containr::build_image(verbose = TRUE)
# imgs <- containr::list_images()
# containr::push_image(
#   image_id = imgs$image_id[1],
#   netid    = "your.netid",
#   project  = "my-analysis",
#   tag      = "1.0.0"
# )

