Skip to content

Instantly share code, notes, and snippets.

@slowkow
Last active February 5, 2019 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slowkow/52cc2be4acb56ae2e0aae22d6d258034 to your computer and use it in GitHub Desktop.
Save slowkow/52cc2be4acb56ae2e0aae22d6d258034 to your computer and use it in GitHub Desktop.
Make a PCA plot in Bash with the OpenCPU API
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#!/usr/bin/env bash
# pca.sh
#
# Kamil Slowikowski
# 2019-02-04
#
# Make a PCA scatter plot in Bash
# ===============================
#
# This script uses curl to make HTTP requests to call R functions on a remote
# server. Since it is a public server running OpenCPU, anyone can call
# functions on it.
#
# Check out OpenCPU
# -----------------
#
# https://www.opencpu.org/api.html
#
# Show Bash commands as they are executed.
# set -x
# Example 1
#
# Get 3 numbers from a normal distribution with mean = 10 and sd = 10
#
# curl http://cloud.opencpu.org/ocpu/library/stats/R/rnorm/json \
# -H "Content-Type: application/json" -d '{"n":3, "mean": 10, "sd":10}'
# Example 2
#
# Run prcomp on a local matrix in a compressed TSV file.
content="S1 S10 S100 S101 S103
87.4781 72.6434 113.892 98.765 90.5169
64.2295 98.6086 102.7 76.0872 74.7082
1.55164 1.82674 1.31479 1.08724 2.63889
12.8908 28.9163 50.4726 35.169 13.6802
3.68155 1.14048 0 0.417137 0.386618
"
file="mat.tsv.gz"
echo "$content" | gzip > "$file"
# Check that the contents are written properly.
# gzip -cd $file
OCPU=http://cloud.opencpu.org/ocpu
function ocpu() {
command curl -s $OCPU/$1 $2 $3 | head -n1 | cut -f4 -d/
}
x=$(ocpu library/readr/R/read_tsv -F "file=@${file}")
x=$(ocpu library/stats/R/prcomp -d "x=${x}")
x=$(ocpu library/graphics/R/plot -d 'x='${x}'::.val$x[,1:2]')
curl -s $OCPU/tmp/$x/graphics/last/pdf?width=8 > out.pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment