SlideShare a Scribd company logo
1 of 17
Download to read offline
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 specific 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 fine, 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 different.
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 Specification language:
You specify your help and docopt parser takes care of
everything.
The documentation = the specification.
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 defined 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 first 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 fixed 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

Viewers also liked

3 descriptive statistics with R
3 descriptive statistics with R3 descriptive statistics with R
3 descriptive statistics with Rnaroranisha
 
Presentation on use of r statistics
Presentation on use of r statisticsPresentation on use of r statistics
Presentation on use of r statisticsKrishna Dhakal
 
Dependencies and Licenses
Dependencies and LicensesDependencies and Licenses
Dependencies and LicensesRobert Reiz
 
Descriptive Statistics with R
Descriptive Statistics with RDescriptive Statistics with R
Descriptive Statistics with RKazuki Yoshida
 
Chunked, dplyr for large text files
Chunked, dplyr for large text filesChunked, dplyr for large text files
Chunked, dplyr for large text filesEdwin de Jonge
 
Introduction to basic statistics
Introduction to basic statisticsIntroduction to basic statistics
Introduction to basic statisticsIBM
 
How to use Logistic Regression in GIS using ArcGIS and R statistics
How to use Logistic Regression in GIS using ArcGIS and R statisticsHow to use Logistic Regression in GIS using ArcGIS and R statistics
How to use Logistic Regression in GIS using ArcGIS and R statisticsOmar F. Althuwaynee
 
Why R? A Brief Introduction to the Open Source Statistics Platform
Why R? A Brief Introduction to the Open Source Statistics PlatformWhy R? A Brief Introduction to the Open Source Statistics Platform
Why R? A Brief Introduction to the Open Source Statistics PlatformSyracuse University
 
عرض محاضرة كيفية انشاء المطاعم لرؤوس الاموال الناشئة والمبتدئة
عرض محاضرة كيفية انشاء المطاعم لرؤوس الاموال الناشئة والمبتدئةعرض محاضرة كيفية انشاء المطاعم لرؤوس الاموال الناشئة والمبتدئة
عرض محاضرة كيفية انشاء المطاعم لرؤوس الاموال الناشئة والمبتدئةMazen AlDarrab
 
R statistics with mongo db
R statistics with mongo dbR statistics with mongo db
R statistics with mongo dbMongoDB
 

Viewers also liked (13)

3 descriptive statistics with R
3 descriptive statistics with R3 descriptive statistics with R
3 descriptive statistics with R
 
Presentation on use of r statistics
Presentation on use of r statisticsPresentation on use of r statistics
Presentation on use of r statistics
 
Dependencies and Licenses
Dependencies and LicensesDependencies and Licenses
Dependencies and Licenses
 
Descriptive Statistics with R
Descriptive Statistics with RDescriptive Statistics with R
Descriptive Statistics with R
 
Chunked, dplyr for large text files
Chunked, dplyr for large text filesChunked, dplyr for large text files
Chunked, dplyr for large text files
 
Using R For Statistics
Using R For StatisticsUsing R For Statistics
Using R For Statistics
 
Introduction to basic statistics
Introduction to basic statisticsIntroduction to basic statistics
Introduction to basic statistics
 
How to use Logistic Regression in GIS using ArcGIS and R statistics
How to use Logistic Regression in GIS using ArcGIS and R statisticsHow to use Logistic Regression in GIS using ArcGIS and R statistics
How to use Logistic Regression in GIS using ArcGIS and R statistics
 
Why R? A Brief Introduction to the Open Source Statistics Platform
Why R? A Brief Introduction to the Open Source Statistics PlatformWhy R? A Brief Introduction to the Open Source Statistics Platform
Why R? A Brief Introduction to the Open Source Statistics Platform
 
عرض محاضرة كيفية انشاء المطاعم لرؤوس الاموال الناشئة والمبتدئة
عرض محاضرة كيفية انشاء المطاعم لرؤوس الاموال الناشئة والمبتدئةعرض محاضرة كيفية انشاء المطاعم لرؤوس الاموال الناشئة والمبتدئة
عرض محاضرة كيفية انشاء المطاعم لرؤوس الاموال الناشئة والمبتدئة
 
Class ppt intro to r
Class ppt intro to rClass ppt intro to r
Class ppt intro to r
 
R statistics with mongo db
R statistics with mongo dbR statistics with mongo db
R statistics with mongo db
 
Data analysis using spss
Data analysis using spssData analysis using spss
Data analysis using spss
 

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

Language-agnostic data analysis workflows and reproducible research
Language-agnostic data analysis workflows and reproducible researchLanguage-agnostic data analysis workflows and reproducible research
Language-agnostic data analysis workflows and reproducible researchAndrew Lowe
 
Massively Parallel Processing with Procedural Python (PyData London 2014)
Massively Parallel Processing with Procedural Python (PyData London 2014)Massively Parallel Processing with Procedural Python (PyData London 2014)
Massively Parallel Processing with Procedural Python (PyData London 2014)Ian Huston
 
