-
-
Save kos59125/5c164d8694c7deb45b32 to your computer and use it in GitHub Desktop.
R as a Shell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
R Package is created: see https://github.com/kos59125/RaaS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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