Gli esempi seguenti mostrano due modi per eseguire codice R in uno script di shell. Entrambi gli esempi definiranno anche le funzioni senza eseguirle, se gli script sono caricati in una sessione R interattiva tramite la funzione source().
Il primo esempio consente di dare argomenti come si farebbe per qualsiasi altro shell script, ma non passerà supplementari R-options a R (perché rscript dà "--args" alla R come uno degli argomenti) .
Il secondo esempio consente di dare ulteriori R-opzioni, ma genera messaggi (innocui) di avvertimento a meno che non si dà "--args" come uno degli script di argomenti. Questa versione è meglio evitare se non si hanno requisiti speciali.
prototipo-Rscript.r
#!/usr/bin/env Rscript
# Prototype R script for use at command line in Linux, Mac OS X, UNIX
# References:
# Manual "A Introduction to R", available via help.start() from the R Console
# Appendix "B.1 Invoking R from the command line" in "A Inroduction to R",
showArguments <- function(argv) {
print(argv)
0
}
if (! interactive()) {
# set some error return codes
SCRIPT_ERROR <- 10 # see documentation for quit()
SCRIPT_ARG_ERROR <- SCRIPT_ERROR + 1
# Define ARGV as script path concatenated to script arguments
ARGV <- commandArgs(FALSE) # start with all the arguments given to R
scriptPath <- sub("^--file=", "", grep("^--file=", ARGV, value=TRUE)) [[1]]
ARGV <- c(scriptPath, commandArgs(TRUE))
if (length(ARGV) < 2) {
cat(file=stderr(), sep="",
"Usage: ", ARGV[[1]], " [ options ] item ...\n",
" Do something with item\n",
" See script for details\n")
quit(save="no", status=SCRIPT_ARG_ERROR)
}
quit(save="no", status=showArguments(ARGV))
}
prototipo-shellscript.r
#!/usr/bin/env R --slave --vanilla --quiet -f
# Prototype R script for use at command line in Linux, Mac OS X, UNIX
# References:
# Manual "A Introduction to R", available via help.start() from the R Console
# Appendix "B.1 Invoking R from the command line" in "A Inroduction to R",
showArguments <- function(argv) {
print(argv)
0
}
if (! interactive()) {
# set some error return codes
SCRIPT_ERROR <- 10 # see documentation for quit()
SCRIPT_ARG_ERROR <- SCRIPT_ERROR + 1
# Define ARGV as the arguments given to this script (after argument “-f”)
ARGV <- commandArgs(FALSE) # start with all the arguments given to R
ARGV <- ARGV[(grep("-f", ARGV) [[1]] + 1):length(ARGV)]
if (any(grepl("--args", ARGV))) { # remove arguments intended only for R
ARGV <- c(ARGV[[1]], commandArgs(TRUE))
}
if (length(ARGV) < 2) {
cat(file=stderr(), sep="",
"Usage: ", ARGV[[1]], " [ R_options ] --args [ options ] item ...\n",
" Do something with item\n",
" See script for details\n")
quit(save="no", status=SCRIPT_ARG_ERROR)
}
quit(save="no", status=showArguments(ARGV))
}
Forse si dovrebbe utilizzare R per Linux? –
Ci scusiamo per la semplice domanda, qual è la differenza tra linux e unix R? Credo che possiamo eseguire R in Unix – SHRram
Che cosa hai provato? R dovrebbe installarsi bene su unix o linux, e puoi accedervi tramite la riga di comando con 'R'. Puoi anche guardare alcuni degli eccellenti strumenti disponibili (suggerirei [RStudio] (http://www.rstudio.org) come un eccellente punto di partenza). Infine, eseguire uno script può essere fatto facilmente. Spesso usi 'R CMD BATCH script.R' ma ci sono molte alternative e opzioni che sono ben documentate. – Justin