AI-powered infographic generation using Nano Banana Pro.

for (pkg in c("httr2", "jsonlite", "base64enc", "optparse")) { if (!requireNamespace(pkg, quietly = TRUE)) { cat(sprintf("E…

MichalKazimierczak updated 23d ago
Claude CodeGeneric
View source ↗
#!/usr/bin/env Rscript
# AI-powered infographic generation using Nano Banana Pro.
#
# Smart iterative refinement approach:
# 1. (Optional) Research phase - gather facts and data using Perplexity Sonar
# 2. Generate initial infographic with Nano Banana Pro
# 3. AI quality review using Gemini 3 Pro for infographic critique
# 4. Only regenerate if quality is below threshold for document type
# 5. Repeat until quality meets standards (max iterations)
#
# Requirements:
#   OPENROUTER_API_KEY environment variable
#   R packages: httr2, jsonlite, base64enc, optparse
#
# Usage:
#   Rscript generate_infographic_ai.R "5 benefits of exercise" -o benefits.png --type list
#   Rscript generate_infographic_ai.R "Global AI market size" -o ai_market.png --type statistical --research
#   Rscript generate_infographic_ai.R "Company history 2010-2025" -o timeline.png --type timeline --style corporate

for (pkg in c("httr2", "jsonlite", "base64enc", "optparse")) {
  if (!requireNamespace(pkg, quietly = TRUE)) {
    cat(sprintf("Error: package '%s' not found. Install with: install.packages('%s')\n", pkg, pkg))
    quit(status = 1)
  }
}
suppressPackageStartupMessages({
  library(httr2)
  library(jsonlite)
  library(base64enc)
  library(optparse)
})


# ── Load .env file ────────────────────────────────────────────────────────────

load_env_file <- function() {
  search_dirs <- c(
    getwd(),
    dirname(normalizePath(getwd())),
    tryCatch({
      cmd_args <- commandArgs(trailingOnly = FALSE)
      sp <- sub("--file=", "", cmd_args[grep("--file=", cmd_args)])
      if (length(sp) > 0) dirname(normalizePath(sp)) else character(0)
    }, error = function(e) character(0))
  )
  for (d in search_dirs) {
    env_path <- file.path(d, ".env")
    if (file.exists(env_path)) {
      lines <- readLines(env_path, warn = FALSE)
      for (line in lines) {
        line <- trimws(line)
        if (nchar(line) == 0 || startsWith(line, "#")) next
        parts <- strsplit(line, "=", fixed = TRUE)[[1]]
        if (length(parts) >= 2) {
          key <- trimws(parts[1])
          val <- trimws(paste(parts[-1], collapse = "="))
          val <- gsub("^['\"]|['\"]$", "", val)
          if (nchar(Sys.getenv(key)) == 0) Sys.setenv(.envir_key = val)
          # Use do.call to set dynamically named env var
          args <- list(val)
          names(args) <- key
          do.call(Sys.setenv, args)
        }
      }
      return(invisible(TRUE))
    }
  }
  invisible(FALSE)
}


# ── Infographic type configurations ──────────────────────────────────────────

INFOGRAPHIC_TYPES <- list(
  statistical = list(
    name = "Statistical/Data-Driven",
    guidelines = "
STATISTICAL INFOGRAPHIC REQUIREMENTS:
- Large, bold numbers that are immediately readable
- Clear data visualization (bar charts, pie charts, or donut charts)
- Data callouts with context (e.g., '15% increase')
- Trend indicators (arrows, growth symbols)
- Legend if multiple data series
- Source attribution area at bottom
- Clean grid or alignment for data elements
"
  ),
  timeline = list(
    name = "Timeline/Chronological",
    guidelines = "
TIMELINE INFOGRAPHIC REQUIREMENTS:
- Clear chronological flow (horizontal or vertical)
- Prominent date/year markers
- Connecting line or path between events
- Event nodes (circles, icons, or markers)
- Brief event descriptions
- Consistent spacing between events
- Visual progression indicating time direction
- Start and end points clearly marked
"
  ),
  process = list(
    name = "Process/How-To",
    guidelines = "
PROCESS INFOGRAPHIC REQUIREMENTS:
- Numbered steps (1, 2, 3, etc.) clearly visible
- Directional arrows between steps
- Action-oriented icons for each step
- Brief action text for each step
- Clear start and end indicators
- Logical flow direction (top-to-bottom or left-to-right)
- Visual connection between sequential steps
"
  ),
  comparison = list(
    name = "Comparison",
    guidelines = "
COMPARISON INFOGRAPHIC REQUIREMENTS:
- Symmetrical side-by-side layout
- Clear headers for each option being compared
- Matching rows/categories for fair comparison
- Visual indicators (checkmarks, X marks, ratings)
- Balanced visual weight on both sides
- Color differentiation between options
- Summary or verdict section if applicable
"
  ),
  list = list(
    name = "List/Informational",
    guidelines = "
LIST INFOGRAPHIC REQUIREMENTS:
- Large, clear numbers or bullet points
- Icons representing each list item
- Brief, scannable text descriptions
- Consistent visual treatment for all items
- Clear hierarchy (title, items, details)
- Adequate spacing between items
- Visual variety to prevent monotony
"
  ),
  geographic = list(
    name = "Geographic/Map-Based",
    guidelines = "
GEOGRAPHIC INFOGRAPHIC REQUIREMENTS:
- Accurate map representation
- Color-coded regions based on data
- Clear legend explaining color scale
- Data callouts for key regions
- Region labels where space permits
- Scale or context indicator
- Clean cartographic style
"
  ),
  hierarchical = list(
    name = "Hierarchical/Pyramid",
    guidelines = "
HIERARCHICAL INFOGRAPHIC REQUIREMENTS:
- Clear pyramid or tree structure
- Distinct levels with visual separation
- Size progression (larger at base, smaller at top)
- Labels for each tier
- Color gradient or distinct colors per level
- Clear top-to-bottom or bottom-to-top hierarchy
- Balanced, centered composition
"
  ),
  anatomical = list(
    name = "Anatomical/Visual Metaphor",
    guidelines = "
ANATOMICAL INFOGRAPHIC REQUIREMENTS:
- Central metaphor image (body, tree, machine, etc.)
- Labeled components with callout lines
- Clear connection between labels and parts
- Explanatory text for each labeled part
- Consistent callout style throughout
- Educational, diagram-like appearance
"
  ),
  resume = list(
    name = "Resume/Professional",
    guidelines = "
RESUME INFOGRAPHIC REQUIREMENTS:
- Professional photo/avatar placeholder area
- Skill visualization (bars, charts, ratings)
- Experience timeline or 

Maintain AI-powered infographic generation using Nano Banana Pro.?

Let people know it's listed here — add the badge (live metrics, light/dark aware) or a plain link to your README or docs.

[AI-powered infographic generation using Nano Banana Pro. on getagentictools](https://getagentictools.com/loops/michalkazimierczak-ai-powered-infographic-generation-using-nano-banana-pro?ref=badge)