Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 43 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,66 +35,69 @@ Visit the live website: [here](https://gnoblet.github.io/TidyTuesday/)

## 📝 Creating New Visualizations

### Quick Template Creation

Use the included script to generate a new visualization from the template:
Use the included script to generate a new visualization from the template in R:

```bash
# Create a new R analysis
./new-viz-from-template.sh 2024-12-15 r "Coffee Analysis"

# Create a new Python analysis
./new-viz-from-template.sh 2024-12-15 python "Climate Data Study"

# Example with single word title
./new-viz-from-template.sh 2024-12-20 r "Olympics"
# Create a new R post from the template
# Usage:
# Rscript posts/new-viz-from-template.R [-c "categories"] [-u "tools used"] [-k "key libraries"] [-f image_file] <date> <title>
#
# Notes:
# - Creates: posts/<YEAR>/week_<N>/week_<N>.qmd
# - Default image filename: weekNN.png (zero-padded) placed inside that week folder, e.g. posts/2025/week_01/week01.png
# - The script replaces placeholders in template.qmd; ensure template.qmd is present.
# - The script will update _quarto.yml to add the new post entry when possible.
# - Week number = ISO week of the date minus one (keeps existing behavior).

# Examples
# Minimal (uses defaults and weekXX.png):
Rscript new-viz-from-template.R 2025-01-07 "Coffee Analysis"

# With metadata flags and explicit image name:
Rscript new-viz-from-template.R \
-c "ggplot2, tidyverse" \
-u "R, ggplot2, tidyverse" \
-k "ggplot2, dplyr, tidyr" \
-f week01.png \
2025-01-07 "Coffee Analysis"

# If you only want to override the image name:
Rscript new-viz-from-template.R -f custom-plot.png 2025-01-07 "Coffee Analysis"

# Quick help:
Rscript new-viz-from-template.R -h
```

**Arguments:**
- `date`: Date in YYYY-MM-DD format (e.g., 2024-12-15)
- `language`: Programming language ('r' or 'python')
- `title`: Title for the analysis (use quotes if it contains spaces)
- `-c "categories"`: Comma-separated categories (default: "TidyTuesday")
- `-u "tools used"`: Comma-separated tools used (default: "R
- `-k "key libraries"`: Comma-separated key libraries (default: "ggplot2, dplyr")
- `-f image_file`: Filename for the main visualization image (default: weekNN.png

This will create:
- A `.qmd` file in `r/` or `python/` directory with a complete template
- A `.qmd` file in the `posts/` directory with a complete template
- All placeholders automatically replaced with your specified values
- Ready-to-edit analysis structure

### Template Structure

Each generated visualization includes:

- **Overview**: Description of the analysis and approach
- **Dataset**: Data loading and exploration
- **Analysis**: Data preparation and key insights
- **Visualization**: Main plots and additional analysis
- **Technical Notes**: Tools, libraries, and methodology
- **Viz**: The output viz

### Manual Creation

You can also manually create new visualizations:

1. Copy `template.qmd` to your desired location
2. Replace all `{{PLACEHOLDER}}` values
3. Add your analysis code
4. Render with `quarto render filename.qmd`

### Automatic Gallery Integration
## Automatic Gallery Integration

- **New analyses automatically appear** in the gallery when you render the site
- **No manual updates needed** - the gallery scans for .qmd files dynamically
- **Consistent formatting** across all projects

### Manual Setup
## Manual Setup

#### R Environment (renv)
### R Environment (renv)
```bash
# Restore R packages
R -e "renv::restore()"
```

#### Python Environment (uv)
### Python Environment (uv)
```bash
# Create virtual environment
uv venv .venv
Expand All @@ -107,7 +110,7 @@ source .venv/bin/activate.fish
uv sync
```

#### Build Website
### Build Website
```bash
# Render the website
quarto render
Expand All @@ -119,15 +122,14 @@ quarto preview
## 📁 Project Structure

```
├── .github/workflows/ # GitHub Actions for deployment
├── r/ # R project pages (Quarto)
├── python/ # Python project pages (Quarto)
├── .github/workflows/ # GitHub Actions for deployment
├── posts/ # Project posts (Quarto)
├── _site/ # Generated website (ignored)
├── renv/ # R environment
├── .venv/ # Python virtual environment (ignored)
├── requirements.txt # Python dependencies
├── renv.lock # R package lockfile
└── _quarto.yml # Quarto configuration
├── renv.lock # R package lockfile
└── _quarto.yml # Quarto configuration
```

## 🔄 Deployment
Expand All @@ -140,15 +142,6 @@ The website is automatically deployed to GitHub Pages when changes are pushed to
4. Renders the Quarto website
5. Deploys to GitHub Pages

## 📊 Adding New Projects

### R Projects
1. Add a corresponding Quarto document in `r/YYYY-MM-DD.qmd`
2. Update `_quarto.yml` sidebar navigation (automated via ./new-viz-from-template.sh)

### Python Projects
1. Add a corresponding Quarto document in `python/YYYY-MM-DD.qmd`
2. Update `_quarto.yml` sidebar navigation (automated via ./new-viz-from-template.sh)

## 🛠️ Package Management

Expand Down
3 changes: 3 additions & 0 deletions _quarto.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
project:
type: website
output-dir: _site
render:
- "*.qmd"
- "!template.qmd"

website:
title: "@GNOBLET TidyTuesday Visualizations"
Expand Down
2 changes: 2 additions & 0 deletions index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ listing:
---Guillaume Noblet

</div>

<br>
124 changes: 124 additions & 0 deletions new-viz-from-template.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#!/usr/bin/env Rscript
# new-viz-from-template.R
# Usage:
# Rscript new-viz-from-template.R [-c "categories"] [-u "tools used"] [-k "key libraries"] [-f image_file] <date> <title>

args <- commandArgs(trailingOnly = TRUE)

usage <- function() {
cat("Usage:\n")
cat(
" Rscript new-viz-from-template.R [-c \"categories\"] [-u \"tools used\"] [-k \"key libraries\"] [-f image_file] <date> <title>\n"
)
quit(status = 1)
}

# parse flags
opts <- list(categories = NULL, tools = NULL, keylibs = NULL, image = NULL)
i <- 1
while (i <= length(args) && startsWith(args[i], "-")) {
opt <- args[i]
if (opt %in% c("-h", "--help")) {
usage()
}
if (i == length(args)) {
cat("Missing value for option", opt, "\n")
usage()
}
val <- args[i + 1]
if (opt == "-c") {
opts$categories <- val
} else if (opt == "-u") {
opts$tools <- val
} else if (opt == "-k") {
opts$keylibs <- val
} else if (opt == "-f") {
opts$image <- val
} else {
cat("Unknown option:", opt, "\n")
usage()
}
i <- i + 2
}

pos <- if (i <= length(args)) args[i:length(args)] else character(0)
if (length(pos) != 2) {
usage()
}
DATE <- pos[1]
TITLE <- pos[2]

if (!grepl("^\\d{4}-\\d{2}-\\d{2}$", DATE)) {
stop("Date must be in YYYY-MM-DD format")
}

# defaults for R metadata
if (is.null(opts$categories)) {
opts$categories <- "ggplot2, tidyverse, data-viz"
}
if (is.null(opts$tools)) {
opts$tools <- "R, ggplot2, tidyverse"
}
if (is.null(opts$keylibs)) {
opts$keylibs <- "ggplot2, dplyr, tidyr"
}

YEAR <- substr(DATE, 1, 4)
week_v <- as.integer(format(as.Date(DATE), "%V"))
WEEK <- week_v - 1
if (is.na(WEEK) || WEEK < 0) {
WEEK <- 0
}
WEEK_DIR <- paste0("week_", WEEK)
WEEK_FILE <- paste0(WEEK_DIR, ".qmd")

# default image name: weekNN.png (zero-padded)
default_image <- sprintf("week%02d.png", WEEK)
if (is.null(opts$image)) {
opts$image <- default_image
}

# Place the post inside posts/<YEAR>/week_<N>/
LANG_DIR <- file.path("posts", YEAR, WEEK_DIR)
QMD_FILE <- file.path(LANG_DIR, WEEK_FILE)

if (!dir.exists(LANG_DIR)) {
dir.create(LANG_DIR, recursive = TRUE, showWarnings = FALSE)
}
if (file.exists(QMD_FILE)) {
stop(sprintf("Error: File %s already exists", QMD_FILE))
}
if (!file.exists("template.qmd")) {
stop("template.qmd not found in current directory")
}

file.copy("template.qmd", QMD_FILE, overwrite = FALSE)
lines <- readLines(QMD_FILE, warn = FALSE)

repls <- list(
"{{TITLE}}" = TITLE,
"{{DATE}}" = DATE,
"{{WEEK}}" = WEEK,
"{{YEAR}}" = YEAR,
"{{LANGUAGE}}" = "r",
"{{LANGUAGE_UPPER}}" = "R",
"{{LANGUAGE_CODE}}" = "r",
"{{LANGUAGE_EXT}}" = "R",
"{{CATEGORIES}}" = opts$categories,
"{{TOOLS_USED}}" = opts$tools,
"{{KEY_LIBRARIES}}" = opts$keylibs,
"{{IMAGE_FILE}}" = opts$image
)

for (ph in names(repls)) {
lines <- gsub(ph, repls[[ph]], lines, fixed = TRUE)
}

writeLines(lines, QMD_FILE)

cat(sprintf("✅ Created new visualization: %s\n", QMD_FILE))
cat(sprintf("📁 Folder structure: %s/\n", LANG_DIR))
cat("\nNext steps:\n")
cat(" 1. Edit the new qmd file to add your analysis\n")
cat(sprintf(" 2. Save the image as %s inside %s\n", opts$image, LANG_DIR))
cat(" 3. Run 'quarto render' to build the site\n")
Loading
Loading