SlideShare a Scribd company logo
Docopt, beautiful command-line options for R
Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS)
July 2014, UseR!2014
Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS)
Docopt, beautiful command-line options for R
What is docopt?
docopt is an utility R library for parsing command-line options. It is
a port of docopt.py (python).
How does it work?
You supply a properly formed help description
docopt creates from this a fully functional command-line
parser
Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS)
Docopt, beautiful command-line options for R
Why Command-line?
R is used more and more:
Ad hoc, interactive analysis, e.g
R REPL shell
RStudio
interactive data analysis
Creating R libraries with vi, Rstudio etc.
no data analysis
But also for repetitive batch jobs:
Rscript my_script.R arg1 arg2 . . .
R -f my_script.R --args arg1 arg2 . . .
reproducible data processing
So also more and more Command-line!
Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS)
Docopt, beautiful command-line options for R
Rscript example
#!/usr/bin/Rscript
my_model <- glm( data=iris
, Sepal.Width ~ Sepal.Length
)
print(coef(my_model))
Hmm, that script only works for this speci๏ฌc data set.
I Need Arguments and Options!
Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS)
Docopt, beautiful command-line options for R
Command-line parameters
Parsing command-line parameters seems easy, but what about:
Switches? e.g. --debug, --help
Short names and long names? -d, -h vs --debug, --help?
Options with a value? --output=garbage.csv
Arguments e.g. input_file.csv?
Optional arguments?
default values for options?
documenting all options and arguments?
That is a lot of work for just a batch script. . .
Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS)
Docopt, beautiful command-line options for R
Retrieving command-line options
What libraries available?
base::commandArgs (very primitive)
library(getopt): (basic)
library(argparse), Python dependency
library(optparse) very nice, Python inspired
These are all ๏ฌne, but result in a lot of parsing or settting-up code
in your script. (and that is not what your script is about. . . )
docopt is di๏ฌ€erent.
Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS)
Docopt, beautiful command-line options for R
What is Docopt?
Originally a Python lib: http://docopt.org
It is a Command Line Interface Speci๏ฌcation language:
You specify your help and docopt parser takes care of
everything.
The documentation = the speci๏ฌcation.
Your script starts with the command-line help
docopt automatically has --help or -h switch to supply help
to users of your script.
It will stop when obligatory switch are not set or non existing
options are set.
Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS)
Docopt, beautiful command-line options for R
Simple example
#!/usr/bin/Rscript
"This is my incredible script
Usage: my_inc_script.R [-v --output=<output>] FILE
" -> doc
library(docopt)
my_opts <- docopt(doc)
Thatโ€™s all you need to handle your command-line options.
Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS)
Docopt, beautiful command-line options for R
Options
Docopt lets you parse:
Both short as long options
Default values
Descriptions of parameters
Optional parameters: my_script.R [-a -b]
Commands: my_script.R (lm | summary)
Positional arguments
Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS)
Docopt, beautiful command-line options for R
Usage patterns
Syntax is de๏ฌned at http://docopt.org
Start with Usage:
"Usage:
script.R --option <argument>
script.R [<optional-argument>]
script.R --another-option=<with-argument>
script.R (--either-that-option | <or-this-argument>)
script.R <repeating-argument> <repeating-argument>...
" -> doc
Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS)
Docopt, beautiful command-line options for R
Longer example
#!/usr/bin/Rscript
"This is my useful scriptI I use on everything
Usage: my_uf_script.R [options] FILE
Options:
-b --bogus This is a bogus switch
-o --output=OUTPUT output file [default: out.csv]
Arguments:
FILE the input file" -> doc
library(docopt)
my_opts <- docopt(doc)
Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS)
Docopt, beautiful command-line options for R
Recall ๏ฌrst example
Lets make a CLI for our script
#!/usr/bin/Rscript
my_model <- glm( data=iris
, Sepal.Width ~ Sepal.Length
)
print(coef(my_model))
Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS)
Docopt, beautiful command-line options for R
Preparing. . .
#!/usr/bin/Rscript
main <- function( DATA, response, terms, family){
data <- read.csv(DATA)
f <- as.formula(paste0(response, " ~ ", terms))
my_model <- glm(f, family=family, data=data)
print(coef(my_model))
}
Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS)
Docopt, beautiful command-line options for R
Done!
"Usage: my_script.R --response=<y> --terms=<x>
[--family=<family>] DATA
Options:
-r --response=<y> Response for glm
-t --terms=<x> Terms for glm
-f --family=<family> Family [default: gaussian]
Arguments:
DATA Input data frame" -> doc
main <- function( DATA, response, terms, family){...}
opt <- docopt::docopt(doc)
main(opt$DATA, opt[["--response"]], opt[["--terms"]],
opt[["--family"]])
Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS)
Docopt, beautiful command-line options for R
Implementation
Docopt is implemented:
using Reference classes (R5) in pure R.
It is port of the original Python project: http://docopt.org
Available from: CRAN and
https://github.com/edwindj/docopt.R
Very functional, except for:
multiple identical arguments -vvv
repeating arguments (both will be ๏ฌxed soon)
Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS)
Docopt, beautiful command-line options for R
Questions?
$ my_talk.R --help
Edwins talk on docopt
Usage: my_talk.R (--questions | --fell-asleep)
Options:
-q --questions Anyone any questions?
-f --fell-asleep Wake up! Next UseR talk!
$ my_talk.R --questions
Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS)
Docopt, beautiful command-line options for R
Questions?
Thanks for listening!
Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS)
Docopt, beautiful command-line options for R

