Basic Syntax in R

 

cover

R Basic Syntax

R basic syntax | how to run r script | how to make comment in R | introduction to R | R programming language | how to Run R using command prompt | how to run R script. | R basics

 

Hello friends in this tutorial we are going to check out the basic syntax of R programming language. After completing this tutorial you are get to know:

  • How to Run R from command prompt
  • How to handle basic operations in R
  • How to print values and strings in R
  • How to handle comments in R
  • how to create and save R script file

so lets get started.

Note: we assumed that you have installed R to your system and setup enviornment variable successfully.

R Command Prompt:

once you installed R to your system and setup the environment successfully so can run it from your command line window by given a simple command “R”. this command will execute and take you to the R environment.

startR

so as you can see in above screen shot we given command R to our Terminal same you can give in your windows DOS, as well as you can see how R window in command prompt looks like.

Basic Working in R:

So now you ran R to your terminal now you can make basic operations like arithmetic  operations or print any string value simply. Check the below screen:

r operations

in above screen we performed basic arithmetic operations and printed strings as well you can try by your own.

Comments in R:

Comments are very useful for developers and users to make their code understandable. Comments can be place in R code or scripts which are present in your code file but not executable.

You can execute comment by given a “#” in front of any line of your document.

Example:

#This is a comment in R

R does not execute multi-line comment but the trick you can use to execute is:

#Trick for multi-line comment

    if(FALSE) {

    "This is a simple trick

    for using multiline comment in R "

    }

R Script File:

So now at last not least we will create a R script file. R script file uses the extension “.R” so whenever you are going to save you R file use “yourFileNameHere.R” to create a R script file.

To create a R file you can open any of your favorite text editor and start creating your code. A simple example given below:

r script

 

once you are done with your program save your file with “.R” extention as shown below:


now you can run your R script directly from your command prompt or terminal window. To execute your R file give the command:

$Rscript filename.R

Note: be sure you are in same directory where you save your file or give the same path to execute.

Check the below image:

run r

so as shown in above image you can run your R file.

At last we covered below topics hope you can able to answer anyone about below queries:

  • How to Run R from command prompt
  • How to handle basic operations in R
  • How to print values and strings in R
  • How to handle comments in R
  • how to create and save R script file

You can download install and run Rstudio IDE to run R directly without command prompt or terminal with Graphical view.

Read More:

Hope you enjoyed this tutorial. We will get back to you with another R tutorial soon.

 

Feel free to leave your valuable comments below.

 

Thank you


4 comments:

  1. Very well explained.. keep sharing like this..

    ReplyDelete
  2. Very well explained. Thanks for sharing

    ReplyDelete
  3. Keep sharing this kind of informative posts it helps to get and clear more points...

    ReplyDelete

INSTAGRAM FEED

@soratemplates