Appendix D — Cheatsheets

Environments as code

Checking library + repository status

Step R Command. Python Command
Check whether library is in sync with lockfile. renv::status() None

Creating and using a standalone project library

Make sure you’re in a standalone project library.

Step R Command Python Command
Create a standalone library.

renv::init()

Tip: get {renv} w/ install.packages("renv")

python -m venv <dir>

Convention: use .venv for <dir>

Tip: {venv} included w/ Python 3.5+

Activate project library.

renv::activate()

Happens automatically if in RStudio project.

source <dir> /bin/activate
Install packages as normal. install.packages("<pkg>") python -m pip install <pkg>
Snapshot package state. renv::snapshot() pip freeze > requirements.txt
Exit project environment. Leave R project or renv::deactivate() deactivate

Collaborating on someone else’s project

Start by downloading the project into a directory on your machine.

Step R Command Python Command
Move into project directory.

setwd("<project-dir>")

Or open project in RStudio.

cd <project-dir>
Create project e nvironment. renv::init()

python -m venv <dir>

Recommend: use .venv for <dir>

Enter project e nvironment. Happens automatically or renv::activate(). source <dir> /bin/activate
Restore packages. Happens automatically or renv::restore(). pip install -r requirements.txt

HTTP code cheatsheet

As you work with HTTP traffic, you’ll learn some of the common codes. Here’s are some of those used most frequently.

Code Meaning
\(200\) Everyone’s favorite, a successful response.
\(\text{3xx}\) Your query was redirected somewhere else, usually ok.
\(\text{4xx}\) Errors with the request
\(400\) Bad request. This isn’t a request the server can understand.
\(401\)/\(403\) Unauthorized or forbidden. Required authentication hasn’t been provided.
\(404\) Not found. There isn’t any content to access here.
\(\text{5xx}\) Errors with the server once your request got there.
\(500\) Generic server-side error. Your request was received, but there was an error processing it.
\(504\) Gateway timeout. This means that a proxy or gateway between you and the server you’re trying to access timed out before it got a response from the server.

Git

Command (prefixed with git) What it does
clone <remote> Clone a remote repo – make sure you’re using SSH URL.
add <files/dir> Add files/directory to staging area.
commit -m <message> Commit staging area.
push origin <branch> Push to a remote.
pull origin <branch> Pull from a remote.
checkout <branch name> Checkout a branch.
checkout -b <branch name> Create and checkout a branch.
branch -d <branch name> Delete a branch.

Docker

Docker CLI commands

Stage Command (prefix w/ docker) What it does Notes and helpful options
Build build <directory> Builds a directory into an image.

-t <name:tag> provides a name to the container.

tag is optional, defaults to latest.

Move push <image> Push a container to a registry.
Move pull <image> Pull a container from a registry. Rarely needed because run pulls the container if needed.
Run run <image> Run a container. See flags in next table.
Run stop <container> Stop a running container. docker kill can be used if stop fails.
Run ps List running c ontainers. Useful to get container id to do things to it.
Run exec <container> <command> Run a command inside a running container. Basically always used to open a shell with docker exec -it <container> /bin/bash
Run logs <container> Views logs for a container.

Flags for docker run

Flag Effect Notes
--name <name> Give a name to container. Optional. Auto-assigned if not provided
--rm Remove container when its stopped. Don’t use in production. You probably want to inspect failed containers.
-d Detach container (don’t block the terminal). Almost always used in production.
-p <port>:<port> Publish port from inside running container to outside. Needed if you want to access an app or API inside the container.
-v <dir>:<dir> Mount volume into the container.

Reminder: Order for -p and -v is <host>:<container>

Dockerfile commands

These are the commands that go in a Dockerfile when you’re building it.

Command Purpose Example
FROM Indicate base container. FROM rocker/r-ver:4.1.0
RUN Run a command when building. RUN apt-get update
COPY Copy from build directory into the container. COPY . /app/
CMD Specify the command to run when the container starts. CMD quarto render .

Cloud services

Service AWS Azure GCP
Kubernetes cluster EKS or Fargate AKS GKE
Run a container or application ECS or Elastic Beanstalk Azure Container Apps Google App Engine
Run an API Lambda Azure Functions Google Cloud Functions
Database RDS Azure SQL Google Cloud Database
Data Warehouse Redshift DataLake BigQuery
ML Platform SageMaker Azure ML Vertex AI
NAS EFS or FSx Azure File Filestore

Command line

General command line

Symbol What it is
man <command> Open manual for command.
q Quit the current screen.
\ Continue bash command on new line.
ctrl + c Quit current execution.
echo <string> Print string (useful for piping).

Linux filesystem navigation

Command What it does/is Notes + Helpful options
/ System root or file path separator.
. Current working directory.
.. Parent of working directory.
~ Home directory of the current user.
ls <dir> List objects in a directory.

-l - format as list

-a - all (include hidden files that start with .)

pwd Print working directory.
cd <dir> Change directory. Can use relative or absolute paths.

Reading text files