More Related Content

What's hot (20)

PPT
Die Schulsachen
Alexandra Vilaplana
ย 
PDF
Apache CouchDB
Trinh Phuc Tho
ย 
PPTX
Steigerung
Romanychch
ย 
PPT
Breadth first search and depth first search
Hossain Md Shakhawat
ย 
PPTX
Brille, Tasche, Tuch, Kopftuch - anziehen? ausziehen? : NEIN!
Maria Vaz Kรถnig
ย 
PPTX
VERGANGENHEIT - Prรคteritum - Perfekt
Maria Vaz Kรถnig
ย 
PDF
Spark SQL
Joud Khattab
ย 
PPTX
SPARQL Cheat Sheet
LeeFeigenbaum
ย 
PPTX
RDF, linked data and semantic web
Jose Emilio Labra Gayo
ย 
PPT
Regular Grammar
Ruchika Sinha
ย 
PPTX
Transformations and actions a visual guide training
Spark Summit
ย 
PPTX
Mongodb
SARAVANAN GOPALAKRISHNAN
ย 
PPTX
PARTIZIP I und PARTIZIP II als ADJEKTIVE - Theorie und รœbungen
Maria Vaz Kรถnig
ย 
PDF
Python openpyxl
Amarjeetsingh Thakur
ย 
PPTX
Json
Shyamala Prayaga
ย 
PPTX
HBase Tutorial For Beginners | HBase Architecture | HBase Tutorial | Hadoop T...
Simplilearn
ย 
PPTX
Direktionaladverbien wo ist das bad
Ilse Gruenbart
ย 
PPTX
NOMEN und PERSONALPRONOMEN - รœBUNGEN zur SATZSTELLUNG- Nominativ, Dativ, Akku...
Maria Vaz Kรถnig
ย 
PPTX
Dfs presentation
Alizay Khan
ย 
Die Schulsachen
Alexandra Vilaplana
ย 
Apache CouchDB
Trinh Phuc Tho
ย 
Steigerung
Romanychch
ย 
Breadth first search and depth first search
Hossain Md Shakhawat
ย 
Brille, Tasche, Tuch, Kopftuch - anziehen? ausziehen? : NEIN!
Maria Vaz Kรถnig
ย 
VERGANGENHEIT - Prรคteritum - Perfekt
Maria Vaz Kรถnig
ย 
Spark SQL
Joud Khattab
ย 
SPARQL Cheat Sheet
LeeFeigenbaum
ย 
RDF, linked data and semantic web
Jose Emilio Labra Gayo
ย 
Regular Grammar
Ruchika Sinha
ย 
Transformations and actions a visual guide training
Spark Summit
ย 
PARTIZIP I und PARTIZIP II als ADJEKTIVE - Theorie und รœbungen
Maria Vaz Kรถnig
ย 
Python openpyxl
Amarjeetsingh Thakur
ย 
HBase Tutorial For Beginners | HBase Architecture | HBase Tutorial | Hadoop T...
Simplilearn
ย 
Direktionaladverbien wo ist das bad
Ilse Gruenbart
ย 
NOMEN und PERSONALPRONOMEN - รœBUNGEN zur SATZSTELLUNG- Nominativ, Dativ, Akku...
Maria Vaz Kรถnig
ย 
Dfs presentation
Alizay Khan
ย 

