R language learning notes

Source: Internet
Author: User

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.

    • Putenv can use a string variable defined in the program and with a value such as "name = value" as the real parameter of the function. In this caseNo longer allocate memory for this environment variableThe variable memory defined in the program is used, but the variable address of the string is saved in the environment. Therefore, this variable should be defined as a global variable to prevent environment variables from being unavailable after the function is released.
      char *var="sex=male";putenv(var);

       


    • Putenv can use a constant string such as "name = value" as the real parameter of the function. In this case, the system needsAllocate memory, Stores the string "name = value ".
      putenv("sex=male");

       


    • SetenvMemory must be allocated, Stores the string "name = value ";
      setenv("sex","male",1);

       

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.