funr
r fun(ction): attempt to make a fun cli for R
A small utility which wraps Rscript and provides easy access to all R functions from the terminal.
>funr
This aims to provide an easy command-line interface to all R functions.
Usage: funr [-h -v] <name of a function> <arguments to the function [<options>=<value>]>
funr -h Show this help
funr -h <function> Show help for a specific function
funr -v Show extra verbose messages for debugging this package
funr <func> Find and run <function>
funr <func> [args] Run <func> with supplied [arguments]
funr <pkg::func> Load the package <pkg>, and then run <func>
Examples:
Using a base package
## load help for rnorm
funr -h rnorm
Normal package:stats R Documentation
The Normal Distribution
Description:
Density, distribution function, quantile function and random
## sample 10 numbers from a normal distribution
funr rnorm n=10
-1.244571 1.378112 0.02189023 -0.3723951 0.282709 -0.22854 -0.8476185 0.3222024 0.08937781 -0.4985827
Using a non-base package
Loading a package automatically by ::
## load help file for knitr
funr -h knitr::knit
knit package:knitr R Documentation
Knit a document
Description:
This function takes an input file, extracts the R code in it
according to a list of patterns, evaluates the code and writes the
output in another file. It can also tangle R source code from the
Knit a HTML file using a example R markdown input from knitr package:
## get path to an example Rmd file. Assuming we have knitr installed.
## Save the filename in a BASH variable rmd
## Pipes are supported starting version 0.1.2
funr system.file package=knitr fl=examples/knitr-minimal.Rmd | funr knitr::knit2html input=-
loading pkg: knitr
processing file: ... knitr/examples/knitr-minimal.Rmd
output file: knitr-minimal.md
knitr-minimal.html
Adding funr to your scripts
Create a file (called norm.r), which looks like the following:
#!/usr/bin/env Rscript
# define your functions
norm_hist <- function(n){
x = rnorm(n)
hist(x)
}
library(funr)
out = funr(commandArgs(trailingOnly = TRUE))
Make sure you have the shebang like with #!/usr/bin/env Rscript
, so that shell knows that this is a Rscript.
Also, add execution privilige to the script using:
chmod ugo+rx norm.r
Now, one may call the function:
norm.r norm_hist n=100
## OR
Rscript norm.r norm_hist n=100