Viewers also liked (19)

PDF
TestR: generating unit tests for R internals
Roman Tsegelskyi
ย 
PDF
Seefeld stats r_bio
haramaya university
ย 
PPTX
Extending and customizing ibm spss statistics with python, r, and .net (2)
Armand Ruis
ย 
PPTX
R Statistics
r content
ย 
PDF
Statistics with R
Johnson Hsieh
ย 
PDF
Getting Up to Speed with R: Certificate Program in R for Statistical Analysis...
Revolution Analytics
ย 
PDF
3 descriptive statistics with R
naroranisha
ย 
PPTX
Presentation on use of r statistics
Krishna Dhakal
ย 
PDF
Dependencies and Licenses
Robert Reiz
ย 
PDF
Descriptive Statistics with R
Kazuki Yoshida
ย 
PDF
Chunked, dplyr for large text files
Edwin de Jonge
ย 
PPTX
Introduction to basic statistics
IBM
ย 
PPTX
How to use Logistic Regression in GIS using ArcGIS and R statistics
Omar F. Althuwaynee
ย 
PPTX
Why R? A Brief Introduction to the Open Source Statistics Platform
Syracuse University
ย 
PDF
ุนุฑุถ ู…ุญุงุถุฑุฉ ูƒูŠููŠุฉ ุงู†ุดุงุก ุงู„ู…ุทุงุนู… ู„ุฑุคูˆุณ ุงู„ุงู…ูˆุงู„ ุงู„ู†ุงุดุฆุฉ ูˆุงู„ู…ุจุชุฏุฆุฉ
Mazen AlDarrab
ย 
PDF
Class ppt intro to r
JigsawAcademy2014
ย 
PDF
R statistics with mongo db
MongoDB
ย 
PDF
Data analysis using spss
Muhammad Ibrahim
ย 
TestR: generating unit tests for R internals
Roman Tsegelskyi
ย 
Seefeld stats r_bio
haramaya university
ย 
Extending and customizing ibm spss statistics with python, r, and .net (2)
Armand Ruis
ย 
R Statistics
r content
ย 
Statistics with R
Johnson Hsieh
ย 
Getting Up to Speed with R: Certificate Program in R for Statistical Analysis...
Revolution Analytics
ย 
3 descriptive statistics with R
naroranisha
ย 
Presentation on use of r statistics
Krishna Dhakal
ย 
Dependencies and Licenses
Robert Reiz
ย 
Descriptive Statistics with R
Kazuki Yoshida
ย 
Chunked, dplyr for large text files
Edwin de Jonge
ย 
Introduction to basic statistics
IBM
ย 
How to use Logistic Regression in GIS using ArcGIS and R statistics
Omar F. Althuwaynee
ย 
Why R? A Brief Introduction to the Open Source Statistics Platform
Syracuse University
ย 
ุนุฑุถ ู…ุญุงุถุฑุฉ ูƒูŠููŠุฉ ุงู†ุดุงุก ุงู„ู…ุทุงุนู… ู„ุฑุคูˆุณ ุงู„ุงู…ูˆุงู„ ุงู„ู†ุงุดุฆุฉ ูˆุงู„ู…ุจุชุฏุฆุฉ
Mazen AlDarrab
ย 
Class ppt intro to r
JigsawAcademy2014
ย 
R statistics with mongo db
MongoDB
ย 
Data analysis using spss
Muhammad Ibrahim
ย 
Ad

