Skip to content

Instantly share code, notes, and snippets.

@njudd
Last active October 20, 2025 16:37
Show Gist options
  • Select an option

  • Save njudd/3179354368505c564804b2617da6caf3 to your computer and use it in GitHub Desktop.

Select an option

Save njudd/3179354368505c564804b2617da6caf3 to your computer and use it in GitHub Desktop.
This automatically creates the skeleton for a new R script (newr). Download; make executable `chmod +x newr`; move here `sudo mv newr /usr/local/bin/newr`
#!/bin/bash
# Check if a file name is provided
if [ -z "$1" ]; then
echo "Usage: $0 <filename>"
exit 1
fi
# Extract the file name and ensure it has the .R extension
filename="$1"
if [[ "$filename" != *.R ]]; then
filename="${filename}.R"
fi
# Check if the file already exists
if [ -e "$filename" ]; then
echo "Error: File '$filename' already exists. Aborting to prevent overwriting."
exit 2
fi
# Get the current date in YYYY-MM-DD format
current_date=$(date +"%Y-%m-%d")
# Create the R file with the comment
echo "#### ### ----------------- ### ####
# Nicholas Judd - njudd.com
# Stockholm University & Donders Institute
# File: $filename - Created on $current_date
#### ### ----------------- ### ####
# Goals:
# packages & data
if (!require(pacman)){install.packages('pacman')}; options(scipen = 999)
pacman::p_load(tidyverse, data.table)
" > "$filename"
# Notify the user
echo "Empty R file '$filename' created with the initial comment."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment