Los ejemplos a continuación muestran dos formas de ejecutar código R en un script de shell. Ambos ejemplos también definirán funciones sin ejecutarlas, si los scripts están cargados en una sesión R interactiva a través de la función source().
El primer ejemplo le permite dar argumentos como lo haría con cualquier otro tejido guión, pero no pasará-R opciones adicionales para R (porque RSCRIPT da "--args" a R como uno de los argumentos) .
El segundo ejemplo le permite ofrecer opciones R adicionales, pero genera (inofensivo) mensajes de advertencia a menos que especifique "--args" como uno de los argumentos de script . Esta versión es mejor evitarla a menos que tenga requisitos especiales.
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))
}
Tal vez deberías utilizar R para Linux? –
Disculpe la simple pregunta, ¿cuál es la diferencia entre linux y unix R? Creo que podemos ejecutar R en unix – SHRram
¿Qué has probado? R debe instalarse bien en Unix o Linux, y puede acceder a él a través de la línea de comando con 'R'. También puede ver algunos de los excelentes guis disponibles (sugeriría [RStudio] (http://www.rstudio.org) como un excelente punto de partida). Finalmente, ejecutar un script se puede hacer fácilmente. A menudo utilizas 'R CMD BATCH script.R' pero hay muchas alternativas y opciones que están bien documentadas. – Justin