What we can learn from Rebol?
What we can learn from Rebol?What we can learn from Rebol?
What we can learn from Rebol?lichtkind
 
Data Science Amsterdam - Massively Parallel Processing with Procedural Languages
Data Science Amsterdam - Massively Parallel Processing with Procedural LanguagesData Science Amsterdam - Massively Parallel Processing with Procedural Languages
Data Science Amsterdam - Massively Parallel Processing with Procedural LanguagesIan Huston
 
Massively Parallel Process with Prodedural Python by Ian Huston
Massively Parallel Process with Prodedural Python by Ian HustonMassively Parallel Process with Prodedural Python by Ian Huston
Massively Parallel Process with Prodedural Python by Ian HustonPyData
 
R and Python, A Code Demo
R and Python, A Code DemoR and Python, A Code Demo
R and Python, A Code DemoVineet Jaiswal
 
First Steps in Python Programming
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python ProgrammingDozie Agbo
 
Programming Under Linux In Python
Programming Under Linux In PythonProgramming Under Linux In Python
Programming Under Linux In PythonMarwan Osman
 
Hadoop with Python
Hadoop with PythonHadoop with Python
Hadoop with PythonDonald Miner
 
Python: an introduction for PHP webdevelopers
Python: an introduction for PHP webdevelopersPython: an introduction for PHP webdevelopers
Python: an introduction for PHP webdevelopersGlenn De Backer
 
Introduction to R and R Studio
Introduction to R and R StudioIntroduction to R and R Studio
Introduction to R and R StudioRupak Roy
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programmingNimrita Koul
 
Data Science - Part II - Working with R & R studio
Data Science - Part II -  Working with R & R studioData Science - Part II -  Working with R & R studio
Data Science - Part II - Working with R & R studioDerek Kane
 
Massively Parallel Processing with Procedural Python by Ronert Obst PyData Be...
Massively Parallel Processing with Procedural Python by Ronert Obst PyData Be...Massively Parallel Processing with Procedural Python by Ronert Obst PyData Be...
Massively Parallel Processing with Procedural Python by Ronert Obst PyData Be...PyData
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAdam Getchell
 

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

Language-agnostic data analysis workflows and reproducible research
Language-agnostic data analysis workflows and reproducible researchLanguage-agnostic data analysis workflows and reproducible research
Language-agnostic data analysis workflows and reproducible research
 
Massively Parallel Processing with Procedural Python (PyData London 2014)
Massively Parallel Processing with Procedural Python (PyData London 2014)Massively Parallel Processing with Procedural Python (PyData London 2014)
Massively Parallel Processing with Procedural Python (PyData London 2014)
 
What we can learn from Rebol?
What we can learn from Rebol?What we can learn from Rebol?
What we can learn from Rebol?
 
Data Science Amsterdam - Massively Parallel Processing with Procedural Languages
Data Science Amsterdam - Massively Parallel Processing with Procedural LanguagesData Science Amsterdam - Massively Parallel Processing with Procedural Languages
Data Science Amsterdam - Massively Parallel Processing with Procedural Languages
 
Massively Parallel Process with Prodedural Python by Ian Huston
Massively Parallel Process with Prodedural Python by Ian HustonMassively Parallel Process with Prodedural Python by Ian Huston
Massively Parallel Process with Prodedural Python by Ian Huston
 
Pyhton-1a-Basics.pdf
Pyhton-1a-Basics.pdfPyhton-1a-Basics.pdf
Pyhton-1a-Basics.pdf
 
biopython, doctest and makefiles
biopython, doctest and makefilesbiopython, doctest and makefiles
biopython, doctest and makefiles
 
R and Python, A Code Demo
R and Python, A Code DemoR and Python, A Code Demo
R and Python, A Code Demo
 
First Steps in Python Programming
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python Programming
 
Programming Under Linux In Python
Programming Under Linux In PythonProgramming Under Linux In Python
Programming Under Linux In Python
 
Hadoop with Python
Hadoop with PythonHadoop with Python
Hadoop with Python
 
Python: an introduction for PHP webdevelopers
Python: an introduction for PHP webdevelopersPython: an introduction for PHP webdevelopers
Python: an introduction for PHP webdevelopers
 
Introduction to R and R Studio
Introduction to R and R StudioIntroduction to R and R Studio
Introduction to R and R Studio
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programming
 
Python Course.docx
Python Course.docxPython Course.docx
Python Course.docx
 
Unit V.pdf
Unit V.pdfUnit V.pdf
Unit V.pdf
 
Data Science - Part II - Working with R & R studio
Data Science - Part II -  Working with R & R studioData Science - Part II -  Working with R & R studio
Data Science - Part II - Working with R & R studio
 
Getting started with R
Getting started with RGetting started with R
Getting started with R
 