Command What it does Notes + Helpful options
cat <file> Print a file from the top.
less <file> Print a file, but just a little.

Can be very helpful to look at a few rows of csv.

Lazily reads lines, so can be much faster than cat for big files.

head <file> Look at the beginning of a file. Defaults to 10 lines, can specify a different number with -n <n>.
tail <file> Look at the end of a file.

Useful for logs where the newest part is last.

The -f flag is useful to follow for a live view.

grep <expression> Search a file using regex.

Writing regex can be a pain. I suggest testing on .

Often useful in combination with the pipe.

| The pipe.
wc <file> Count words in a file. Use -l to count lines, useful for .csv files.

Manipulating files

Command What it does Notes + Helpful Options
rm <path> Remove.

-r - recursively remove everything below a file path

-f - force - dont ask for each file

Be very careful, its permanent!

cp <from> <to> Copy.
mv <from> <to> Move.
* Wildcard.
mkdir /rmdir Make/remove directory. -p - create any parts of path that dont exist

Move things to/from server

Command What it does Notes + Helpful Options
tar C reate/extract archive file.

Almost always used with flags.

Create is usually tar -czf <archive> <file(s)>

Extract is usually tar -xfv <archive>

scp Secure copy via ssh.

Run on laptop to server.

Can use most ssh flags (like -i and -v).

Write files from the command line

Command What it does Notes
touch Creates file if doesn’t exist. Updates last updated to current time if it does exist.
> Overwrite file contents. Creates a new file if it doesn’t exist.
>> Concatenate to end of file. Creates a new file if it doesn’t exist.

Command line text editors (Vim + Nano)

Command What it does Notes + Helpful options
^ Prefix for file command in nano editor. It’s the or Ctrl key, not the caret symbol.
i Enter insert mode (able to type) in vim.
escape Enter normal mode (navigation) in vim.
:w Write the current file in vim (from normal mode). Can be combined to save and quit in one, :wq.
:q Quit vim (from normal mode). :q! quit without saving.

SSH

General usage:

ssh <user>@<host>
Flag What it does Notes
-v Verbose, good for debugging. Add more vs as you please, -vv or -vvv.
-i Choose identity file (private key). Not necessary with default key names.

Linux admin

Users

Command What it does Helpful options + notes
su <username> Change to be a different user.
whoami Get username of current user.
id Get full user + group info on current user.
passwd Change password.
useradd Add a new user.
usermod <username> Modify user username. -aG <group> adds to a group (e.g.,sudo)

Permissions

Command What it does Helpful options + notes
chmod <permissions> <file> Modifies permissions on a file or directory. Number indicates permissions for user, group, others: add 4 for read, 2 for write, 1 for execute, 0 for nothing, e.g.,644.
chown <user>:<group> <file> Change the owner of a file or directory. Can be used for user or group, e.g.,:my-group.
sudo <command> Adopt root permissions for the following command.

Install applications (Ubuntu)

Command What it does
apt-get update && apt-get upgrade -y. Fetch and install upgrades to system packages
apt-get install <package> Install a system package.
wget Download a file from a URL.
gdebi Install local .deb file.

Storage

Command What it does Helpful options
df Check storage space on device. -h for human readable file sizes.
du Check size of files.

Most likely to be used as du -h <dir> | sort -h

Also useful to combine with head.

Processes

Command What it does Helpful options
top See what’s running on the system.
ps aux See all system processes. Consider using --sort and pipe into head or grep.
kill Kill a system process. -9 to force kill immediately

Networking

Command What it does Helpful Options
netstat See ports and services using them. Usually used with -tlp, for tcp listening applications, including pid.
ssh -L <port>:<ip>:<port>:<host> Port forwards a remote port on remote host to local.

Remote ip is usually localhost.

Choose local port to match remote port.

The path

Command What it does
which <command> Finds the location of the binary that runs when you run command.
ln -s <linked location> <where to put symlink> Creates a symlink from file/directory at linked location to where to put symlink.

systemd

Daemonizing services is accomplished by configuring them in /etc/systemd/system/<service name>.service.

The format of all commands is systemctl <command> <application>.

Command Notes/Tips
status Report status.
start
stop
restart stop then start.
reload Reload configuration that doesn’t require restart (depends on service).
enable Daemonize the service.
disable Un-daemonize the service.

IP Addresses and ports

Special IP Addresses

Address Meaning
\(127.0.0.1\) \(\text{localhost}\) or loopback – the machine that originated the request.

\(\text{10.x.x.x}\)

\(\text{172.16.x.x}\) to \(\text{172.31.x.x}\)

\(\text{192.168.x.x}\)

Protected address blocks used for private IP addresses.

Special ports

All ports below \(1024\) are reserved for server tasks and cannot be assigned to admin-controlled services.

Protocol/application Default port
HTTP \(80\)
HTTPS \(443\)
SSH \(22\)
PostgreSQL \(5432\)
RStudio Server \(8787\)
Shiny Server \(3939\)
JupyterHub \(8000\)