Skip to content

Instantly share code, notes, and snippets.

@kos59125
Last active August 29, 2015 14:13
Show Gist options
  • Save kos59125/5c164d8694c7deb45b32 to your computer and use it in GitHub Desktop.
Save kos59125/5c164d8694c7deb45b32 to your computer and use it in GitHub Desktop.
R as a Shell
R Package is created: see https://github.com/kos59125/RaaS
> source("shell.R")
> as.command(ls)
> # Just you type "ls" as a command
> ls
Applications Documents Library ...
> # Arguments are passed with brackets
> ls["-l", "Documents"]
total 1456
drwxr-xr-x 6 user user 204 1 17 20:26 Digital Editions
...
> # Original ls function also goes
> ls()
[1] "[.command" "as.command" "print.command" "print.dev.null"
print.command <- system
print.dev.null <- function(...) {}
`[.command` <- function(cmd, ...) {
args <- sapply(list(...), shQuote)
cmd <- paste(c(cmd, args), collapse=" ")
result <- system(cmd)
class(result) <- "dev.null"
result
}
as.command <- function(cmd) {
e <- as.environment(-1)
cmd <- deparse(substitute(cmd))
class(cmd) <- "command"
assign(cmd, cmd, envir=e)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment