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

## -----------------------------------------------------------------------------
library(dragmapr)

hhs <- example_hhs_layout()
render_dragged_map(
  hhs$states,
  region_offsets = hhs$region_offsets,
  region_col = "hhs_region",
  labels = hhs$labels,
  label_offsets = hhs$label_offsets,
  region_palette = hhs$region_colors,
  region_labels = hhs$region_names,
  title = "US Map by HHS Regions"
)

## -----------------------------------------------------------------------------
panels <- example_panel_layout()
render_dragged_map(
  panels$panels,
  region_offsets = panels$region_offsets,
  region_col = "group",
  labels = panels$labels,
  label_offsets = panels$label_offsets,
  region_palette = panels$region_colors,
  region_labels = panels$region_names,
  title = "Draggable non-map panels"
)

## -----------------------------------------------------------------------------
tmp_regions <- tempfile(fileext = ".csv")
tmp_labels <- tempfile(fileext = ".csv")
utils::write.csv(hhs$region_offsets, tmp_regions, row.names = FALSE)
utils::write.csv(hhs$label_offsets, tmp_labels, row.names = FALSE)

render_dragged_map(
  hhs$states,
  region_offsets = tmp_regions,
  region_col = "hhs_region",
  labels = hhs$labels,
  label_offsets = tmp_labels,
  region_palette = hhs$region_colors,
  region_labels = hhs$region_names,
  title = "CSV round trip"
)

## -----------------------------------------------------------------------------
note <- as_drag_annotations(data.frame(
  label_id = "hhs-9-note",
  region = "9",
  label = "A longer callout can be dragged separately from the region.",
  x = hhs$labels$x[hhs$labels$region == "9"],
  y = hhs$labels$y[hhs$labels$region == "9"]
), width_px = 210, height_px = 88, connector = TRUE, connector_type = "squiggle")

render_dragged_map(
  hhs$states,
  region_offsets = hhs$region_offsets,
  region_col = "hhs_region",
  labels = note,
  label_offsets = data.frame(
    label_id = "hhs-9-note",
    region = "9",
    dx_m = 120000,
    dy_m = 80000
  ),
  region_palette = hhs$region_colors,
  region_labels = hhs$region_names,
  connector_linewidth = 1,
  connector_linetype = "dashed",
  connector_endpoint = "arrow",
  legend_title = "HHS region",
  map_background = "light_grid",
  label_padding = 0.12,
  title = "Annotation box and connector"
)

## -----------------------------------------------------------------------------
render_dragged_map(
  hhs$states,
  region_offsets = hhs$region_offsets,
  region_col = "hhs_region",
  labels = hhs$labels,
  label_offsets = hhs$label_offsets,
  region_palette = hhs$region_colors,
  region_labels = hhs$region_names,
  label_values = c("1", "2", "3"),
  legend_values = c("1", "2", "3"),
  title = "Selected labels and legend keys"
)

## -----------------------------------------------------------------------------
render_dragged_map(
  hhs$states,
  region_offsets = hhs$region_offsets,
  region_col = "hhs_region",
  labels = hhs$labels,
  label_offsets = hhs$label_offsets,
  region_palette = hhs$region_colors,
  region_labels = hhs$region_names,
  show_origin_outlines = TRUE,
  show_movement_connectors = TRUE,
  movement_connector_linetype = "dashed",
  movement_connector_endpoint = "closed",
  title = "Movement context"
)

## ----eval = FALSE-------------------------------------------------------------
# if (interactive()) {
#   shiny::runApp(system.file("examples", "shiny_draggable_plot.R", package = "dragmapr"))
#   shiny::runApp(system.file("examples", "shiny_custom_labels.R", package = "dragmapr"))
#   shiny::runApp(system.file("examples", "shiny_draggable_export.R", package = "dragmapr"))
#   shiny::runApp(system.file("examples", "shiny_spatial_studio.R", package = "dragmapr"))
#   shiny::runApp(system.file("examples", "shiny_static_export.R", package = "dragmapr"))
# }

