Appendix D — Cheatsheets
D.1 Environments as Code
D.1.1 Checking library + repository status
Step | R Command | Python Command |
---|---|---|
Check whether library in sync with lockfile. | renv::status() |
None |
D.1.2 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. Tip: Make sure you’ve got |
renv::init() |
Convention: use |
Activate project library. |
Happens automatically if in RStudio project. |
source <dir> /bin/activate |
Install packages as normal. | install.p ackages("<pkg>") |
python -m pip install <pkg> |
Snapshot package state. | renv::snapshot() |
pip freez e > requirements.txt |
Exit project environment. | Leave R project or re nv::deactivate() |
deactivate |
D.1.3 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. |
Or open project in RStudio. |
cd <project-dir> |
Create project environment. | renv::init() |
Recommend: use |
Enter project environment. | Happens automatically or re nv::activate() |
source <dir> /bin/activate |
Restore packages. | Happens automatically or r env::restore() |
pi p install -r requirements.txt |
D.2 HTTP Code Cheatsheet
As you work more with HTTP traffic, you’ll learn some of the common codes. Here’s a cheatsheet for some of the most frequent you’ll see.
Code | Meaning |
---|---|
200 |
Everyone’s favorite, a successful response. |
3xx |
Your query was redirected somewhere else, usually ok. |
4xx |
Errors with the request |
400 |
Bad request. This isn’t a request the server can understand. |
401 and 403 |
Unauthorized or forbidden. Required authentication hasn’t been provided. |
404 |
Not found. There isn’t any content to access here. |
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. |
D.3 Git
Command | What it does |
git clone <remote> |
Clone a remote repo – make sure you’re using SSH URL. |
git add <files/dir> |
Add files/dir to staging area. |
git commit -m <message> |
Commit staging area. |
git p ush origin <branch> |
Push to a remote. |
git p ull origin <branch> |
Pull from a remote. |
git che ckout <branch name> |
Checkout a branch. |
git checko ut -b <branch name> |
Create and checkout a branch. |
git bran ch -d <branch name> |
Delete a branch. |
D.4 Docker
D.4.1 Docker CLI Commands
S ta ge | Command | What it does | Notes and helpful options |
B ui ld | docker b uild <directory> |
Builds a directory into an image. |
|
Mo ve | doc ker push <image> |
Push a container to a registry. | |
Mo ve | doc ker pull <image> |
Pull a container from a registry. | Rarely needed because run pulls the container if needed. |
R un | do cker run <image> |
Run a container. | See flags in next table. |
R un | docker stop <container> |
Stop a running container. | docker kill can be used if stop fails. |
R un | docker ps |
List running containers. | Useful to get container id to do things to it. |
R un | docker exec <cont ainer> <command> |
Run a command inside a running container. | Basically always used to open a shell with docker exec -it <container> /bin/bash |
R un | docker logs <container> |
Views logs for a container. |
D.4.2 Flags for docker run
Flag | Effect | Notes |
--n ame <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 <po rt>:<port> |
Publish port from inside running inside 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>
D.4.3 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 . |
D.5 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 |
D.6 Command Line
D.6.1 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) |
D.6.3 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 |
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 |
gre p <expression> |
Search a file using regex. | Writing regex can be a pain. I suggest testing on \(\text{regex101.com}\). 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. |
D.6.4 Manipulating Files
Command | What it does | Notes + Helpful Options |
rm <path> |
Remove |
Be very careful, it’s permanent |
c p <from> <to> |
Copy | |
m v <from> <to> |
Move | |
* |
Wildcard | |
mkdir /rmdir |
Make/remove directory | -p - create any parts of path that don’t exist |
D.6.5 Move things to/from server
Command | What it does | Notes + Helpful Options |
tar |
Create/extract archive file | Almost always used with flags. Create is usually Extract is usually |
scp |
Secure copy via ssh |
Run on laptop to server Can use most |
D.6.6 Write files from the command line
Command | What it does | Notes |
---|---|---|
touch |
Creates file if doesn’t already 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 |
D.6.7 Command Line Text Editors (Vim + Nano)
Command | What it does | Notes + Helpful options |
---|---|---|
^ |
Prefix for file command in nano editor. |
Its 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 |
D.7 ssh
ssh <user>@<host>
Flag | What it does | Notes |
---|---|---|
-v |
Verbose, good for debugging. | Add more v s as you please, -vv or -vvv . |
-i |
Choose identity file (private key) | Not necessary with default key names. |
D.8 Linux Admin
D.8.1 Users
Command | What it does | Helpful options + notes |
s u <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. | |
usermo d <username> |
Modify user username |
-aG <group> adds to a group (e.g. sudo ) |
D.8.2 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 . |
chow n <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. |
D.8.3 Install applications (Ubuntu)
Command | What it does |
apt-get u pdate && 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. |
D.8.4 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 Also useful to combine with |
D.8.5 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 |
D.8.6 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>:<i p>:<port>:<host> |
Port forwards a remote port on remote host to local. | Remote Choose local port to match remote port. |
D.8.7 The path
Command | What it does |
which <command> |
Finds the location of the binary that runs when you run command . |
ln -s <location to l ink>:<location of symlink> |
Creates a symlink from file at location to link to location of symlink . |
D.8.8 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 |
D.9 IP Addresses and Ports
D.9.1 Special IP Addresses
Address | Meaning |
---|---|
\(\text{127.0.0.1}\) | \(\text{localhost}\) or loopback – the machine that originated the request. |
\(\text{192.168.x.x}\) \(\text{172.16.x.x.x}\) \(\text{10.x.x.x}\) |
Protected address blocks used for private IP addresses. |
D.9.2 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\) |