- R Programming By Example
- Omar Trejo Navarro
- 405字
- 2021-07-02 21:30:35
Executing R code in non-interactive sessions
Once your code has the functionality you were looking to implement, executing it through an interactive session using the console may not be the best way to do so. In such cases, another option you have is to tell your computer to directly execute the code for you, in a non-interactive session. This means that you won't be able to type commands into the console, but you'll get the benefit of being able to configure your computer to automatically execute code for you, or to integrate it into larger systems where R is only one of many components. This is known as batch mode.
To execute code in the batch mode, you have two options: the old R CMD BATCH command which we won't look into, and the newer Rscript command, which we will. The Rscript is a command that you can execute within your computer's terminal. It receives the name of a source code file and executes its contents.
In the following example, we will make use of various concepts that we will explain in later sections, so if you don't feel ready to understand it, feel free to skip it now and come back to it later.
Suppose you have the following code in a file named greeting.R. It gets the arguments passed through the command line to Rscript through the args object created with the commandArgs() function, assigns the corresponding values to the greeting and name variables, and finally prints a vector that contains those values.
args <- commandArgs(TRUE) greeting <- args[1] name <- args[2] print(c(greeting, name))
Once ready, you may use the Rscript command to execute it from your Terminal (not from within your R console) as is shown ahead. The result shows the vector with the greeting and name variable values you passed it.
When you see a Command Prompt that begins with the $ symbol instead of of the > symbol, it means that you should execute that line in your computer's Terminal, not in the R console.
$ Rscript greeting.R Hi John [1] "Hi" "John"
Note that if you simply execute the file without any arguments, they will be passed as NA values, which allows you to customize your code to deal with such situations:
$ Rscript greeting.R [1] NA NA
This was a very simple example, but the same mechanism can be used to execute much more complex systems, like the one we will build in the final chapters of this book to constantly retrieve real-time price data from remote servers.
Finally, if you want to provide a mechanism that is closer to the one in Python, you may want to look into the optparse package to create command-line help pages as well as to parse arguments.
- 面向STEM的mBlock智能機器人創(chuàng)新課程
- 協(xié)作機器人技術(shù)及應(yīng)用
- Getting Started with MariaDB
- VMware Performance and Capacity Management(Second Edition)
- 最后一個人類
- 網(wǎng)絡(luò)綜合布線設(shè)計與施工技術(shù)
- 變頻器、軟啟動器及PLC實用技術(shù)260問
- 大學(xué)C/C++語言程序設(shè)計基礎(chǔ)
- 邊緣智能:關(guān)鍵技術(shù)與落地實踐
- Hands-On SAS for Data Analysis
- 工業(yè)機器人操作
- Microsoft System Center Data Protection Manager Cookbook
- SQL Server 2019 Administrator's Guide
- 數(shù)據(jù)結(jié)構(gòu)與實訓(xùn)
- 工業(yè)機器人編程指令詳解