<?xml version='1.0'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:atom="http://www.w3.org/2005/Atom" >
<channel>
	<title><![CDATA[BOL: Collecting arguments with R]]></title>
	<link>https://bioinformaticsonline.com/snippets/view/38164/collecting-arguments-with-r?</link>
	<atom:link href="https://bioinformaticsonline.com/snippets/view/38164/collecting-arguments-with-r?" rel="self" type="application/rss+xml" />
	<description><![CDATA[]]></description>
	
	<item>
	<guid isPermaLink="true">https://bioinformaticsonline.com/snippets/view/38164/collecting-arguments-with-r</guid>
	<pubDate>Fri, 09 Nov 2018 12:21:40 -0600</pubDate>
	<link>https://bioinformaticsonline.com/snippets/view/38164/collecting-arguments-with-r</link>
	<title><![CDATA[Collecting arguments with R]]></title>
	<description><![CDATA[<code>#! /usr/bin/Rscript

## Collect arguments
args &lt;- commandArgs(TRUE)

## Parse arguments (we expect the form --arg=value)
parseArgs &lt;- function(x) strsplit(sub(&quot;^--&quot;, &quot;&quot;, x), &quot;=&quot;)
argsL &lt;- as.list(as.character(as.data.frame(do.call(&quot;rbind&quot;, parseArgs(args)))$V2))
names(argsL) &lt;- as.data.frame(do.call(&quot;rbind&quot;, parseArgs(args)))$V1
args &lt;- argsL
rm(argsL)

## Give some value to options if not provided 
if(is.null(args$opt_arg1)) {args$opt_arg1=&quot;default_option1&quot;}
if(is.null(args$opt_arg2)) {args$opt_arg2=&quot;default_option1&quot;} else {args$opt_arg2=as.numeric(args$opt_arg2)}

## Default setting when no all arguments passed or help needed
if(&quot;--help&quot; %in% args | is.null(args$arg1) | is.null(args$arg2)) {
  cat(&quot;
      The R Script arguments_section.R
      
      Mandatory arguments:
      --arg1=type           - description
      --arg2=type           - description
      --help                - print this text
      
      Optionnal arguments:
      --opt_arg1=String          - example:an absolute path, default:default_option1
      --opt_arg2=Value           - example:a threshold, default:10

      WARNING : here put all the things the user has to know
      
      Example:
      ./arguments_section.R --arg1=~/Documents/ --arg2=10 --opt_arg2=8 \n\n&quot;)
  
  q(save=&quot;no&quot;)
}

cat(&quot;first mandatory argument : &quot;, args$arg1,&quot;\n&quot;,sep=&quot;&quot;)
cat(&quot;second mandatory argument : &quot;, args$arg2,&quot;\n&quot;,sep=&quot;&quot;)
cat(&quot;first optional argument : &quot;, args$opt_arg1,&quot;\n&quot;,sep=&quot;&quot;)
cat(&quot;second optional argument : &quot;, args$opt_arg2,&quot;\n&quot;,sep=&quot;&quot;)</code>]]></description>
	<dc:creator>Neel</dc:creator>
</item>

</channel>
</rss>