1. How to run the r script:
* ******** In the r environment:
Method 1:
Click "file-open program script" in the menu, open chisq2.r, select all, right click, and click "run current line or selected code".
Method 2:
Source ("chisq2.r ")
However, you must first set the file path:
For example, if your file is on the desktop
First
Setwd ("C:/users/qoiqpwqr/desktop ")
* ******* In the command line or linix environment:
Method 1. Run R in the background
(1) create a file. r File
(2) In the first line of the file, type:
#! /Path/to/rscript
(3) In the following line, type the r code
(4) Save (remember that the code for functions such as PNG (), JPEG (),... must end with Dev. Off ())
(5) In the working directory of file. R, enter the following command in the terminal:
R cmd batch -- ARGs file. r
(6) file. R is running !!!
Advantage: R command line prompts the wrong command and then returns the carriage return. All the previous commands have been replaced. If you do not want this to happen, write a file. r file.
Disadvantage: The R cmd batch mode enables R to run file. r in the system background, and the system gets stuck for 2 s.
Method 2. Create an R script
(1) create a file. r File
(2) In the first line of the file, type:
#! /Usr/bin/ENV rscript
(3) In the following line, type the r code
(4) Save (remember that the code for functions such as PNG (), JPEG (),... must end with Dev. Off ())
(5) In the working directory of file. R, enter the following command in the terminal:
R cmd batch -- ARGs file. r
Or rscript file. r
(6) file. R is running !!!
Advantage: compared to the former, it does not occupy much system resources and can be run directly on the terminal, so that it can be applied together with the bash language. The details are as follows:
Chmod 755 file. r
./File. R # in this way, change file. R to file. Sh or file..., and run it in Ubuntu.
Method 3. Insert the r code in the bash script
(1) In the file. Sh file, type:
#! /Bin/sh
R -- slave [other option] <EOF
# R code
EOF
(2) run on a terminal
Chmod 755 file. Sh
./File. Sh
(3) file. Sh is running, including the r code
Advantage: R code can be inserted into bash scripts to run in other more powerful scripts. In fact, R-Python has become a python module.
2. Obtain and set environment variables:
Problem
You want to view or change the value of an environment variable.
Solution
Use the SYS. getenv function to view the environment variable value, use the SYS. setenv function to set the environment variable value, and use the SYS. putenv function to change the environment variable value:
> SYS. getenv ("shell ")
Shell
"/Bin/bash"
> SYS. setenv (shell = "/bin/KSh ")
Or
> SYS. putenv (shell = "/bin/KSh ")
Appendix: (differences between putenv and setenv)
The putenv () function does not copy the environment variable values to the process environment table, but stores the pointer to the environment variable values. The setenv () function completely copies the environment variable values to the process environment table.