Similar to Docopt, beautiful command-line options for R, user2014 (20)

PPTX
R lecture oga
Osamu Ogasawara
ย 
PDF
PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop - Xi...
The Statistical and Applied Mathematical Sciences Institute
ย 
PDF
Rtips123
Mahendra Babu
ย 
PDF
Data Analysis with R (combined slides)
Guy Lebanon
ย 
PDF
Learning notes of r for python programmer (Temp1)
Chia-Chi Chang
ย 
PPT
Plot function in R
Vladimir Bakhrushin
ย 
PDF
Poetry with R -- Dissecting the code
Peter Solymos
ย 
PDF
Introduction to R programming
Alberto Labarga
ย 
PPTX
R1-Intro (2udsjhfkjdshfkjsdkfhsdkfsfsffs
sabari Giri
ย 
PPT
R Basics
AllsoftSolutions
ย 
PPTX
Data Exploration in R.pptx
Ramakrishna Reddy Bijjam
ย 
PPTX
R language introduction
Shashwat Shriparv
ย 
PDF
Introduction to R - Lab slides for UGA course FANR 6750
richardchandler
ย 
PDF
Poly_introduction_R.pdf
BenjaminTheodorNicai
ย 
PPTX
Getting Started with R
Sankhya_Analytics
ย 
PDF
Mixed Effects Models - Descriptive Statistics
Scott Fraundorf
ย 
PPTX
Workshop presentation hands on r programming
Nimrita Koul
ย 
PDF
RDataMining slides-r-programming
Yanchang Zhao
ย 
PDF
20170509 rand db_lesugent
Prof. Wim Van Criekinge
ย 
PDF
ู…ุญุงุถุฑุฉ ุจุฑู†ุงู…ุฌ ุงู„ุชุญู„ูŠู„ ุงู„ูƒู…ูŠ R program ุฏ.ู‡ุฏูŠู„ ุงู„ู‚ููŠุฏูŠ
ู…ุฑูƒุฒ ุงู„ุจุญูˆุซ ุงู„ุฃู‚ุณุงู… ุงู„ุนู„ู…ูŠุฉ
ย 
R lecture oga
Osamu Ogasawara
ย 
PMED Undergraduate Workshop - R Tutorial for PMED Undegraduate Workshop - Xi...
The Statistical and Applied Mathematical Sciences Institute
ย 
Rtips123
Mahendra Babu
ย 
Data Analysis with R (combined slides)
Guy Lebanon
ย 
Learning notes of r for python programmer (Temp1)
Chia-Chi Chang
ย 
Plot function in R
Vladimir Bakhrushin
ย 
Poetry with R -- Dissecting the code
Peter Solymos
ย 
Introduction to R programming
Alberto Labarga
ย 
R1-Intro (2udsjhfkjdshfkjsdkfhsdkfsfsffs
sabari Giri
ย 
R Basics
AllsoftSolutions
ย 
Data Exploration in R.pptx
Ramakrishna Reddy Bijjam
ย 
R language introduction
Shashwat Shriparv
ย 
Introduction to R - Lab slides for UGA course FANR 6750
richardchandler
ย 
Poly_introduction_R.pdf
BenjaminTheodorNicai
ย 
Getting Started with R
Sankhya_Analytics
ย 
Mixed Effects Models - Descriptive Statistics
Scott Fraundorf
ย 
Workshop presentation hands on r programming
Nimrita Koul
ย 
RDataMining slides-r-programming
Yanchang Zhao
ย 
20170509 rand db_lesugent
Prof. Wim Van Criekinge
ย 
ู…ุญุงุถุฑุฉ ุจุฑู†ุงู…ุฌ ุงู„ุชุญู„ูŠู„ ุงู„ูƒู…ูŠ R program ุฏ.ู‡ุฏูŠู„ ุงู„ู‚ููŠุฏูŠ
ู…ุฑูƒุฒ ุงู„ุจุญูˆุซ ุงู„ุฃู‚ุณุงู… ุงู„ุนู„ู…ูŠุฉ
ย 
Ad

More from Edwin de Jonge (15)

PDF
sdcSpatial user!2019
Edwin de Jonge
ย 
PDF
Validatetools, resolve and simplify contradictive or data validation rules
Edwin de Jonge
ย 
PDF
Data error! But where?
Edwin de Jonge
ย 
PDF
Daff: diff, patch and merge for data.frame
Edwin de Jonge
ย 
PDF
Uncertainty visualisation
Edwin de Jonge
ย 
PDF
Heatmaps best practices Strata Hadoop
Edwin de Jonge
ย 
PPTX
Big data experiments
Edwin de Jonge
ย 
PPTX
StatMine
Edwin de Jonge
ย 
PDF
Big Data Visualization
Edwin de Jonge
ย 
PDF
ffbase, statistical functions for large datasets
Edwin de Jonge
ย 
PDF
Tabplotd3, interactive inspection of large data
Edwin de Jonge
ย 
PPTX
Big data as a source for official statistics
Edwin de Jonge
ย 
PPT
Statmine, Visuele dataexploratie
Edwin de Jonge
ย 
PPTX
StatMine (New Technologies and Techniques for Statistics)
Edwin de Jonge
ย 
PPT
StatMine, visual exploration of output data
Edwin de Jonge
ย 
sdcSpatial user!2019
Edwin de Jonge
ย 
Validatetools, resolve and simplify contradictive or data validation rules
Edwin de Jonge
ย 
Data error! But where?
Edwin de Jonge
ย 
Daff: diff, patch and merge for data.frame
Edwin de Jonge
ย 
Uncertainty visualisation
Edwin de Jonge
ย 
Heatmaps best practices Strata Hadoop
Edwin de Jonge
ย 
Big data experiments
Edwin de Jonge
ย 
StatMine
Edwin de Jonge
ย 
Big Data Visualization
Edwin de Jonge
ย 
ffbase, statistical functions for large datasets
Edwin de Jonge
ย 
Tabplotd3, interactive inspection of large data
Edwin de Jonge
ย 
Big data as a source for official statistics
Edwin de Jonge
ย 
Statmine, Visuele dataexploratie
Edwin de Jonge
ย 
StatMine (New Technologies and Techniques for Statistics)
Edwin de Jonge
ย 
StatMine, visual exploration of output data
Edwin de Jonge
ย 

Recently uploaded (20)

PPTX
BB FlashBack Pro 5.61.0.4843 With Crack Free Download
cracked shares
ย 
PPTX
Build a Custom Agent for Agentic Testing.pptx
klpathrudu
ย 
PDF
SAP GUI Installation Guide for macOS (iOS) | Connect to SAP Systems on Mac
SAP Vista, an A L T Z E N Company
ย 
PPTX
Function & Procedure: Function Vs Procedure in PL/SQL
Shani Tiwari
ย 
PDF
Meet in the Middle: Solving the Low-Latency Challenge for Agentic AI
Alluxio, Inc.
ย 
PDF
How to Download and Install ADT (ABAP Development Tools) for Eclipse IDE | SA...
SAP Vista, an A L T Z E N Company
ย 
PPTX
UI5con_2025_Accessibility_Ever_Evolving_
gerganakremenska1
ย 
PPTX
API DOCUMENTATION | API INTEGRATION PLATFORM
philipnathen82
ย 
PDF
custom development enhancement | Togglenow.pdf
aswinisuhu
ย 
PDF
Show Which Projects Support Your Strategy and Deliver Results with OnePlan df
OnePlan Solutions
ย 
PPTX
Transforming Insights: How Generative AI is Revolutionizing Data Analytics
LetsAI Solutions
ย 
PDF
SAP GUI Installation Guide for Windows | Step-by-Step Setup for SAP Access
SAP Vista, an A L T Z E N Company
ย 
PDF
Notification System for Construction Logistics Application
Safe Software
ย 
PDF
Ready Layer One: Intro to the Model Context Protocol
mmckenna1
ย 
PDF
ERP Consulting Services and Solutions by Contetra Pvt Ltd
jayjani123
ย 
PDF
Step-by-Step Guide to Install SAP HANA Studio | Complete Installation Tutoria...
SAP Vista, an A L T Z E N Company
ย 
PDF
Salesforce Experience Cloud Consultant.pdf
VALiNTRY360
ย 
PPTX
Cutting Optimization Pro 5.18.2 Crack With Free Download
cracked shares
ย 
PDF
How Attendance Management Software is Revolutionizing Education.pdf
Pikmykid
ย 
PDF
How to get the licensing right for Microsoft Core Infrastructure Server Suite...
Q-Advise
ย 
BB FlashBack Pro 5.61.0.4843 With Crack Free Download
cracked shares
ย 
Build a Custom Agent for Agentic Testing.pptx
klpathrudu
ย 
SAP GUI Installation Guide for macOS (iOS) | Connect to SAP Systems on Mac
SAP Vista, an A L T Z E N Company
ย 
Function & Procedure: Function Vs Procedure in PL/SQL
Shani Tiwari
ย 
Meet in the Middle: Solving the Low-Latency Challenge for Agentic AI
Alluxio, Inc.
ย 
How to Download and Install ADT (ABAP Development Tools) for Eclipse IDE | SA...
SAP Vista, an A L T Z E N Company
ย 
UI5con_2025_Accessibility_Ever_Evolving_
gerganakremenska1
ย 
API DOCUMENTATION | API INTEGRATION PLATFORM
philipnathen82
ย 
custom development enhancement | Togglenow.pdf
aswinisuhu
ย 
Show Which Projects Support Your Strategy and Deliver Results with OnePlan df
OnePlan Solutions
ย 
Transforming Insights: How Generative AI is Revolutionizing Data Analytics
LetsAI Solutions
ย 
SAP GUI Installation Guide for Windows | Step-by-Step Setup for SAP Access
SAP Vista, an A L T Z E N Company
ย 
Notification System for Construction Logistics Application
Safe Software
ย 
Ready Layer One: Intro to the Model Context Protocol
mmckenna1
ย 
ERP Consulting Services and Solutions by Contetra Pvt Ltd
jayjani123
ย 
Step-by-Step Guide to Install SAP HANA Studio | Complete Installation Tutoria...
SAP Vista, an A L T Z E N Company
ย 
Salesforce Experience Cloud Consultant.pdf
VALiNTRY360
ย 
Cutting Optimization Pro 5.18.2 Crack With Free Download
cracked shares
ย 
How Attendance Management Software is Revolutionizing Education.pdf
Pikmykid
ย 
How to get the licensing right for Microsoft Core Infrastructure Server Suite...
Q-Advise
ย 

Docopt, beautiful command-line options for R, user2014

  • 1. Docopt, beautiful command-line options for R Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS) July 2014, UseR!2014 Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS) Docopt, beautiful command-line options for R
  • 2. What is docopt? docopt is an utility R library for parsing command-line options. It is a port of docopt.py (python). How does it work? You supply a properly formed help description docopt creates from this a fully functional command-line parser Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS) Docopt, beautiful command-line options for R
  • 3. Why Command-line? R is used more and more: Ad hoc, interactive analysis, e.g R REPL shell RStudio interactive data analysis Creating R libraries with vi, Rstudio etc. no data analysis But also for repetitive batch jobs: Rscript my_script.R arg1 arg2 . . . R -f my_script.R --args arg1 arg2 . . . reproducible data processing So also more and more Command-line! Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS) Docopt, beautiful command-line options for R
  • 4. Rscript example #!/usr/bin/Rscript my_model <- glm( data=iris , Sepal.Width ~ Sepal.Length ) print(coef(my_model)) Hmm, that script only works for this speci๏ฌc data set. I Need Arguments and Options! Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS) Docopt, beautiful command-line options for R
  • 5. Command-line parameters Parsing command-line parameters seems easy, but what about: Switches? e.g. --debug, --help Short names and long names? -d, -h vs --debug, --help? Options with a value? --output=garbage.csv Arguments e.g. input_file.csv? Optional arguments? default values for options? documenting all options and arguments? That is a lot of work for just a batch script. . . Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS) Docopt, beautiful command-line options for R
  • 6. Retrieving command-line options What libraries available? base::commandArgs (very primitive) library(getopt): (basic) library(argparse), Python dependency library(optparse) very nice, Python inspired These are all ๏ฌne, but result in a lot of parsing or settting-up code in your script. (and that is not what your script is about. . . ) docopt is di๏ฌ€erent. Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS) Docopt, beautiful command-line options for R
  • 7. What is Docopt? Originally a Python lib: http://docopt.org It is a Command Line Interface Speci๏ฌcation language: You specify your help and docopt parser takes care of everything. The documentation = the speci๏ฌcation. Your script starts with the command-line help docopt automatically has --help or -h switch to supply help to users of your script. It will stop when obligatory switch are not set or non existing options are set. Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS) Docopt, beautiful command-line options for R
  • 8. Simple example #!/usr/bin/Rscript "This is my incredible script Usage: my_inc_script.R [-v --output=<output>] FILE " -> doc library(docopt) my_opts <- docopt(doc) Thatโ€™s all you need to handle your command-line options. Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS) Docopt, beautiful command-line options for R
  • 9. Options Docopt lets you parse: Both short as long options Default values Descriptions of parameters Optional parameters: my_script.R [-a -b] Commands: my_script.R (lm | summary) Positional arguments Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS) Docopt, beautiful command-line options for R
  • 10. Usage patterns Syntax is de๏ฌned at http://docopt.org Start with Usage: "Usage: script.R --option <argument> script.R [<optional-argument>] script.R --another-option=<with-argument> script.R (--either-that-option | <or-this-argument>) script.R <repeating-argument> <repeating-argument>... " -> doc Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS) Docopt, beautiful command-line options for R
  • 11. Longer example #!/usr/bin/Rscript "This is my useful scriptI I use on everything Usage: my_uf_script.R [options] FILE Options: -b --bogus This is a bogus switch -o --output=OUTPUT output file [default: out.csv] Arguments: FILE the input file" -> doc library(docopt) my_opts <- docopt(doc) Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS) Docopt, beautiful command-line options for R
  • 12. Recall ๏ฌrst example Lets make a CLI for our script #!/usr/bin/Rscript my_model <- glm( data=iris , Sepal.Width ~ Sepal.Length ) print(coef(my_model)) Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS) Docopt, beautiful command-line options for R
  • 13. Preparing. . . #!/usr/bin/Rscript main <- function( DATA, response, terms, family){ data <- read.csv(DATA) f <- as.formula(paste0(response, " ~ ", terms)) my_model <- glm(f, family=family, data=data) print(coef(my_model)) } Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS) Docopt, beautiful command-line options for R
  • 14. Done! "Usage: my_script.R --response=<y> --terms=<x> [--family=<family>] DATA Options: -r --response=<y> Response for glm -t --terms=<x> Terms for glm -f --family=<family> Family [default: gaussian] Arguments: DATA Input data frame" -> doc main <- function( DATA, response, terms, family){...} opt <- docopt::docopt(doc) main(opt$DATA, opt[["--response"]], opt[["--terms"]], opt[["--family"]]) Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS) Docopt, beautiful command-line options for R
  • 15. Implementation Docopt is implemented: using Reference classes (R5) in pure R. It is port of the original Python project: http://docopt.org Available from: CRAN and https://github.com/edwindj/docopt.R Very functional, except for: multiple identical arguments -vvv repeating arguments (both will be ๏ฌxed soon) Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS) Docopt, beautiful command-line options for R
  • 16. Questions? $ my_talk.R --help Edwins talk on docopt Usage: my_talk.R (--questions | --fell-asleep) Options: -q --questions Anyone any questions? -f --fell-asleep Wake up! Next UseR talk! $ my_talk.R --questions Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS) Docopt, beautiful command-line options for R
  • 17. Questions? Thanks for listening! Edwin de Jonge (@edwindjonge), Statistics Netherlands (CBS) Docopt, beautiful command-line options for R