Massively Parallel Processing with Procedural Python by Ronert Obst PyData Be...
Massively Parallel Processing with Procedural Python by Ronert Obst PyData Be...Massively Parallel Processing with Procedural Python by Ronert Obst PyData Be...
Massively Parallel Processing with Procedural Python by Ronert Obst PyData Be...
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
 

More from Edwin de Jonge

Validatetools, resolve and simplify contradictive or data validation rules
Validatetools, resolve and simplify contradictive or data validation rulesValidatetools, resolve and simplify contradictive or data validation rules
Validatetools, resolve and simplify contradictive or data validation rulesEdwin de Jonge
 
Data error! But where?
Data error! But where?Data error! But where?
Data error! But where?Edwin de Jonge
 
Daff: diff, patch and merge for data.frame
Daff: diff, patch and merge for data.frameDaff: diff, patch and merge for data.frame
Daff: diff, patch and merge for data.frameEdwin de Jonge
 
Uncertainty visualisation
Uncertainty visualisationUncertainty visualisation
Uncertainty visualisationEdwin de Jonge
 
Heatmaps best practices Strata Hadoop
Heatmaps best practices Strata HadoopHeatmaps best practices Strata Hadoop
Heatmaps best practices Strata HadoopEdwin de Jonge
 
Big Data Visualization
Big Data VisualizationBig Data Visualization
Big Data VisualizationEdwin de Jonge
 
ffbase, statistical functions for large datasets
ffbase, statistical functions for large datasetsffbase, statistical functions for large datasets
ffbase, statistical functions for large datasetsEdwin de Jonge
 
Tabplotd3, interactive inspection of large data
Tabplotd3, interactive inspection of large dataTabplotd3, interactive inspection of large data
Tabplotd3, interactive inspection of large dataEdwin de Jonge
 
Big data as a source for official statistics
Big data as a source for official statisticsBig data as a source for official statistics
Big data as a source for official statisticsEdwin de Jonge
 
Statmine, Visuele dataexploratie
Statmine, Visuele dataexploratieStatmine, Visuele dataexploratie
Statmine, Visuele dataexploratieEdwin de Jonge
 
StatMine (New Technologies and Techniques for Statistics)
StatMine (New Technologies and Techniques for Statistics)StatMine (New Technologies and Techniques for Statistics)
StatMine (New Technologies and Techniques for Statistics)Edwin de Jonge
 
StatMine, visual exploration of output data
StatMine, visual exploration of output dataStatMine, visual exploration of output data
StatMine, visual exploration of output dataEdwin de Jonge
 

More from Edwin de Jonge (15)

sdcSpatial user!2019
sdcSpatial user!2019sdcSpatial user!2019
sdcSpatial user!2019
 
Validatetools, resolve and simplify contradictive or data validation rules
Validatetools, resolve and simplify contradictive or data validation rulesValidatetools, resolve and simplify contradictive or data validation rules
Validatetools, resolve and simplify contradictive or data validation rules
 
Data error! But where?
Data error! But where?Data error! But where?
Data error! But where?
 
Daff: diff, patch and merge for data.frame
Daff: diff, patch and merge for data.frameDaff: diff, patch and merge for data.frame
Daff: diff, patch and merge for data.frame
 
Uncertainty visualisation
Uncertainty visualisationUncertainty visualisation
Uncertainty visualisation
 
Heatmaps best practices Strata Hadoop
Heatmaps best practices Strata HadoopHeatmaps best practices Strata Hadoop
Heatmaps best practices Strata Hadoop
 
Big data experiments
Big data experimentsBig data experiments
Big data experiments
 
StatMine
StatMineStatMine
StatMine
 
Big Data Visualization
Big Data VisualizationBig Data Visualization
Big Data Visualization
 
ffbase, statistical functions for large datasets
ffbase, statistical functions for large datasetsffbase, statistical functions for large datasets
ffbase, statistical functions for large datasets
 
Tabplotd3, interactive inspection of large data
Tabplotd3, interactive inspection of large dataTabplotd3, interactive inspection of large data
Tabplotd3, interactive inspection of large data
 
Big data as a source for official statistics
Big data as a source for official statisticsBig data as a source for official statistics
Big data as a source for official statistics
 
Statmine, Visuele dataexploratie
Statmine, Visuele dataexploratieStatmine, Visuele dataexploratie
Statmine, Visuele dataexploratie
 
StatMine (New Technologies and Techniques for Statistics)
StatMine (New Technologies and Techniques for Statistics)StatMine (New Technologies and Techniques for Statistics)
StatMine (New Technologies and Techniques for Statistics)
 
StatMine, visual exploration of output data
StatMine, visual exploration of output dataStatMine, visual exploration of output data
StatMine, visual exploration of output data
 

Recently uploaded

What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 

Recently uploaded (20)

What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 

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 specific 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 fine, 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 different. 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 Specification language: You specify your help and docopt parser takes care of everything. The documentation = the specification. 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 defined 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 first 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 fixed 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